-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeletionInAVLTree.java
More file actions
190 lines (154 loc) · 5.34 KB
/
Copy pathDeletionInAVLTree.java
File metadata and controls
190 lines (154 loc) · 5.34 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
class Node {
int key, height;
Node left, right;
public Node(int key) {
this.key = key;
height = 1;
left = right = null;
}
}
public class DeletionInAVLTree {
Node root;
int height(Node root) {
if (root == null) {
return 0;
}
return root.height;
}
int max(int a, int b) {
return (a > b)? a : b;
}
// method to right rotate subtree rooted with y
Node rightRotate(Node y) {
Node x = y.left;
Node t2 = x.right;
// perform rotation
x.right = y;
y.left = t2;
// update heights
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
return x; // return new root
}
// method to right rotate subtree rooted with x
Node leftRotate(Node x) {
Node y = x.right;
Node t2 = y.left;
// perform rotation
y.left = x;
x.right = t2;
// update heights
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
return y; // return new root
}
int getBalance(Node root) {
if (root == null) {
return 0;
}
return height(root.left) - height(root.right);
}
Node insert(Node root, int key) {
if (root == null) {
return new Node(key);
}
if (key < root.key) {
root.left = insert(root.left, key);
} else if (key > root.key) {
root.right = insert(root.right, key);
} else {
return root;
}
root.height = 1 + max(height(root.left), height(root.right));
int balance = getBalance(root);
if (balance > 1 && key < root.left.key) {
return rightRotate(root);
}
if (balance < -1 && key > root.right.key) {
return leftRotate(root);
}
if (balance > 1 && key > root.left.key) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && key < root.right.key) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
void printInOrder(Node root) {
if (root == null) {
return;
}
printInOrder(root.left);
System.out.print(root.key + " ");
printInOrder(root.right);
}
// This method performs deletion in AVL Tree. First it deletes the node then balances the tree.
public Node delete(Node root, int key) {
// Base case: if tree is empty
if (root == null) {
return root;
}
// Otherwise recur down the tree
if (key < root.key) {
root.left = delete(root.left, key);
} else if (key > root.key) {
root.right = delete(root.right, key);
// if key is same as root's key, then this is the root to be deleted
} else {
// root with only one child or no child
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
// root with two children: Get the inorder successor (smallest in the right subtree)
root.key = minValue(root.right);
// delete the inorder successor
root.right = delete(root.right, root.key);
root.height = 1 + max(height(root.left), height(root.right));
int balance = getBalance(root);
if (balance > 1 && key < root.left.key) {
return rightRotate(root);
}
if (balance < -1 && key > root.right.key) {
return leftRotate(root);
}
if (balance > 1 && key > root.left.key) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && key < root.right.key) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
}
return root;
}
private int minValue(Node root) {
int min = root.key;
// We are traversing in left becasue in BST, left contain smaller values
while (root.left != null) {
min = root.left.key;
root = root.left;
}
return min;
}
public static void main(String[] args) {
DeletionInAVLTree tree = new DeletionInAVLTree();
tree.root = tree.insert(tree.root, 10);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 30);
tree.root = tree.insert(tree.root, 40);
tree.root = tree.insert(tree.root, 50);
tree.root = tree.insert(tree.root, 25);
System.out.println("InOrder traversal of constructed tree: ");
tree.printInOrder(tree.root);
Node deletedNode = tree.delete(tree.root, 20);
System.out.println(deletedNode.key + " is deleted");
System.out.println("InOrder traversal of constructed tree after deletion: ");
tree.printInOrder(tree.root);
}
}