-
Notifications
You must be signed in to change notification settings - Fork 106
/
AVLTree.java
241 lines (208 loc) · 7.39 KB
/
AVLTree.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.Scanner;
class Node {
int element;
int h;
Node leftChild;
Node rightChild;
public Node() {
leftChild = null;
rightChild = null;
element = 0;
h = 0;
}
public Node(int element) {
leftChild = null;
rightChild = null;
this.element = element;
h = 0;
}
}
class ConstructAVLTree {
private Node rootNode;
public ConstructAVLTree() {
rootNode = null;
}
public void removeAll() {
rootNode = null;
}
public boolean checkEmpty() {
if (rootNode == null)
return true;
else
return false;
}
public void insertElement(int element) {
rootNode = insertElement(element, rootNode);
}
private int getHeight(Node node) {
return node == null ? -1 : node.h;
}
private int getMaxHeight(int leftNodeHeight, int rightNodeHeight) {
return leftNodeHeight > rightNodeHeight ? leftNodeHeight : rightNodeHeight;
}
private Node insertElement(int element, Node node) {
if (node == null)
node = new Node(element);
else if (element < node.element) {
node.leftChild = insertElement(element, node.leftChild);
if (getHeight(node.leftChild) - getHeight(node.rightChild) == 2)
if (element < node.leftChild.element)
node = rotateWithLeftChild(node);
else
node = doubleWithLeftChild(node);
} else if (element > node.element) {
node.rightChild = insertElement(element, node.rightChild);
if (getHeight(node.rightChild) - getHeight(node.leftChild) == 2)
if (element > node.rightChild.element)
node = rotateWithRightChild(node);
else
node = doubleWithRightChild(node);
} else
;
node.h = getMaxHeight(getHeight(node.leftChild), getHeight(node.rightChild)) + 1;
return node;
}
private Node rotateWithLeftChild(Node node2) {
Node node1 = node2.leftChild;
node2.leftChild = node1.rightChild;
node1.rightChild = node2;
node2.h = getMaxHeight(getHeight(node2.leftChild), getHeight(node2.rightChild)) + 1;
node1.h = getMaxHeight(getHeight(node1.leftChild), node2.h) + 1;
return node1;
}
private Node rotateWithRightChild(Node node1) {
Node node2 = node1.rightChild;
node1.rightChild = node2.leftChild;
node2.leftChild = node1;
node1.h = getMaxHeight(getHeight(node1.leftChild), getHeight(node1.rightChild)) + 1;
node2.h = getMaxHeight(getHeight(node2.rightChild), node1.h) + 1;
return node2;
}
private Node doubleWithLeftChild(Node node3) {
node3.leftChild = rotateWithRightChild(node3.leftChild);
return rotateWithLeftChild(node3);
}
private Node doubleWithRightChild(Node node1) {
node1.rightChild = rotateWithLeftChild(node1.rightChild);
return rotateWithRightChild(node1);
}
public int getTotalNumberOfNodes() {
return getTotalNumberOfNodes(rootNode);
}
private int getTotalNumberOfNodes(Node head) {
if (head == null)
return 0;
else {
int length = 1;
length = length + getTotalNumberOfNodes(head.leftChild);
length = length + getTotalNumberOfNodes(head.rightChild);
return length;
}
}
public boolean searchElement(int element) {
return searchElement(rootNode, element);
}
private boolean searchElement(Node head, int element) {
boolean check = false;
while ((head != null) && !check) {
int headElement = head.element;
if (element < headElement)
head = head.leftChild;
else if (element > headElement)
head = head.rightChild;
else {
check = true;
break;
}
check = searchElement(head, element);
}
return check;
}
public void inorderTraversal() {
inorderTraversal(rootNode);
}
private void inorderTraversal(Node head) {
if (head != null) {
inorderTraversal(head.leftChild);
System.out.print(head.element + " ");
inorderTraversal(head.rightChild);
}
}
public void preorderTraversal() {
preorderTraversal(rootNode);
}
private void preorderTraversal(Node head) {
if (head != null) {
System.out.print(head.element + " ");
preorderTraversal(head.leftChild);
preorderTraversal(head.rightChild);
}
}
public void postorderTraversal() {
postorderTraversal(rootNode);
}
private void postorderTraversal(Node head) {
if (head != null) {
postorderTraversal(head.leftChild);
postorderTraversal(head.rightChild);
System.out.print(head.element + " ");
}
}
}
public class AVLTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ConstructAVLTree obj = new ConstructAVLTree();
char choice;
do {
System.out.println("\nSelect an operation:\n");
System.out.println("1. Insert a node");
System.out.println("2. Search a node");
System.out.println("3. Get total number of nodes in AVL Tree");
System.out.println("4. Is AVL Tree empty?");
System.out.println("5. Remove all nodes from AVL Tree");
System.out.println("6. Display AVL Tree in Post order");
System.out.println("7. Display AVL Tree in Pre order");
System.out.println("8. Display AVL Tree in In order");
// get choice from user
int ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println("Please enter an element to insert in AVL Tree");
obj.insertElement(sc.nextInt());
break;
case 2:
System.out.println("Enter integer element to search");
System.out.println(obj.searchElement(sc.nextInt()));
break;
case 3:
System.out.println(obj.getTotalNumberOfNodes());
break;
case 4:
System.out.println(obj.checkEmpty());
break;
case 5:
obj.removeAll();
System.out.println("\nTree Cleared successfully");
break;
case 6:
System.out.println("\nDisplay AVL Tree in Post order");
obj.postorderTraversal();
break;
case 7:
System.out.println("\nDisplay AVL Tree in Pre order");
obj.preorderTraversal();
break;
case 8:
System.out.println("\nDisplay AVL Tree in In order");
obj.inorderTraversal();
break;
default:
System.out.println("\n ");
break;
}
System.out.println("\nPress 'y' or 'Y' to continue \n");
choice = sc.next().charAt(0);
} while (choice == 'Y' || choice == 'y');
}
}