forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTreeJava.java
More file actions
Latest commit
192 lines (174 loc) · 4.95 KB
/
Copy pathAVLTreeJava.java
File metadata and controls
192 lines (174 loc) · 4.95 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
// AVL tree implementation in Java
// Create node
classNode {
intitem, height;
Nodeleft, right;
Node(intd) {
item = d;
height = 1;
}
}
// Tree class
classAVLTreeJava {
Noderoot;
intheight(NodeN) {
if (N == null)
return0;
returnN.height;
}
intmax(inta, intb) {
return (a > b) ? a : b;
}
NoderightRotate(Nodey) {
Nodex = y.left;
NodeT2 = x.right;
x.right = y;
y.left = T2;
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
returnx;
}
NodeleftRotate(Nodex) {
Nodey = x.right;
NodeT2 = y.left;
y.left = x;
x.right = T2;
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
returny;
}
// Get balance factor of a node
intgetBalanceFactor(NodeN) {
if (N == null)
return0;
returnheight(N.left) - height(N.right);
}
// Insert a node
NodeinsertNode(Nodenode, intitem) {
// Find the position and insert the node
if (node == null)
return (newNode(item));
if (item < node.item)
node.left = insertNode(node.left, item);
elseif (item > node.item)
node.right = insertNode(node.right, item);
else
returnnode;
// Update the balance factor of each node
// And, balance the tree
node.height = 1 + max(height(node.left), height(node.right));
intbalanceFactor = getBalanceFactor(node);
if (balanceFactor > 1) {
if (item < node.left.item) {
returnrightRotate(node);
} elseif (item > node.left.item) {
node.left = leftRotate(node.left);
returnrightRotate(node);
}
}
if (balanceFactor < -1) {
if (item > node.right.item) {
returnleftRotate(node);
} elseif (item < node.right.item) {
node.right = rightRotate(node.right);
returnleftRotate(node);
}
}
returnnode;
}
NodenodeWithMimumValue(Nodenode) {
Nodecurrent = node;
while (current.left != null)
current = current.left;
returncurrent;
}
// Delete a node
NodedeleteNode(Noderoot, intitem) {
// Find the node to be deleted and remove it
if (root == null)
returnroot;
if (item < root.item)
root.left = deleteNode(root.left, item);
elseif (item > root.item)
root.right = deleteNode(root.right, item);
else {
if ((root.left == null) || (root.right == null)) {
Nodetemp = null;
if (temp == root.left)
temp = root.right;
else
temp = root.left;
if (temp == null) {
temp = root;
root = null;
} else
root = temp;
} else {
Nodetemp = nodeWithMimumValue(root.right);
root.item = temp.item;
root.right = deleteNode(root.right, temp.item);
}
}
if (root == null)
returnroot;
// Update the balance factor of each node and balance the tree
root.height = max(height(root.left), height(root.right)) + 1;
intbalanceFactor = getBalanceFactor(root);
if (balanceFactor > 1) {
if (getBalanceFactor(root.left) >= 0) {
returnrightRotate(root);
} else {
root.left = leftRotate(root.left);
returnrightRotate(root);
}
}
if (balanceFactor < -1) {
if (getBalanceFactor(root.right) <= 0) {
returnleftRotate(root);
} else {
root.right = rightRotate(root.right);
returnleftRotate(root);
}
}
returnroot;
}
voidpreOrder(Nodenode) {
if (node != null) {
System.out.print(node.item + " ");
preOrder(node.left);
preOrder(node.right);
}
}
// Print the tree
privatevoidprintTree(NodecurrPtr, Stringindent, booleanlast) {
if (currPtr != null) {
System.out.print(indent);
if (last) {
System.out.print("R----");
indent += " ";
} else {
System.out.print("L----");
indent += "| ";
}
System.out.println(currPtr.item);
printTree(currPtr.left, indent, false);
printTree(currPtr.right, indent, true);
}
}
// Driver code
publicstaticvoidmain(String[] args) {
AVLTreetree = newAVLTree();
tree.root = tree.insertNode(tree.root, 33);
tree.root = tree.insertNode(tree.root, 13);
tree.root = tree.insertNode(tree.root, 53);
tree.root = tree.insertNode(tree.root, 9);
tree.root = tree.insertNode(tree.root, 21);
tree.root = tree.insertNode(tree.root, 61);
tree.root = tree.insertNode(tree.root, 8);
tree.root = tree.insertNode(tree.root, 11);
tree.printTree(tree.root, "", true);
tree.root = tree.deleteNode(tree.root, 13);
System.out.println("After Deletion: ");
tree.printTree(tree.root, "", true);
}
}