-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSelf Balancing Tree(AVL Tree)
179 lines (167 loc) · 3.79 KB
/
Self Balancing Tree(AVL Tree)
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
import java.util.Scanner;
public class AVLTree{
/**
*
* @author animesh
*
*/
// Node declaration
static class Node
{
int val; //Value
int ht; //Height
Node left; //Left child
Node right; //Right child
}
/**
* Function to find height of tree
* @param root node
* @return Integer height of tree
*/
public static int heightMax(Node root)
{
int maxHeight = -1;
if(root.left != null)
maxHeight = Math.max(maxHeight, root.left.ht);
if(root.right != null)
maxHeight = Math.max(maxHeight, root.right.ht);
return maxHeight;
}
/**
* Function to insert a number in AVL tree height of leaf nodes is considered as 0
* @param root node of tree
* @param val to be inserted in the tree
* @return the new root node of the tree after balancing it
*/
public static Node insert(Node root, int val)
{
if(root == null)
{
Node node = new Node();
node.val = val;
node.left = node.right = null;
node.ht = 0;
return node;
}
if(val < root.val)
root.left = insert(root.left, val);
else
root.right = insert(root.right, val);
int balanceFactor = balanceFactor(root);
if(balanceFactor < -1)
{
if(balanceFactor(root.right) > 0)
{
root.right = rotateRight(root.right);
return rotateLeft(root);
}
else
return rotateLeft(root);
}
else
{
if(balanceFactor > 1)
{
if(balanceFactor(root.left) < 0)
{
root.left = rotateLeft(root.left);
return rotateRight(root);
}
else
return rotateRight(root);
}
}
root.ht = heightMax(root) + 1;
return root;
}
/**
* Function to right rotate the tree corresponding to root node
* @param x node at which right rotation is needed
* @return the new root of the tree
*/
public static Node rotateRight(Node x)
{
Node y = x.left;
x.left = y.right;
y.right = x;
// height
x.ht = heightMax(x)+1;
y.ht = heightMax(y)+1;
return y;
}
/**
* Function to left rotate the tree corresponding to root node
* @param x node at which left rotation is needed
* @return the new root of the tree
*/
public static Node rotateLeft(Node x)
{
Node y = x.right;
x.right = y.left;
y.left = x;
// height
x.ht = heightMax(x)+1;
y.ht = heightMax(y)+1;
return y;
}
/**
* Function to find balance factor of root
* @param root node at which balance factor is calcualted
* @return Integer balance factor of root node
*/
public static int balanceFactor(Node root)
{
int left = -1;
int right = -1;
if(root.left != null)
left = root.left.ht;
if(root.right != null)
right = root.right.ht;
return (left - right);
}
/**
* Main driver class to take input from System.in
* First Line will indicate the number of insertion
* From second line give one value per line for insertion i nthe tree
* @param args
*/
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
Node root = null;
for (int i = 0; i < n; i++)
{
int val = Integer.parseInt(in.nextLine());
root = insert(root, val);
}
System.out.println();
inorder(root);
System.out.println();
preorder(root);
}
/**
* Function to print preOrder of the tree with balance factor
* @param node root
*/
public static void preorder(Node node)
{
if(node == null)
return;
System.out.print(node.val+"("+balanceFactor(node)+") ");
preorder(node.left);
preorder(node.right);
}
/**
* Function to print inOrder of the tree with balance factor
* @param node root
*/
public static void inorder(Node node)
{
if(node == null)
return;
inorder(node.left);
System.out.print(node.val+"("+balanceFactor(node)+") ");
inorder(node.right);
}
}