0% found this document useful (0 votes)
53 views

AVL Tree Implementation

The document describes an implementation of an AVL tree data structure in C++. It includes functions to insert nodes, remove nodes, rotate nodes to rebalance the tree, find the minimum/maximum nodes, and calculate the height and balance factor of nodes. The tree uses rotations to maintain an AVL tree property where the heights of the left and right subtrees differ by at most 1 during insertions and deletions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

AVL Tree Implementation

The document describes an implementation of an AVL tree data structure in C++. It includes functions to insert nodes, remove nodes, rotate nodes to rebalance the tree, find the minimum/maximum nodes, and calculate the height and balance factor of nodes. The tree uses rotations to maintain an AVL tree property where the heights of the left and right subtrees differ by at most 1 during insertions and deletions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

/* AVL Tree

Implementation
in C++ */
/* Harish R */

#include<iostream>
using namespace std;
class BST
{
struct node
{
int data;
node* left;
node* right;
int height;
};
node* root;
void makeEmpty(node* t)
{
if(t == NULL)
return;
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
node* insert(int x, node* t)
{
if(t == NULL)
{
t = new node;
t->data = x;
t->height = 0;
t->left = t->right = NULL;
}
else if(x < t->data)
{
t->left = insert(x, t->left);
if(height(t->left) - height(t->right) == 2)
{
if(x < t->left->data)
t = singleRightRotate(t);
else
t = doubleRightRotate(t);
}
}
else if(x > t->data)

1
{
t->right = insert(x, t->right);
if(height(t->right) - height(t->left) == 2)
{
if(x > t->right->data)
t = singleLeftRotate(t);
else
t = doubleLeftRotate(t);
}
}
t->height = max(height(t->left), height(t->right))+1;
return t;
}
node* singleRightRotate(node* &t)
{
node* u = t->left;
t->left = u->right;
u->right = t;
t->height = max(height(t->left), height(t->right))+1;
u->height = max(height(u->left), t->height)+1;
return u;
}
node* singleLeftRotate(node* &t)
{
node* u = t->right;
t->right = u->left;
u->left = t;
t->height = max(height(t->left), height(t->right))+1;
u->height = max(height(t->right), t->height)+1 ;
return u;
}
node* doubleLeftRotate(node* &t)
{
t->right = singleRightRotate(t->right);
return singleLeftRotate(t);
}
node* doubleRightRotate(node* &t)
{
t->left = singleLeftRotate(t->left);
return singleRightRotate(t);
}
node* findMin(node* t)
{
if(t == NULL)
return NULL;
else if(t->left == NULL)

2
return t;
else
return findMin(t->left);
}
node* findMax(node* t)
{
if(t == NULL)
return NULL;
else if(t->right == NULL)
return t;
else
return findMax(t->right);
}
node* remove(int x, node* t)
{
node* temp;
// Element not found
if(t == NULL)
return NULL;
// Searching for element
else if(x < t->data)
t->left = remove(x, t->left);
else if(x > t->data)
t->right = remove(x, t->right);
// Element found
// With 2 children
else if(t->left && t->right)
{
temp = findMin(t->right);
t->data = temp->data;
t->right = remove(t->data, t->right);
}
// With one or zero child
else
{
temp = t;
if(t->left == NULL)
t = t->right;
else if(t->right == NULL)
t = t->left;
delete temp;
}
if(t == NULL)
return t;
t->height = max(height(t->left), height(t->right))+1;
// If node is unbalanced

3
// If left node is deleted, right case
if(height(t->left) - height(t->right) == 2)
{
// right right case
if(height(t->left->left) - height(t->left->right) == 1)
return singleLeftRotate(t);
// right left case
else
return doubleLeftRotate(t);
}
// If right node is deleted, left case
else if(height(t->right) - height(t->left) == 2)
{
// left left case
if(height(t->right->right) - height(t->right->left) == 1)
return singleRightRotate(t);
// left right case
else
return doubleRightRotate(t);
}
return t;
}
int height(node* t)
{
return (t == NULL ? -1 : t->height);
}
int getBalance(node* t)
{
if(t == NULL)
return 0;
else
return height(t->left) - height(t->right);
}
void inorder(node* t)
{
if(t == NULL)
return;
inorder(t->left);
cout << t->data << " ";
inorder(t->right);
}
public:
BST()
{
root = NULL;
}

4
void insert(int x)
{
root = insert(x, root);
}
void remove(int x)
{
root = remove(x, root);
}
void display()
{
inorder(root);
cout << endl;
}
};
int main()
{
BST t;
t.insert(20);
t.insert(25);
t.insert(15);
t.insert(10);
t.insert(30);
t.insert(5);
t.insert(35);
t.insert(67);
t.insert(43);
t.insert(21);
t.insert(10);
t.insert(89);
t.insert(38);
t.insert(69);
t.display();
t.remove(5);
t.remove(35);
t.remove(65);
t.remove(89);
t.remove(43);
t.remove(88);
t.remove(20);
t.remove(38);
t.display();
}
//2nd program

3 #include<stdio.h>

5
4

5 typedef struct node

6 {

7     int data;

8     struct node *left,*right;

9     int ht;

10 }node;

11

12 node *insert(node *,int);

13 node *Delete(node *,int);

14 void preorder(node *);

15 void inorder(node *);

16 int height( node *);

17 node *rotateright(node *);

18 node *rotateleft(node *);

19 node *RR(node *);

20 node *LL(node *);

21 node *LR(node *);

22 node *RL(node *);

23 int BF(node *);

24

25 int main()

26 {

27     node *root=NULL;

28     int x,n,i,op;

6
29

30     do

31     {

32         printf("\n1)Create:");

33         printf("\n2)Insert:");

34         printf("\n3)Delete:");

35         printf("\n4)Print:");

36         printf("\n5)Quit:");

37         printf("\n\nEnter Your Choice:");

38         scanf("%d",&op);

39

40         switch(op)

41         {

42             case 1: printf("\nEnter no. of elements:");

43                     scanf("%d",&n);

44                     printf("\nEnter tree data:");

45                     root=NULL;

46                     for(i=0;i<n;i++)

47                     {

48                         scanf("%d",&x);

49                         root=insert(root,x);

50                     }

51                     break;

52

53             case 2: printf("\nEnter a data:");

7
54                     scanf("%d",&x);

55                     root=insert(root,x);

56                     break;

57

58             case 3: printf("\nEnter a data:");

59                     scanf("%d",&x);

60                     root=Delete(root,x);

61                     break;

62

63             case 4: printf("\nPreorder sequence:\n");

64                     preorder(root);

65                     printf("\n\nInorder sequence:\n");

66                     inorder(root);

67                     printf("\n");

68                     break;

69         }

70     }while(op!=5);

71

72     return 0;

73 }

74

75 node * insert(node *T,int x)

76 {

77     if(T==NULL)

78     {

8
79         T=(node*)malloc(sizeof(node));

80         T->data=x;

81         T->left=NULL;

82         T->right=NULL;

83     }

84     else

85         if(x > T->data)        // insert in right subtree

86         {

87             T->right=insert(T->right,x);

88             if(BF(T)==-2)

89                 if(x>T->right->data)

90                     T=RR(T);

91                 else

92                     T=RL(T);

93         }

94         else

95             if(x<T->data)

96             {

97                 T->left=insert(T->left,x);

98                 if(BF(T)==2)

99                     if(x < T->left->data)

100                         T=LL(T);

101                     else

102                         T=LR(T);

103             }

9
104

105         T->ht=height(T);

106

107         return(T);

108 }

109

110 node * Delete(node *T,int x)

111 {

112     node *p;

113

114     if(T==NULL)

115     {

116         return NULL;

117     }

118     else

119         if(x > T->data)        // insert in right subtree

120         {

121             T->right=Delete(T->right,x);

122             if(BF(T)==2)

123                 if(BF(T->left)>=0)

124                     T=LL(T);

125                 else

126                     T=LR(T);

127         }

128         else

10
129             if(x<T->data)

130             {

131                 T->left=Delete(T->left,x);

132                 if(BF(T)==-2)    //Rebalance during windup

133                     if(BF(T->right)<=0)

134                         T=RR(T);

135                     else

136                         T=RL(T);

137             }

138             else

139             {

140                 //data to be deleted is found

141                 if(T->right!=NULL)

142                 {    //delete its inorder succesor

143                     p=T->right;

144

145                     while(p->left!= NULL)

146                         p=p->left;

147

148                     T->data=p->data;

149                     T->right=Delete(T->right,p->data);

150

151                     if(BF(T)==2)//Rebalance during windup

152                         if(BF(T->left)>=0)

153                             T=LL(T);

11
154                         else

155                             T=LR(T);\

156                 }

157                 else

158                     return(T->left);

159             }

160     T->ht=height(T);

161     return(T);

162 }

163

164 int height(node *T)

165 {

166     int lh,rh;

167     if(T==NULL)

168         return(0);

169

170     if(T->left==NULL)

171         lh=0;

172     else

173         lh=1+T->left->ht;

174

175     if(T->right==NULL)

176         rh=0;

177     else

178         rh=1+T->right->ht;

12
179

180     if(lh>rh)

181         return(lh);

182

183     return(rh);

184 }

185

186 node * rotateright(node *x)

187 {

188     node *y;

189     y=x->left;

190     x->left=y->right;

191     y->right=x;

192     x->ht=height(x);

193     y->ht=height(y);

194     return(y);

195 }

196

197 node * rotateleft(node *x)

198 {

199     node *y;

200     y=x->right;

201     x->right=y->left;

202     y->left=x;

203     x->ht=height(x);

13
204     y->ht=height(y);

205

206     return(y);

207 }

208

209 node * RR(node *T)

210 {

211     T=rotateleft(T);

212     return(T);

213 }

214

215 node * LL(node *T)

216 {

217     T=rotateright(T);

218     return(T);

219 }

220

221 node * LR(node *T)

222 {

223     T->left=rotateleft(T->left);

224     T=rotateright(T);

225

226     return(T);

227 }

228

14
229 node * RL(node *T)

230 {

231     T->right=rotateright(T->right);

232     T=rotateleft(T);

233     return(T);

234 }

235

236 int BF(node *T)

237 {

238     int lh,rh;

239     if(T==NULL)

240         return(0);

241

242     if(T->left==NULL)

243         lh=0;

244     else

245         lh=1+T->left->ht;

246

247     if(T->right==NULL)

248         rh=0;

249     else

250         rh=1+T->right->ht;

251

252     return(lh-rh);

253 }

15
254 void preorder(node *T)

255 {

256     if(T!=NULL)

257     {

258         printf("%d(Bf=%d)",T->data,BF(T));

259         preorder(T->left);

260         preorder(T->right);

261     }

262 }

263

264 void inorder(node *T)

265 {

266     if(T!=NULL)

267     {

268         inorder(T->left);

269         printf("%d(Bf=%d)",T->data,BF(T));

270         inorder(T->right);

271     }

//3rd program

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
# include<stdlib.h>
class node
{
public:
int data;

16
struct node *left,*right;
int ht;
};

class AVL
{
node *root;
int height(node *);
node *rotate_right(node *);
node *rotate_left(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
public:
AVL()
{
root=NULL;
}
node *insert(node *,int);
node *delet(node *,int);
void preorder (node *);
void inorder(node *);
};
void main()
{
AVL A;
int x,n,i,choice;
node *root=NULL;
clrscr();
cout<<“\n\n\t\t\t\t\t\t\t\t\t\t\t\t…. CREATION OF AVL TREE ….”;

do
{
cout<<“\n\n MENU: “;
cout<<“\n\n\t1.Create\n\n\t2.Insert\n\n\t3.Delete\n\n\t4.Display\n\n\t5.Exit “;

cout<<“\n\nEnter the choice: “;


cin>>choice;
switch(choice)
{
case 1:
cout<<“\n\nEnter the number of elements: “;
cin>>n;
cout<<“\n\nEnter tree data: “;
for(i=0;i<n;i++)
{
cin>>x;

17
root=A.insert(root,x);
}
break;
case 2:
cout<<“\n\nEnter the data to be insert: “;
cin>>x;
A.insert(root,x);
break;
case 3:
cout<<“\n\nEnter a data which u have to delete: “;
cin>>x;
A.delet(root,x);
break;
case 4:
cout<<“\n\nPreorder Display:\n\n”;
A.preorder(root);
cout<<“\n\nInorder Display:\n\n”;
A.inorder(root);
break;
}
}while(choice!=5);
getch();
}

node *AVL::insert(node *T,int x)

{
if(T==NULL)
{
T=new node;
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
{
if(x>T->data)
{
T->right=insert(T->right,x);
if(BF(T)==-2)
{
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
}
else if(x<T->data)
{
T->left=insert(T->left,x);

18
if(BF(T)==2)
{
if(x<T->left->data)
T=LL(T);
else
T=LR(T);
}
}
}
T->ht=height(T);
return(T);
}

node *AVL::delet(node *T,int x)

{
node *p;
if(T==NULL)
return(0);
else
if(x>T->data)
{
T->right=delet(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else if(x<T->data)
{
T->left=delet(T->left,x);
if(BF(T)==-2)
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
if(T->right!=NULL)
{
p=T->right;
while(p->left!=NULL)
p=p->left;
T->data=p->data;
T->right=delet(T->right,p->data);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);

19
else
T=LR(T);
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}

int AVL::height(node *T)

{
int LH,RH;
if(T==NULL)
return(0);
if(T->left==NULL)
LH=0;
else
LH=1+T->left->ht;
if(T->right==NULL)
RH=0;
else
RH=1+T->right->ht;
if(LH>RH)
return (LH);
else
return (RH);
}

int AVL::BF(node *T)

{
int LH,RH;
if(T==NULL)
return(0);
if(T->left==NULL)
LH=0;
else
LH=1+T->left->ht;
if(T->right==NULL)
RH=0;
else
RH=1+T->right->ht;
return(LH-RH);
}

node *AVL::rotate_left(node *x)

20
{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}

node *AVL::rotate_right(node *x)

{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}

node *AVL::LL(node *T)

{
T=rotate_right(T);
return(T);
}

node *AVL::RR(node *T)

{
T=rotate_left(T);
return(T);
}

node *AVL::LR(node *T)

{
T->left=rotate_left(T->left);
T=rotate_right(T);
return(T);
}

node *AVL::RL(node *T)

{
T->right=rotate_right(T->right);
T=rotate_left(T);

21
return(T);
}

void AVL::preorder(node *root)

{
node *T=root;
if(T!=NULL)
{
cout<<” \n”<<T->data<<” [BF= “<<BF(T)<<“]”;
preorder(T->left);
preorder(T->right);
}
}

void AVL::inorder(node *root)

{
node *T=root;
if(T!=NULL)
{
inorder(T->left);
cout<<” \n”<<T->data<<” [BF= “<<BF(T)<<“]”;
inorder(T->right);
}
}

//4th program

struct node

int key;

unsigned char height;

node* left;

node* right;

node(int k) { key = k; left = right = 0; height = 1; }

};

22
unsigned char height(node* p)

return p?p->height:0;

int bfactor(node* p)

return height(p->right)-height(p->left);

void fixheight(node* p)

unsigned char hl = height(p->left);

unsigned char hr = height(p->right);

p->height = (hl>hr?hl:hr)+1;

node* rotateright(node* p)

node* q = p->left;

p->left = q->right;

q->right = p;

fixheight(p);

fixheight(q);

return q;

23
node* rotateleft(node* q)

node* p = q->right;

q->right = p->left;

p->left = q;

fixheight(q);

fixheight(p);

return p;

node* balance(node* p) // balancing the p node

fixheight(p);

if( bfactor(p)==2 )

if( bfactor(p->right) < 0 )

p->right = rotateright(p->right);

return rotateleft(p);

if( bfactor(p)==-2 )

if( bfactor(p->left) > 0 )

p->left = rotateleft(p->left);

return rotateright(p);

return p; // balancing is not required

24
node* insert(node* p, int k) // insert k key in a tree with p root

if( !p ) return new node(k);

if( k<p->key )

p->left = insert(p->left,k);

else

p->right = insert(p->right,k);

return balance(p);

node* findmin(node* p) // find a node with minimal key in a p tree

return p->left?findmin(p->left):p;

node* removemin(node* p) // deleting a node with minimal key from a p tree

if( p->left==0 )

return p->right;

p->left = removemin(p->left);

return balance(p);

node* remove(node* p, int k) // deleting k key from p tree

if( !p ) return 0;

25
if( k < p->key )

p->left = remove(p->left,k);

else if( k > p->key )

p->right = remove(p->right,k);

else // k == p->key

node* q = p->left;

node* r = p->right;

delete p;

if( !r ) return q;

node* min = findmin®;

min->right = removemin®;

min->left = q;

return balance(min);

return balance(p);

//5th program

1. /*
2.  * C++ program to Implement AVL Tree
3.  */
4. #include<iostream>
5. #include<cstdio>
6. #include<sstream>
7. #include<algorithm>
8. #define pow2(n) (1 << (n))
9. using namespace std;
10.  
11. /*
12.  * Node Declaration
26
13.  */
14. struct avl_node
15. {
16. int data;
17. struct avl_node *left;
18. struct avl_node *right;
19. }*root;
20.  
21. /*
22.  * Class Declaration
23.  */
24. class avlTree
25. {
26. public:
27. int height(avl_node *);
28. int diff(avl_node *);
29. avl_node *rr_rotation(avl_node *);
30. avl_node *ll_rotation(avl_node *);
31. avl_node *lr_rotation(avl_node *);
32. avl_node *rl_rotation(avl_node *);
33. avl_node* balance(avl_node *);
34. avl_node* insert(avl_node *, int );
35. void display(avl_node *, int);
36. void inorder(avl_node *);
37. void preorder(avl_node *);
38. void postorder(avl_node *);
39. avlTree()
40. {
41. root = NULL;
42. }
43. };
44.  
45. /*
46.  * Main Contains Menu
47.  */
48. int main()
49. {
50. int choice, item;
51. avlTree avl;
52. while (1)
53. {
54. cout<<"\n---------------------"<<endl;
55. cout<<"AVL Tree Implementation"<<endl;
56. cout<<"\n---------------------"<<endl;
57. cout<<"1.Insert Element into the tree"<<endl;
58. cout<<"2.Display Balanced AVL Tree"<<endl;

27
59. cout<<"3.InOrder traversal"<<endl;
60. cout<<"4.PreOrder traversal"<<endl;
61. cout<<"5.PostOrder traversal"<<endl;
62. cout<<"6.Exit"<<endl;
63. cout<<"Enter your Choice: ";
64. cin>>choice;
65. switch(choice)
66. {
67. case 1:
68. cout<<"Enter value to be inserted: ";
69. cin>>item;
70. root = avl.insert(root, item);
71. break;
72. case 2:
73. if (root == NULL)
74. {
75. cout<<"Tree is Empty"<<endl;
76. continue;
77. }
78. cout<<"Balanced AVL Tree:"<<endl;
79. avl.display(root, 1);
80. break;
81. case 3:
82. cout<<"Inorder Traversal:"<<endl;
83. avl.inorder(root);
84. cout<<endl;
85. break;
86. case 4:
87. cout<<"Preorder Traversal:"<<endl;
88. avl.preorder(root);
89. cout<<endl;
90. break;
91. case 5:
92. cout<<"Postorder Traversal:"<<endl;
93. avl.postorder(root);
94. cout<<endl;
95. break;
96. case 6:
97. exit(1);
98. break;
99. default:
100. cout<<"Wrong Choice"<<endl;
101. }
102. }
103. return 0;
104. }

28
105.  
106. /*
107.  * Height of AVL Tree
108.  */
109. int avlTree::height(avl_node *temp)
110. {
111. int h = 0;
112. if (temp != NULL)
113. {
114. int l_height = height (temp->left);
115. int r_height = height (temp->right);
116. int max_height = max (l_height, r_height);
117. h = max_height + 1;
118. }
119. return h;
120. }
121.  
122. /*
123.  * Height Difference
124.  */
125. int avlTree::diff(avl_node *temp)
126. {
127. int l_height = height (temp->left);
128. int r_height = height (temp->right);
129. int b_factor= l_height - r_height;
130. return b_factor;
131. }
132.  
133. /*
134.  * Right- Right Rotation
135.  */
136. avl_node *avlTree::rr_rotation(avl_node *parent)
137. {
138. avl_node *temp;
139. temp = parent->right;
140. parent->right = temp->left;
141. temp->left = parent;
142. return temp;
143. }
144. /*
145.  * Left- Left Rotation
146.  */
147. avl_node *avlTree::ll_rotation(avl_node *parent)
148. {
149. avl_node *temp;
150. temp = parent->left;

29
151. parent->left = temp->right;
152. temp->right = parent;
153. return temp;
154. }
155.  
156. /*
157.  * Left - Right Rotation
158.  */
159. avl_node *avlTree::lr_rotation(avl_node *parent)
160. {
161. avl_node *temp;
162. temp = parent->left;
163. parent->left = rr_rotation (temp);
164. return ll_rotation (parent);
165. }
166.  
167. /*
168.  * Right- Left Rotation
169.  */
170. avl_node *avlTree::rl_rotation(avl_node *parent)
171. {
172. avl_node *temp;
173. temp = parent->right;
174. parent->right = ll_rotation (temp);
175. return rr_rotation (parent);
176. }
177.  
178. /*
179.  * Balancing AVL Tree
180.  */
181. avl_node *avlTree::balance(avl_node *temp)
182. {
183. int bal_factor = diff (temp);
184. if (bal_factor > 1)
185. {
186. if (diff (temp->left) > 0)
187. temp = ll_rotation (temp);
188. else
189. temp = lr_rotation (temp);
190. }
191. else if (bal_factor < -1)
192. {
193. if (diff (temp->right) > 0)
194. temp = rl_rotation (temp);
195. else
196. temp = rr_rotation (temp);

30
197. }
198. return temp;
199. }
200.  
201. /*
202.  * Insert Element into the tree
203.  */
204. avl_node *avlTree::insert(avl_node *root, int value)
205. {
206. if (root == NULL)
207. {
208. root = new avl_node;
209. root->data = value;
210. root->left = NULL;
211. root->right = NULL;
212. return root;
213. }
214. else if (value < root->data)
215. {
216. root->left = insert(root->left, value);
217. root = balance (root);
218. }
219. else if (value >= root->data)
220. {
221. root->right = insert(root->right, value);
222. root = balance (root);
223. }
224. return root;
225. }
226.  
227. /*
228.  * Display AVL Tree
229.  */
230. void avlTree::display(avl_node *ptr, int level)
231. {
232. int i;
233. if (ptr!=NULL)
234. {
235. display(ptr->right, level + 1);
236. printf("\n");
237. if (ptr == root)
238. cout<<"Root -> ";
239. for (i = 0; i < level && ptr != root; i++)
240. cout<<" ";
241. cout<<ptr->data;
242. display(ptr->left, level + 1);

31
243. }
244. }
245.  
246. /*
247.  * Inorder Traversal of AVL Tree
248.  */
249. void avlTree::inorder(avl_node *tree)
250. {
251. if (tree == NULL)
252. return;
253. inorder (tree->left);
254. cout<<tree->data<<" ";
255. inorder (tree->right);
256. }
257. /*
258.  * Preorder Traversal of AVL Tree
259.  */
260. void avlTree::preorder(avl_node *tree)
261. {
262. if (tree == NULL)
263. return;
264. cout<<tree->data<<" ";
265. preorder (tree->left);
266. preorder (tree->right);
267.  
268. }
269.  
270. /*
271.  * Postorder Traversal of AVL Tree
272.  */
273. void avlTree::postorder(avl_node *tree)
274. {
275. if (tree == NULL)
276. return;
277. postorder ( tree ->left );
278. postorder ( tree ->right );
279. cout<<tree->data<<" ";
280. }
//6th program

#include<iostream>
#include<stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define NULL 0

32
class AVL;
class AVLNODE
{
friend class AVL;
private:
int data;
AVLNODE *left,*right;
int bf;
};
class AVL
{
private:
AVLNODE *root;
public:
AVLNODE *loc,*par;
AVL()
{
root=NULL;
}
int insert(int);
void displayitem();
void display(AVLNODE *);
void removeitem(int);
void remove1(AVLNODE *,AVLNODE *,int);
void remove2(AVLNODE *,AVLNODE *,int);
void search(int x);
void search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
AVLNODE *a,*b,*c,*f,*p,*q,*y,*clchild,*crchild;
int found,unbalanced;
int d;
if(!root) //special case empty tree
{ y=new AVLNODE;
y->data=x;
root=y;
root->bf=0;
root->left=root->right=NULL;
return TRUE; }
//phase 1:locate insertion point for x.a keeps track of the most
// recent node with balance factor +/-1,and f is the parent of a
// q follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{ //search for insertion point for x
if(p->bf)

33
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else if(x>p->data)
{
q=p;
p=p->right;
}
else
{
y=p;
found=TRUE;
}
} //end while
//phase 2:insert and rebalance.x is not in the tree and
// may be inserted as the appropriate child of q.
if(!found)
{
y = new AVLNODE;
y->data=x;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust balance factors of nodes on path from a to q
//note that by the definition of a,all nodes on this
//path must have balance factors of 0 and so will change
//to +/- d=+1 implies that x is inserted in the left
// subtree of a d=-1 implies
//to that x inserted in the right subtree of a.

if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;

34
d=1;
}
while(p!=y)
if(x>p->data) //height of right increases by 1
{
p->bf=-1;
p=p->right;
}
else //height of left increases by 1
{
p->bf=1;
p=p->left;
}
//is tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{ //tree still balanced
a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation type
{
if(d==1)
{ //left imbalance
if(b->bf==1) //rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;
c->left=b;
c->right=a;

switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;

35
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
} //end of left imbalance
else //right imbalance
{
if(b->bf==-1) //rotation type RR
{
a->right=b->left;
b->left=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
}
//subtree with root b has been rebalanced and is the new subtree

if(!f)
root=b;
else if(a==f->left)
f->left=b;
else if(a==f->right)
f->right=b;
} //end of if unbalanced
return TRUE;
} //end of if(!found)
return FALSE;

36
} //end of AVL INSERTION

void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
{
if(temp==NULL)
return;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE *ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
root=l;
suc->left=l->left;
suc->right=l->right;
return;

37
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
AVLNODE *child;
if(s->left==NULL && s->right==NULL)
child=NULL;
else if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;

}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE *ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe tree is empty";
return;
}
if(temp->data==x)
{
cout<<"\nthe item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return; }
if( x < temp->data)
{
ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;
save=temp;
}

38
while(ptr!=NULL)
{
if(x==ptr->data)
{ flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;

}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}

main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter u r choice to perform on AVL tree";
cin>>c;

switch(c)
{
case 1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;
case 2:a.displayitem(); break;
case 3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);

39
break;
case 4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case 5:exit(0); break;
default :cout<<"\nInvalid option try again";
}
cout<<"\ndo u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}

40

You might also like