This repository was archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
221 lines (197 loc) · 6.11 KB
/
AVLTree.java
File metadata and controls
221 lines (197 loc) · 6.11 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* This is an implementation of an AVLTree based on the Tree class.
* The balancing of this class has been partially built on the strategy used on the following webpage: https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
* The AVLTree balances a Tree which makes searching the structure much more efficient.
* The balancing is heavier than the balancing of a RedBlackTree but the balancing results in a Tree
* which is as balanced as possible. This is ideal for static Trees which are not updated frequently
* but searched a lot.
* Author: Seppe Lampe
*/
public class AVLTree extends Tree{
public class AVLTreeNode extends Tree.TreeNode{
private AVLTreeNode parentNode;
private int height;
public AVLTreeNode(Comparable v) {
super(v);
height = 1;
parentNode = null;
}
public AVLTreeNode(Comparable v, AVLTreeNode left, AVLTreeNode right, AVLTreeNode parent) {
super(v, left, right);
parentNode = parent;
this.height = 1;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public AVLTreeNode getLeftNode() {
return (AVLTreeNode) leftNode;
}
public void setLeftNode(AVLTreeNode n) {
leftNode = n;
if (n!= null) {
n.setParentNode(this);
}
}
public AVLTreeNode getRightNode() {
return (AVLTreeNode) rightNode;
}
public void setRightNode(AVLTreeNode n) {
rightNode = n;
if (n!= null) {
n.setParentNode(this);
}
}
public AVLTreeNode getParentNode() {
return parentNode;
}
public void setParentNode(AVLTreeNode parentNode) {
this.parentNode = parentNode;
}
@Override
public int compareTo(Object o) { // O(1)
return value.compareTo(((AVLTreeNode) o).value);
}
}
public AVLTree() {
}
public void insert(Comparable element) { // O(log(n))
insertAtNode(element, (AVLTreeNode) root, null);
}
private void insertAtNode(Comparable element, AVLTreeNode current, AVLTreeNode parent) { // O(log(n))
if (current == null) { // if the node we check is empty
AVLTreeNode newNode = new AVLTreeNode(element);
if (parent != null) { // the current node is empty, but we have a parent
if (element.compareTo(parent.value) < 0) { // do we add it to the left?
parent.setLeftNode(newNode);
}
else {
parent.setRightNode(newNode); // or do we add it to the right?
}
balance(parent);
}
else { // the current node is empty and it has no parent, we actually have an empty tree
root = newNode;
}
count += 1;
}
else if (element.compareTo(current.value) == 0) {
System.out.println("Element is already in tree."); // if the element is already in the tree, what to do?
}
else if (element.compareTo(current.value) < 0) { // if the element is smaller than current, go left
insertAtNode(element, current.getLeftNode(), current);
}
else // if the element is bigger than current, go right
insertAtNode(element, current.getRightNode(), current);
}
private void splayLeft(AVLTreeNode n) { // O(1)
/* p p
/ /
n r
/ \ --> / \
l r n rr
/ \ / \
rl rr l rl
*/
AVLTreeNode rightTree = n.getRightNode();
AVLTreeNode parent = n.getParentNode();
n.setRightNode(rightTree.getLeftNode()); // The original LeftTree of the RightTree of n becomes the new RightTree of n
rightTree.setLeftNode(n); // n becomes the LeftTree of r (its RightTree)
if(n == super.root) {
super.root = rightTree;
}
else {
if(parent.getLeftNode() == n) { // The RightTree now comes at the place where n used to be
parent.setLeftNode(rightTree);
}
else { // The RightTree now comes at the place where n used to be
parent.setRightNode(rightTree);
}
}
correctHeight(n);
correctHeight(rightTree);
}
private void splayRight(AVLTreeNode n) { // Similar to splayLeft but mirrored
/* p p
/ /
n l
/ \ --> / \
l r ll n
/ \ / \
ll lr lr r
*/
AVLTreeNode leftTree = n.getLeftNode();
AVLTreeNode parent = n.getParentNode();
n.setLeftNode(leftTree.getRightNode());
leftTree.setRightNode(n);
if(n == root) {
root = leftTree;
}
else {
if(parent.getRightNode() == n) {
parent.setRightNode(leftTree);
}
else {
parent.setLeftNode(leftTree);
}
}
correctHeight(n);
correctHeight(leftTree);
}
/* This method was based on the webpage mentioned in the beginning of the document.
However it has been written to function with pointers to the parentNode as I found this resulted
in more understandable/readable code. */
private void balance(AVLTreeNode n) {
AVLTreeNode parent = n.getParentNode();
correctHeight(n);
correctHeight(parent);
int balance = getBalance(parent);
if (balance > 1) {
if (n == parent.getLeftNode()) {
splayRight(parent);
}
else {
splayLeft(parent.getLeftNode());
splayRight(parent);
}
}
else if (balance < -1) {
if (n == parent.getRightNode()) {
splayLeft(parent);
}
else {
splayRight(parent.getRightNode());
splayLeft(parent);
}
}
if(n != root && parent != root) {
balance(parent);
}
}
// These are some helper functions for the balancing
private int max(int one, int two) {
if (one > two) {
return one;
}
return two;
}
private int height(AVLTreeNode n) {
if (n == null) {
return 0;
}
return n.getHeight();
}
private void correctHeight(AVLTreeNode n) {
if (n != null) {
n.setHeight(max(height(n.getLeftNode()), height(n.getRightNode())) + 1);
}
}
private int getBalance(AVLTreeNode n) {
if (n == null) {
return 0;
}
return height(n.getLeftNode()) - height(n.getRightNode());
}
}