Repository navigation
Expand file tree
/
Copy pathSplayTree.java
More file actions
164 lines (142 loc) · 4.09 KB
/
Copy pathSplayTree.java
File metadata and controls
164 lines (142 loc) · 4.09 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
/**
* Created by newuser on 2/25/16.
*/
public class SplayTree<K extends Comparable<K>> {
node root;
public SplayTree(){
root=null;
}
public boolean empty(){
return root ==null;
}
public node search(K data){
return search(root,data);
}
private node search(node tree, K data){
if(tree==null){
return tree;
}
if (tree.data.compareTo(data)<0){
return search(tree.leftChild,data);
}
return search(tree.rightChild,data);
}
public void insert(K key) {
// splay key to root
if (root == null) {
root = new node(key);
return;
}
root = splay(root, key);
int cmp = key.compareTo(root.data);
// Insert new node at root
if (cmp < 0) {
node n = new node(key);
n.leftChild = root.leftChild;
n.rightChild = root;
root.leftChild= null;
root = n;
}
// Insert new node at root
else if (cmp > 0) {
node n = new node(key);
n.rightChild= root.rightChild;
n.leftChild= root;
root.rightChild= null;
root = n;
}
// It was a duplicate key. Simply replace the value
else if (cmp == 0) {
root.data = key;
}
}
public void inorder(){
inorder(root);
}
private void inorder(node root){
if(root!=null){
inorder(root.leftChild);
System.out.println(root.data);
inorder(root.rightChild);
}
}
public void preorder(){
preorder(root);
}
private void preorder(node root){
if(root!=null){
System.out.println(root.data);
inorder(root.leftChild);
inorder(root.rightChild);
}
}
private node splay(node h, K key) {
if (h == null) return null;
int cmp1 = key.compareTo(h.data);
if (cmp1 < 0) {
// key not in tree, so we're done
if (h.leftChild == null) {
return h;
}
int cmp2 = key.compareTo(h.leftChild.data);
if (cmp2 < 0) {
//zig-zig
h.leftChild.leftChild = splay(h.leftChild.leftChild, key);
h = rotateRight(h);
}
else if (cmp2 > 0) {
//zig-zag
h.leftChild.rightChild = splay(h.leftChild.rightChild, key);
if (h.leftChild.rightChild != null)
h.leftChild = rotateLeft(h.leftChild);
}
if (h.leftChild == null) return h;
else return rotateRight(h);
}
else if (cmp1 > 0) {
// key not in tree, so we're done
if (h.rightChild == null) {
return h;
}
int cmp2 = key.compareTo(h.rightChild.data);
if (cmp2 < 0) {
//zig-zag
h.rightChild.leftChild = splay(h.rightChild.leftChild, key);
if (h.rightChild.leftChild!= null)
h.rightChild = rotateRight(h.rightChild);
}
else if (cmp2 > 0) {
//zig-zig
h.rightChild.rightChild = splay(h.rightChild.rightChild, key);
h = rotateLeft(h);
}
if (h.rightChild == null){ return h;}
else{ return rotateLeft(h);}
}
else return h;
}
private node rotateRight(node h){
node x = h.leftChild;
h.leftChild = x.rightChild;
x.rightChild = h;
return x;
}
private node rotateLeft(node h){
node x = h.rightChild;
h.rightChild = x.leftChild;
x.leftChild = h;
return x;
}
class node{
public node parent;
public K data;
public node leftChild;
public node rightChild;
public node(K data){
this.data = data;
leftChild=null;
rightChild=null;
parent=null;
}
}
}