-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
199 lines (168 loc) · 5.51 KB
/
AVLTree.java
File metadata and controls
199 lines (168 loc) · 5.51 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
public class AVLTree extends BST {
public AVLTree(int[] initialValues) {
super(initialValues);
}
/**
* The return value must be pointed to be the original parent of the subroot argument
* This method should only be called on non-null nodes with a non-null left subtree (the subtree can be just a single node)
* @param subroot
* @return
*/
public Node rightRotate(Node subroot){
Node temp = new Node(-100);
boolean tempneeded = false;
Node nextSubroot = subroot.left;
if(nextSubroot.right != null){
temp = nextSubroot.right;
tempneeded = true;
}
nextSubroot.right = subroot;
if(tempneeded){
subroot.left = temp;
}
// Which heights should be updated and where?
nextSubroot.height = Math.max((nextSubroot.left!=null ? nextSubroot.left.height : 0),
(nextSubroot.right!=null ? nextSubroot.right.height : 0));
subroot.height = Math.max(subroot.left.height, (subroot.right!=null ? subroot.right.height : 0));
// TODO: Any other heights to manage?
// return value must be attached into to the rest of the structure
return nextSubroot;
}
/**
* The return value must be pointed to be the original parent of the subroot argument
* This method should only be called on non-null nodes with a non-null right subtree (the subtree can be just a single node)
* @param subroot
* @return
*/
public Node leftRotate(Node subroot){
Node temp = new Node(-100);
boolean tempneeded = false;
Node nextSubroot = subroot.right;
if(nextSubroot.left!=null){
temp = nextSubroot.left;
tempneeded = true;
}
nextSubroot.left = subroot;
if(tempneeded){
subroot.right = temp;
}
// return value must be attached into to the rest of the structure
return nextSubroot;
}
/**
* May cause a rebalancing of the AVLTree
* @param n
*/
public void insert(Node n){
root = insert(root, n);
}
public int loadBalance(Node n){
return (n.left!=null ? n.left.height : 0) - (n.right!=null ? n.right.height : 0);
}
/**
* Inserts from a particular starting origin node
*
* @param origin
* @param n
*/
private Node insert(Node origin, Node n){
if(origin.data > n.data){ // new value belongs in left subtree
if(origin.left==null){
origin.left = n;
} else {
//// INSERTION DOWN LEFT EDGE ~ Traverse Left Subtree ////
Node resultNode = insert(origin.left, n);
// since insertion may have caused a rebalance, we must link to the returned node
origin.left = resultNode;
// update height of origin node
origin.height = Math.max(resultNode.height, (origin.right!=null ? origin.right.height : 0));
int originLoadBalance = loadBalance(origin);
// Find the particular case, if too unbalanced.
if(originLoadBalance > 1){ // left-heavy
int leftLoadBalance = loadBalance(origin.left);
if(leftLoadBalance < 0){
// resultnode is right heavy
// *** LR case *** //
origin.left = leftRotate(resultNode);
origin = rightRotate(origin);
// TODO: Adjust heights??
} else {
// *** LL case *** //
origin = rightRotate(origin);
// TODO: aDjust heights?
}
} else if(originLoadBalance < -1){ // right-heavy
int rightLoadBalance = loadBalance(origin.right);
if(rightLoadBalance > 0){
// *** RL case *** //
origin.right = rightRotate(resultNode);
origin = leftRotate(origin);
// TODO: height fixes?
} else {
// *** RR case *** //
origin = leftRotate(origin);
// TODO: height fixes?
}
}
}
} else if(origin.data < n.data) { // new value belongs in right subtree
if(origin.right==null){
origin.right = n;
} else {
//// INSERTION DOWN RIGHT EDGE ~ Traverse Right Subtree ////
// Once a location is found for the
Node resultNode = insert(origin.right, n);
// since insertion may have caused a rebalance, we must link to the returned node
origin.right = resultNode;
// update height of origin node
origin.height = Math.max(resultNode.height, (origin.left!=null ? origin.left.height : 0));
int originLoadBalance = loadBalance(origin);
if(originLoadBalance > 1){ // left-heavy
int leftLoadBalance = loadBalance(origin.left);
if(leftLoadBalance < 0){
// resultnode is right heavy
// *** LR case *** //
origin.left = leftRotate(resultNode);
origin = rightRotate(origin);
// TODO: Adjust heights??
} else {
// *** LL case *** //
origin = rightRotate(origin);
// TODO: aDjust heights?
}
} else if(originLoadBalance < -1){ // right-heavy
int rightLoadBalance = loadBalance(origin.right);
if(rightLoadBalance > 0){
// *** RL case *** //
origin.right = rightRotate(resultNode);
origin = leftRotate(origin);
// TODO: height fixes?
} else {
// *** RR case *** //
origin = leftRotate(origin);
// TODO: height fixes?
}
}
}
} else {
// do nothing. value already exists in the BST.
}
return root;
}
public static void main(String[] args){
int[] input0 = new int[]{7,3,11,1,5,9,13,0,2,4,6,8,10,12,14};
AVLTree avlTree = new AVLTree(input0);
avlTree.printInOrder();
System.out.println("-------");
Node n = avlTree.findNode(3);
Node returnNode = avlTree.rightRotate(n);
avlTree.findNode(7).left = returnNode;
avlTree.printInOrder();
avlTree.displayBST();
Node n2 = avlTree.findNode(3);
avlTree.leftRotate(n2);
avlTree.displayBST();
avlTree.leftRotate(avlTree.root);
avlTree.displayBST();
}
}