-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.java
105 lines (88 loc) · 2.77 KB
/
AVL.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
Class Node{
int key, height;
Node left, right;
Node(int d) {
key = d;
height = 1;
}
}
class AVL{
Node root;
int height(Node N) {
if (N == null)
return 0;
return N.height;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
Node right_Rotate(Node y) {
Node x = y.left;
Node T2 = 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;
return x;
}
Node left_Rotate(Node x) {
Node y = x.right;
Node T2 = 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;
return y;
}
int get_Balance(Node N) {
if (N == null)
return 0;
return height(N.left) - height(N.right);
}
Node insert(Node node, int key) {
if (node == null)
return (new Node(key));
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else
return node;
node.height = 1 + max(height(node.left), height(node.right));
int balance = get_Balance(node);
if (balance > 1 && key < node.left.key)
return right_Rotate(node);
if (balance < -1 && key > node.right.key)
return left_Rotate(node);
if (balance > 1 && key > node.left.key) {
node.left = left_Rotate(node.left);
return right_Rotate(node);
}
if (balance < -1 && key < node.right.key) {
node.right = right_Rotate(node.right);
return left_Rotate(node);
}
return node;
}
void pre_Order(Node node) {
if (node != null) {
System.out.print(node.key + " ");
pre_Order(node.left);
pre_Order(node.right);
}
}
public static void main(String[] args) {
AVL avl_tree = new AVL();
avl_tree.root = avl_tree.insert(avl_tree.root, 3);
avl_tree.root = avl_tree.insert(avl_tree.root, 2);
avl_tree.root = avl_tree.insert(avl_tree.root, 1);
avl_tree.root = avl_tree.insert(avl_tree.root, 4);
avl_tree.root = avl_tree.insert(avl_tree.root, 5);
avl_tree.root = avl_tree.insert(avl_tree.root, 6);
avl_tree.root = avl_tree.insert(avl_tree.root, 7);
avl_tree.root = avl_tree.insert(avl_tree.root, 8);
avl_tree.root = avl_tree.insert(avl_tree.root, 9);
System.out.println("The AVL Tree is : ");
avl_tree.pre_Order(avl_tree.root);
}
}