forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.java
122 lines (118 loc) · 3.31 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
package BinaryTrees;
public class AVL {
int count = 5;
public class Node{
private int val;
private int height;
Node left;
Node right;
public Node(int val){
this.val = val;
}
public int getVal(){
return val;
}
}
public Node root;
public int height(Node node){
if(node==null){
return -1;
}
return node.height;
}
public boolean isEmpty(){
return root==null;
}
public void insert(int val){
root = insert(root,val);
}
public Node insert(Node node,int val){
if(node==null){
node = new Node(val);
return node;
}
if(node.val>val){
node.left = insert(node.left,val);
}
if(node.val<val) {
node.right = insert(node.right, val);
}
node.height = Math.max(height(node.left),height(node.right))+1;
return rotate(node);
}
private Node rotate(Node node){
if(height(node.left)-height(node.right)>1){
//left heavy
if(height(node.left.left)-height(node.left.right)>0){
return rightRotate(node);
}
if(height(node.left.left)-height(node.left.right)<0){
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
if((height(node.left) - height(node.right)) < -1){
//right heavy
if(height(node.right.left)-height(node.right.right)<0){
return leftRotate(node);
}
if(height(node.right.left)-height(node.right.right)>0){
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
return node;
}
public Node rightRotate(Node p){
Node c = p.left;
Node t = c.right;
c.right = p;
p.left = t;
p.height = Math.max(height(p.left),height(p.right))+1;
c.height = Math.max(height(c.left),height(c.right))+1;
return c;
}
public Node leftRotate(Node p){
Node c = p.right;
Node t = c.left;
c.left = p;
p.right = t;
p.height = Math.max(height(p.left),height(p.right))+1;
c.height = Math.max(height(c.left),height(c.right))+1;
return c;
}
public void display(){
display2D(root,0);
}
public void display2D(Node root, int space){
if(root==null){
return;
}
space+=count;
display2D(root.right,space);
System.out.println();
for(int i=count;i<space;i++){
System.out.print(" ");
}
System.out.println(root.val);
display2D(root.left,space);
}
public void populateSorted(int[] nums){
populateSorted(nums,0,nums.length);
}
public void populateSorted(int[] nums,int s,int e){
if(s>=e){
return ;
}
int mid = (s+e)/2;
this.insert(nums[mid]);
populateSorted(nums,s,mid);
populateSorted(nums,mid+1,e);
}
public boolean isBalanced(Node node){
if(node==null){
return true;
}
return Math.abs(height(node.left)-height(node.right))<=1 && isBalanced(node.left) && isBalanced(node.right);
}
}