-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAVLtree.java
More file actions
153 lines (124 loc) · 2.71 KB
/
AVLtree.java
File metadata and controls
153 lines (124 loc) · 2.71 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
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
import java.util.*;
class AVL{
class Node{
int data;
int height;
Node left;
Node right;
Node(int d){
data = d;
left = null;
right = null;
}
}
static Node root;
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
AVL obj = new AVL();
int res,v;
System.out.println("1.Insert.");
System.out.println("2.Display.");
System.out.println("3.Exit.");
System.out.println("4.Height.");
System.out.println("5.Preorder Display.");
while(true){
res = ip.nextInt();
switch(res){
case 1:
System.out.print("Enter data : ");
root = obj.insert(root,v=ip.nextInt());
break;
case 2:
obj.Inorder(root);
break;
case 3:
break;
case 4:
System.out.println(obj.height(root));
break;
case 5:
obj.Preorder(root);
break;
default:
System.out.println("Enter a valid option!");
}
if(res==3)
break;
}
}
public Node insert(Node node,int d){
if(node==null)
node = new Node(d);
else if(node.data>d)
node.left = insert(node.left,d);
else if(node.data<d)
node.right = insert(node.right,d);
else
return node;
node.height = 1 + max(height(node.left),height(node.right));
int bal = balanceFactor(node);
if(bal>1 && d<node.left.data)
return RightRotate(node);
if(bal<-1 && d>node.right.data)
return LeftRotate(node);
if(bal>1 && d>node.left.data){
node.left = LeftRotate(node.left);
return RightRotate(node);
}
if(bal<-1 && d<node.right.data){
node.right = RightRotate(node.right);
return LeftRotate(node);
}
return node;
}
public static Node RightRotate(Node node){
Node x = node.left;
Node y = x.right;
x.right = node;
node.left = y;
node = x;
x.height = max(height(node.left),height(node.right))+1;
y.height = max(height(node.left),height(node.right))+1;
return x;
}
public static Node LeftRotate(Node node){
Node x = node.right;
Node y = x.left;
x.left = node;
node.right = y;
node = x;
x.height = max(height(node.left),height(node.right))+1;
y.height = max(height(node.left),height(node.right))+1;
return x;
}
public static int max(int a,int b){
int q = (a<b)?b:a;
return q;
}
public static int height(Node node){
if(node==null)
return 0;
else
return node.height;
}
public static int balanceFactor(Node node){
if(node==null)
return 0;
else
return height(node.left) - height(node.right);
}
public static void Inorder(Node node){
if(node!=null){
Inorder(node.left);
System.out.print(node.data + " ");
Inorder(node.right);
}
}
public static void Preorder(Node node){
if(node!=null){
System.out.print(node.data + " ");
Preorder(node.left);
Preorder(node.right);
}
}
}