forked from Bopitiya77/Hack22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl tree.java
More file actions
303 lines (275 loc) · 10.8 KB
/
avl tree.java
File metadata and controls
303 lines (275 loc) · 10.8 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//import classes and packages
import java.util.Scanner;
// create Node class to design the structure of the AVL Tree Node
class Node
{
int element;
int h; //for height
Node leftChild;
Node rightChild;
//default constructor to create null node
public Node()
{
leftChild = null;
rightChild = null;
element = 0;
h = 0;
}
// parameterized constructor
public Node(int element)
{
leftChild = null;
rightChild = null;
this.element = element;
h = 0;
}
}
// create class ConstructAVLTree for constructing AVL Tree
class ConstructAVLTree
{
private Node rootNode;
//Constructor to set null value to the rootNode
public ConstructAVLTree()
{
rootNode = null;
}
//create removeAll() method to make AVL Tree empty
public void removeAll()
{
rootNode = null;
}
// create checkEmpty() method to check whether the AVL Tree is empty or not
public boolean checkEmpty()
{
if(rootNode == null)
return true;
else
return false;
}
// create insertElement() to insert element to to the AVL Tree
public void insertElement(int element)
{
rootNode = insertElement(element, rootNode);
}
//create getHeight() method to get the height of the AVL Tree
private int getHeight(Node node )
{
return node == null ? -1 : node.h;
}
//create maxNode() method to get the maximum height from left and right node
private int getMaxHeight(int leftNodeHeight, int rightNodeHeight)
{
return leftNodeHeight > rightNodeHeight ? leftNodeHeight : rightNodeHeight;
}
//create insertElement() method to insert data in the AVL Tree recursively
private Node insertElement(int element, Node node)
{
//check whether the node is null or not
if (node == null)
node = new Node(element);
//insert a node in case when the given element is lesser than the element of the root node
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
; // if the element is already present in the tree, we will do nothing
node.h = getMaxHeight( getHeight( node.leftChild ), getHeight( node.rightChild ) ) + 1;
return node;
}
// creating rotateWithLeftChild() method to perform rotation of binary tree node with left child
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;
}
// creating rotateWithRightChild() method to perform rotation of binary tree node with right child
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;
}
//create doubleWithLeftChild() method to perform double rotation of binary tree node. This method first rotate the left child with its right child, and after that, node3 with the new left child
private Node doubleWithLeftChild(Node node3)
{
node3.leftChild = rotateWithRightChild( node3.leftChild );
return rotateWithLeftChild( node3 );
}
//create doubleWithRightChild() method to perform double rotation of binary tree node. This method first rotate the right child with its left child and after that node1 with the new right child
private Node doubleWithRightChild(Node node1)
{
node1.rightChild = rotateWithLeftChild( node1.rightChild );
return rotateWithRightChild( node1 );
}
//create getTotalNumberOfNodes() method to get total number of nodes in the AVL Tree
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;
}
}
//create searchElement() method to find an element in the AVL Tree
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;
}
// create inorderTraversal() method for traversing AVL Tree in in-order form
public void inorderTraversal()
{
inorderTraversal(rootNode);
}
private void inorderTraversal(Node head)
{
if (head != null)
{
inorderTraversal(head.leftChild);
System.out.print(head.element+" ");
inorderTraversal(head.rightChild);
}
}
// create preorderTraversal() method for traversing AVL Tree in pre-order form
public void preorderTraversal()
{
preorderTraversal(rootNode);
}
private void preorderTraversal(Node head)
{
if (head != null)
{
System.out.print(head.element+" ");
preorderTraversal(head.leftChild);
preorderTraversal(head.rightChild);
}
}
// create postorderTraversal() method for traversing AVL Tree in post-order form
public void postorderTraversal()
{
postorderTraversal(rootNode);
}
private void postorderTraversal(Node head)
{
if (head != null)
{
postorderTraversal(head.leftChild);
postorderTraversal(head.rightChild);
System.out.print(head.element+" ");
}
}
}
// create AVLTree class to construct AVL Tree
public class AVLTreeExample
{
//main() method starts
public static void main(String[] args)
{
//creating Scanner class object to get input from user
Scanner sc = new Scanner(System.in);
// create object of ConstructAVLTree class object for costructing AVL Tree
ConstructAVLTree obj = new ConstructAVLTree();
char choice; //initialize a character type variable to choice
// perform operation of AVL Tree using switch
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');
}
}