-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.java
178 lines (159 loc) · 4.07 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.util.*;
class AVLNode
{
public AVLNode left, right;
public Position data;
public int height;
public int value;
public AVLNode(Position pos)
{
left = null;
right = null;
data = pos;
value=pos.phraseIndex;
height = 0;
}
}
public class AVLTree
{
public AVLNode root;
Vector<Integer> inorder_val = new Vector<Integer>();
Boolean flag = false;
public AVLTree()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public int height(AVLNode t )
{
if(t==null) return (-1);
else return t.height;
}
public int max(int lhs, int rhs)
{
if(lhs>rhs) return lhs;
else return rhs;
}
public void insert(Position pos)
{
root = insert(pos, root);
}
private AVLNode insert(Position pos, AVLNode t)
{
if (t == null)
t = new AVLNode(pos);
else if (pos.phraseIndex < t.value)
{
t.left = insert( pos, t.left );
if( height( t.left ) - height( t.right ) == 2 )
if( pos.phraseIndex < t.left.value )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( pos.phraseIndex > t.value )
{
t.right = insert( pos, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( pos.phraseIndex > t.right.value)
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else;
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
private AVLNode rotateWithLeftChild(AVLNode k2)
{
AVLNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
private AVLNode rotateWithRightChild(AVLNode k1)
{
AVLNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = max( height( k2.right ), k1.height ) + 1;
return k2;
}
private AVLNode doubleWithLeftChild(AVLNode k3)
{
k3.left = rotateWithRightChild( k3.left );
return rotateWithLeftChild( k3 );
}
private AVLNode doubleWithRightChild(AVLNode k1)
{
k1.right = rotateWithLeftChild( k1.right );
return rotateWithRightChild( k1 );
}
public int countNodes()
{
return countNodes(root);
}
public int countNodes(AVLNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.left);
l += countNodes(r.right);
return l;
}
}
public boolean search(int val)
{
return search(root, val);
}
public boolean search(AVLNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.value;
if (val < rval)
r = r.left;
else if (val > rval)
r = r.right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public Vector<Integer> inorder()
{
if(flag==false)
{
inorder(root);
flag=true;
return inorder_val;
}
else
{
return inorder_val;
}
}
public void inorder(AVLNode r)
{
if (r != null)
{
inorder(r.left);
inorder_val.add(r.value);
inorder(r.right);
}
}
}