- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathsplayTree.java
More file actions
Latest commit
177 lines (154 loc) · 4.55 KB
/
Copy pathsplayTree.java
File metadata and controls
177 lines (154 loc) · 4.55 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
importjava.util.Scanner;
classsplayTree {
classNode {
intkey; // Key value for the node
Nodeleft, right; // Left and right child nodes of the node
publicNode(intkey) {
this.key = key;
left = right = null;
}
}
Noderoot;
publicSplayTree() {
root = null;
}
// for searching the node
publicNodesearch(intkey) {
returnsearch(root, key);
}
/**
* Splay the node with the given key to the root of the tree.
* If the key is not in the tree, splay the last accessed node to the root.
*/
privateNodesearch(Noderoot, intkey) {
if (root == null || root.key == key) {
returnroot;
}
if (root.key > key) {
returnsearch(root.left, key);
} else {
returnsearch(root.right, key);
}
}
// inserting
publicvoidinsert(intkey) {
root = insert(root, key);
}
privateNodeinsert(Noderoot, intkey) {
if (root == null) {
returnnewNode(key);
}
root = splay(root, key);
if (key < root.key) {
NodenewNode = newNode(key);
newNode.right = root;
newNode.left = root.left;
root.left = null;
root = newNode;
} elseif (key > root.key) {
NodenewNode = newNode(key);
newNode.left = root;
newNode.right = root.right;
root.right = null;
root = newNode;
}
returnroot;
}
// deleting
publicvoiddelete(intkey) {
root = delete(root, key);
}
privateNodedelete(Noderoot, intkey) {
Nodetemp;
if (root == null) {
returnnull;
}
root = splay(root, key);
if (key == root.key) {
if (root.left == null) {
temp = root;
root = root.right;
} else {
temp = root;
root = splay(root.left, key);
root.right = temp.right;
}
temp = null;
}
returnroot;
}
privateNodesplay(Noderoot, intkey) {
if (root == null || root.key == key) {
returnroot;
}
if (key < root.key) {
if (root.left == null) {
returnroot;
}
if (key < root.left.key) {
root.left.left = splay(root.left.left, key);
root = rightRotate(root);
} elseif (key > root.left.key) {
root.left.right = splay(root.left.right, key);
if (root.left.right != null) {
root.left = leftRotate(root.left);
}
}
returnroot.left == null ? root : rightRotate(root);
} else {
if (root.right == null) {
returnroot;
}
if (key > root.right.key) {
root.right.right = splay(root.right.right, key);
root = leftRotate(root);
} elseif (key < root.right.key) {
root.right.left = splay(root.right.left, key);
if (root.right.left != null) {
root.right = rightRotate(root.right);
}
}
returnroot.right == null ? root : leftRotate(root);
}
/**
* Rotate the given node to the left.
*/
privateNodeleftRotate(Nodex) {
Nodey = x.right;
x.right = y.left;
y.left = x;
returny;
}
/**
* Rotate the given node to the right.
*/
privateNoderightRotate(Nodex) {
Nodey = x.left;
x.left = y.right;
y.right = x;
returny;
}
publicvoidinOrder() {
inOrder(root);
}
privatevoidinOrder(Noderoot) {
if (root != null) {
inOrder(root.left);
System.out.print(root.key + " ");
inOrder(root.right);
}
}
}
publicclassMain {
publicstaticvoidmain(String[] args) {
SplayTreespt = newSplayTree();
Scannersc = newScanner(System.in);
intn = sc.nextInt();
for (inti = 0; i < n; i++) {
intnum = sc.nextInt();
spt.insert(num);
}
spt.inOrder();
}
}
}