-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathAVLSimple.java
146 lines (122 loc) · 3.45 KB
/
AVLSimple.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
package com.thealgorithms.datastructures.trees;
/*
* Avl is algo that balance itself while adding new alues to tree
* by rotating branches of binary tree and make itself Binary seaarch tree
* there are four cases which has to tackle
* rotating - left right ,left left,right right,right left
Test Case:
AVLTree tree=new AVLTree();
tree.insert(20);
tree.insert(25);
tree.insert(30);
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(27);
tree.insert(19);
tree.insert(16);
tree.display();
*/
public class AVLSimple {
private class Node {
int data;
int height;
Node left;
Node right;
Node(int data) {
this.data = data;
this.height = 1;
}
}
private Node root;
public void insert(int data) {
this.root = insert(this.root, data);
}
private Node insert(Node node, int item) {
if (node == null) {
return new Node(item);
}
if (node.data > item) {
node.left = insert(node.left, item);
}
if (node.data < item) {
node.right = insert(node.right, item);
}
node.height = Math.max(height(node.left), height(node.right)) + 1;
int bf = bf(node);
// LL case
if (bf > 1 && item < node.left.data) {
return rightRotate(node);
}
// RR case
if (bf < -1 && item > node.right.data) {
return leftRotate(node);
}
// RL case
if (bf < -1 && item < node.right.data) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
// LR case
if (bf > 1 && item > node.left.data) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
return node;
}
public void display() {
this.display(this.root);
System.out.println(this.root.height);
}
private void display(Node node) {
String str = "";
if (node.left != null) {
str += node.left.data + "=>";
} else {
str += "END=>";
}
str += node.data + "";
if (node.right != null) {
str += "<=" + node.right.data;
} else {
str += "<=END";
}
System.out.println(str);
if (node.left != null) {
display(node.left);
}
if (node.right != null) {
display(node.right);
}
}
private int height(Node node) {
if (node == null) {
return 0;
}
return node.height;
}
private int bf(Node node) {
if (node == null) {
return 0;
}
return height(node.left) - height(node.right);
}
private Node rightRotate(Node c) {
Node b = c.left;
Node t3 = b.right;
b.right = c;
c.left = t3;
c.height = Math.max(height(c.left), height(c.right)) + 1;
b.height = Math.max(height(b.left), height(b.right)) + 1;
return b;
}
private Node leftRotate(Node c) {
Node b = c.right;
Node t3 = b.left;
b.left = c;
c.right = t3;
c.height = Math.max(height(c.left), height(c.right)) + 1;
b.height = Math.max(height(b.left), height(b.right)) + 1;
return b;
}
}