-
Notifications
You must be signed in to change notification settings - Fork 368
/
splayTree.java
177 lines (154 loc) · 4.55 KB
/
splayTree.java
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
import java.util.Scanner;
class splayTree {
class Node {
int key; // Key value for the node
Node left, right; // Left and right child nodes of the node
public Node(int key) {
this.key = key;
left = right = null;
}
}
Node root;
public SplayTree() {
root = null;
}
// for searching the node
public Node search(int key) {
return search(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.
*/
private Node search(Node root, int key) {
if (root == null || root.key == key) {
return root;
}
if (root.key > key) {
return search(root.left, key);
} else {
return search(root.right, key);
}
}
// inserting
public void insert(int key) {
root = insert(root, key);
}
private Node insert(Node root, int key) {
if (root == null) {
return new Node(key);
}
root = splay(root, key);
if (key < root.key) {
Node newNode = new Node(key);
newNode.right = root;
newNode.left = root.left;
root.left = null;
root = newNode;
} else if (key > root.key) {
Node newNode = new Node(key);
newNode.left = root;
newNode.right = root.right;
root.right = null;
root = newNode;
}
return root;
}
// deleting
public void delete(int key) {
root = delete(root, key);
}
private Node delete(Node root, int key) {
Node temp;
if (root == null) {
return null;
}
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;
}
return root;
}
private Node splay(Node root, int key) {
if (root == null || root.key == key) {
return root;
}
if (key < root.key) {
if (root.left == null) {
return root;
}
if (key < root.left.key) {
root.left.left = splay(root.left.left, key);
root = rightRotate(root);
} else if (key > root.left.key) {
root.left.right = splay(root.left.right, key);
if (root.left.right != null) {
root.left = leftRotate(root.left);
}
}
return root.left == null ? root : rightRotate(root);
} else {
if (root.right == null) {
return root;
}
if (key > root.right.key) {
root.right.right = splay(root.right.right, key);
root = leftRotate(root);
} else if (key < root.right.key) {
root.right.left = splay(root.right.left, key);
if (root.right.left != null) {
root.right = rightRotate(root.right);
}
}
return root.right == null ? root : leftRotate(root);
}
/**
* Rotate the given node to the left.
*/
private Node leftRotate(Node x) {
Node y = x.right;
x.right = y.left;
y.left = x;
return y;
}
/**
* Rotate the given node to the right.
*/
private Node rightRotate(Node x) {
Node y = x.left;
x.left = y.right;
y.right = x;
return y;
}
public void inOrder() {
inOrder(root);
}
private void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.key + " ");
inOrder(root.right);
}
}
}
public class Main {
public static void main(String[] args) {
SplayTree spt = new SplayTree();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
spt.insert(num);
}
spt.inOrder();
}
}
}