-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL_Trees.java
More file actions
126 lines (99 loc) · 2.97 KB
/
Copy pathAVL_Trees.java
File metadata and controls
126 lines (99 loc) · 2.97 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
import java.util.ArrayList;
public class AVL_Trees {
static class Node {
int data , height;
Node left , right;
Node (int data){
this.data = data;
this.height = 1;
}
}
public static Node root;
public static int height(Node root){
if (root == null){
return 0;
}
return root.height;
}
public static int getBalanceFactor(Node root){
if (root == null){
return 0;
}
return height(root.left) - height(root.right);
}
public static Node leftRotate(Node x){
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
// Return new root
return y;
}
public static Node rightRotate(Node y){
Node x = y.left;
Node T2 = x.right;
// Perform rotation
x.right = y;
y.left = T2;
// Update Height
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
// Return new root
return x;
}
public static Node insert(Node root , int val){
if (root == null){
return new Node(val);
}
if (root.data > val){
root.left = insert(root.left, val);
}else if (root.data < val){
root.right = insert(root.right, val);
}else{
return root;
}
// Update the height
root.height = Math.max(height(root.left), height(root.right));
int bf = getBalanceFactor(root);
// left left case
if (bf > 1 && val < root.left.data){
return rightRotate(root);
}
// Right right Case
if (bf < -1 && val > root.right.data){
return leftRotate(root);
}
// Left right case
if (bf > 1 && val > root.left.data){
root.left = leftRotate(root.left);
return rightRotate(root);
}
// Right left case
if (bf < -1 && val < root.right.data){
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
public static void preOrder(Node root){
if (root == null){
return;
}
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
public static void main(String[] args) {
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
preOrder(root);
}
}