FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TreesPresentation/SplayTree.cpp at master · samplec0de/TreesPresentation · GitHub
samplec0de
/
TreesPresentation
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TreesPresentation
/
SplayTree.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
134 lines (124 loc) · 3.09 KB
Breadcrumbs
TreesPresentation
/
SplayTree.cpp
Copy path
File metadata and controls
134 lines (124 loc) · 3.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//
Created by Андрей Москалёв on 24.10.2019.
//
#
include
"
SplayTree.h
"
Node *
SplayTree::_rotateRight
(Node * k2) {
Node * k1 = k2->
left
;
k2->
left
= k1->
right
;
k1->
right
= k2;
return
k1;
}
Node *
SplayTree::_rotateLeft
(Node * k2) {
Node * k1 = k2->
right
;
k2->
right
= k1->
left
;
k1->
left
= k2;
return
k1;
}
Node *
SplayTree::_splay
(
int
key, Node * root) {
if
(root ==
nullptr
)
return
nullptr
;
Node head;
head.
left
= head.
right
=
nullptr
;
Node * leftMx = &head;
Node * rightMn = &head;
while
(
true
) {
if
(key < root->
key
) {
if
(root->
left
==
nullptr
) {
break
;
}
if
(key < root->
left
->
key
) {
root =
_rotateRight
(root);
if
(root->
left
==
nullptr
)
break
;
}
rightMn->
left
= root;
rightMn = rightMn->
left
;
root = root->
left
;
rightMn->
left
=
nullptr
;
}
else
if
(key > root->
key
) {
if
(root->
right
==
nullptr
)
break
;
if
(key > root->
right
->
key
) {
root =
_rotateLeft
(root);
if
(root->
right
==
nullptr
)
break
;
}
leftMx->
right
= root;
leftMx = leftMx->
right
;
root = root->
right
;
leftMx->
right
=
nullptr
;
}
else
break
;
}
leftMx->
right
= root->
left
;
rightMn->
left
= root->
right
;
root->
left
= head.
right
;
root->
right
= head.
left
;
return
root;
}
Node *
SplayTree::_insert
(
int
key, Node * root) {
static
Node * p_node =
nullptr
;
if
(p_node ==
nullptr
)
p_node =
new
Node
(key);
else
p_node->
key
= key;
if
(root ==
nullptr
) {
root = p_node;
p_node =
nullptr
;
return
root;
}
root =
_splay
(key, root);
if
(key < root->
key
) {
p_node->
left
= root->
left
;
p_node->
right
= root;
root->
left
=
nullptr
;
root = p_node;
}
else
if
(key > root->
key
) {
p_node->
right
= root->
right
;
p_node->
left
= root;
root->
right
=
nullptr
;
root = p_node;
}
else
return
root;
p_node =
nullptr
;
return
root;
}
Node *
SplayTree::_remove
(
int
key, Node * root) {
Node * tmp;
if
(root ==
nullptr
) {
return
nullptr
;
}
root =
_splay
(key, root);
if
(key != root->
key
)
return
root;
else
{
if
(root->
left
==
nullptr
) {
tmp = root;
root = root->
right
;
}
else
{
tmp = root;
root =
_splay
(key, root->
left
);
root->
right
= tmp->
right
;
}
delete
tmp;
return
root;
}
}
Node *
SplayTree::_findNode
(
int
key, Node * root) {
return
_splay
(key, root);
}
int
SplayTree::_height
(Node * x) {
if
(x ==
nullptr
) {
return
0
;
}
int
hLeft =
0
, hRight =
0
;
if
(x->
left
!=
nullptr
) {
hLeft =
_height
(x->
left
);
}
if
(x->
right
!=
nullptr
) {
hRight =
_height
(x->
right
);
}
return
1
+
std::max
(hLeft, hRight);
}
Back
|
FazBrowse Home
|
New Git URL