-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL.cpp
More file actions
173 lines (155 loc) · 2.85 KB
/
Copy pathAVL.cpp
File metadata and controls
173 lines (155 loc) · 2.85 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
using namespace std;
struct tnode
{
string data;
string mean;
tnode * L,* R;
tnode()
{
L=R=NULL;
}
};
class AVL
{
private:
tnode * root;
public:
AVL(){root = NULL;}
tnode * LL(tnode *);
tnode * RR(tnode *);
tnode * LR(tnode *);
tnode * RL(tnode *);
int height(tnode *);
int MAX(int,int);
void create();
tnode * insert(tnode *,string,string);
void display();
void inorder(tnode *);
void preorder(tnode *);
};
tnode * AVL :: LL(tnode *parent)
{
tnode *temp;
temp = parent->L;
parent->L = temp->R;
temp->R = parent;
return temp;
}
tnode * AVL :: RR(tnode *parent)
{
tnode *temp;
temp = parent->R;
parent->R = temp->L;
temp->L = parent;
return temp;
}
tnode * AVL :: LR(tnode *parent)
{
parent->L = RR(parent->L);
parent = LL(parent);
return parent;
}
tnode * AVL :: RL(tnode *parent)
{
parent->R = LL(parent->R);
parent = RR(parent);
return parent;
}
int AVL :: MAX(int a, int b)
{
if(a>b)
return a;
else
return b;
}
int AVL :: height(tnode *temp )
{
if(temp == NULL) //height of empty node(subtree)
return -1;
if(temp->L == NULL and temp->R == NULL) //height of leaf node
return 0;
return(1 + MAX( height(temp->L),height(temp->R) ));
}
void AVL :: create()
{
string val,mval;
char ch;
do
{
cout<<"Enter word & Meaning : ";
cin>>val>>mval;
root = insert(root,val,mval);
cout<<"Continue : ";
cin>>ch;
}while(ch=='y');
}
tnode * AVL :: insert(tnode *temp,string val,string mval)
{
if(temp == NULL)
{
temp = new tnode;
temp->data = val;
temp->mean= mval;
//temp->L = temp->R = NULL;
}
else
{
if(val < temp->data)
{
temp->L = insert(temp->L,val,mval);
if( ( height(temp->L) - height(temp->R) == 2 ) )
{
if(val < temp->L->data)
temp = LL(temp);
else
temp = LR(temp);
}
}
else
{
temp->R = insert(temp->R,val,mval);
if( ( height(temp->L) - height(temp->R)== -2 ) )
{
if(val > temp->R->data)
temp = RR(temp);
else
temp = RL(temp);
}
}
}
return temp;
}
void AVL :: inorder(tnode *temp)
{
if(temp != NULL)
{
inorder(temp->L);
cout<<"\n"<<temp->data<<"\t"<<temp->mean;
inorder(temp->R);
}
}
void AVL :: preorder(tnode *temp)
{
if(temp != NULL)
{
cout<<"\n"<<temp->data<<"\t"<<temp->mean;
preorder(temp->L);
preorder(temp->R);
}
}
void AVL :: display()
{
cout<<"\n Dictionary Data in Sorted way : ";
inorder(root);
cout<<"\n Preorder : ";
preorder(root);
}
int main()
{
AVL t;
t.create();
t.display();
cout<<endl;
return 0;
}