1
+
2
+ #include < iostream>
3
+ #include < cstdlib>
4
+ #include < cstring>
5
+ #include < fstream>
6
+ using namespace std ;
7
+ /*
8
+ * Node Declaration
9
+ */
10
+ struct node
11
+ {
12
+ int count, level;
13
+ string key;
14
+ node *right;
15
+ node *left;
16
+ node *parent;
17
+ node *root;
18
+ }*root;
19
+
20
+ /*
21
+ * Class Declaration
22
+ */
23
+ class AATree
24
+ {
25
+ public:
26
+ int lookup (string &);
27
+ void skew (node *);
28
+ bool split (node *);
29
+ void rebal (node *);
30
+ node *insert (node *,node *);
31
+ void print (node *);
32
+ int countnode (node *);
33
+ AATree ()
34
+ {
35
+ root = NULL ;
36
+ }
37
+ };
38
+
39
+ /*
40
+ * Main: Contains Menu
41
+ */
42
+ int main ()
43
+ {
44
+ AATree at;
45
+ int ch;
46
+ string x;
47
+ ifstream fin (" test.txt" );
48
+ while (1 )
49
+ {
50
+ cout<<" \n ---------------------" <<endl;
51
+ cout<<" \n Operations on AA Tree" <<endl;
52
+ cout<<" \n ---------------------" <<endl;
53
+ cout<<" 1.Insert String into the Tree" <<endl;
54
+ cout<<" 2.Print Tree Data" <<endl;
55
+ cout<<" 3.Total Tree Nodes" <<endl;
56
+ cout<<" 4.Exit" <<endl;
57
+ cout<<" Enter Your Choice: " ;
58
+ cin>>ch;
59
+ switch (ch)
60
+ {
61
+ case 1 :
62
+ if (fin.is_open ())
63
+ {
64
+ while (fin>>x)
65
+ {
66
+ at.lookup (x);
67
+ }
68
+ fin.close ();
69
+ }
70
+ break ;
71
+ case 2 :
72
+ cout<<" Elemets of AA Tree" <<endl;
73
+ at.print (root);
74
+ break ;
75
+ case 3 :
76
+ cout<<" Total number of nodes" <<endl;
77
+ cout<<at.countnode (root)<<endl;
78
+ break ;
79
+ case 4 :
80
+ cout<<" Exiting" <<endl;
81
+ exit (1 );
82
+ break ;
83
+ default :
84
+ cout<<" Wrong Choice" <<endl;
85
+ }
86
+ }
87
+ return 0 ;
88
+ }
89
+ /*
90
+ * Insert String into the Tree
91
+ */
92
+ int AATree::lookup (string &key)
93
+ {
94
+ node *temp = new node;
95
+ temp->key = key;
96
+ temp->level = 1 ;
97
+ temp->count = 0 ;
98
+ temp->left = NULL ;
99
+ temp->right = NULL ;
100
+ temp->parent = NULL ;
101
+ temp = insert (root, temp);
102
+ return temp->count ;
103
+ }
104
+
105
+ /*
106
+ * Skew Tree
107
+ */
108
+
109
+ void AATree::skew (node *temp)
110
+ {
111
+ node *ptr = temp->left ;
112
+ if (temp->parent ->left == temp)
113
+ temp->parent ->left = ptr;
114
+ else
115
+ temp->parent ->right = ptr;
116
+ ptr->parent = temp->parent ;
117
+ temp->parent = ptr;
118
+ temp->left = ptr->right ;
119
+ if (temp->left != NULL )
120
+ temp->left ->parent = temp;
121
+ ptr->right = temp;
122
+ temp->level = (temp->left ? temp->left ->level + 1 : 1 );
123
+ }
124
+
125
+ /*
126
+ * Splitting of AA Tree
127
+ */
128
+ bool AATree::split (node *temp)
129
+ {
130
+ node* ptr = temp->right ;
131
+ if (ptr && ptr->right && (ptr->right ->level == temp->level ))
132
+ {
133
+ if (temp->parent ->left == temp)
134
+ temp->parent ->left = ptr;
135
+ else
136
+ temp->parent ->right = ptr;
137
+ ptr->parent = temp->parent ;
138
+ temp->parent = ptr;
139
+ temp->right = ptr->left ;
140
+ if (temp->right != NULL )
141
+ temp->right ->parent = temp;
142
+ ptr->left = temp;
143
+ ptr->level = temp->level + 1 ;
144
+ return true ;
145
+ }
146
+ return false ;
147
+ }
148
+
149
+ /*
150
+ * Rebalancing of AA Tree
151
+ */
152
+ void AATree::rebal (node* temp)
153
+ {
154
+ temp->left = NULL ;
155
+ temp->right = NULL ;
156
+ temp->level = 1 ;
157
+ for (temp = temp->parent ; temp != root; temp = temp->parent )
158
+ {
159
+ if (temp->level != (temp->left ? temp->left ->level + 1 : 1 ))
160
+ {
161
+ skew (temp);
162
+ if (temp->right == NULL )
163
+ temp = temp->parent ;
164
+ else if (temp->level != temp->right ->level )
165
+ temp = temp->parent ;
166
+ }
167
+ if (temp->parent != root)
168
+ {
169
+ if (split (temp->parent ) == false )
170
+ break ;
171
+ }
172
+ }
173
+ }
174
+
175
+ /*
176
+ * Insert Function to insert string into the tree
177
+ */
178
+ node* AATree::insert (node* temp, node* ins)
179
+ {
180
+ if (root == NULL )
181
+ {
182
+ ins->count = 1 ;
183
+ ins->parent = NULL ;
184
+ ins->left = NULL ;
185
+ ins->right = NULL ;
186
+ root = ins;
187
+ return root;
188
+ }
189
+ if (ins->key < temp->key )
190
+ {
191
+ if (temp->left )
192
+ return insert (temp->left , ins);
193
+ temp->left = ins;
194
+ ins->parent = temp;
195
+ ins->count = 1 ;
196
+ rebal (ins);
197
+ return ins;
198
+ }
199
+ if (ins->key > temp->key )
200
+ {
201
+ if (temp->right )
202
+ return insert (temp->right , ins);
203
+ temp->right = ins;
204
+ ins->parent = temp;
205
+ ins->count = 1 ;
206
+ rebal (ins);
207
+ return ins;
208
+ }
209
+ temp->count ++;
210
+ delete ins;
211
+ return temp;
212
+ }
213
+
214
+ /*
215
+ * Display Tree Elements
216
+ */
217
+ void AATree::print (node* temp)
218
+ {
219
+ if (!temp)
220
+ return ;
221
+ print (temp->left );
222
+ cout <<" Value: " <<temp->key << " Count:" << temp->count ;
223
+ cout<<" Level: " <<temp->level <<endl;
224
+ print (temp->right );
225
+ }
226
+
227
+ /*
228
+ * Count number of nodes in AA Tree
229
+ */
230
+ int AATree::countnode (node* temp)
231
+ {
232
+ if (!temp)
233
+ return 0 ;
234
+ int count = 1 ;
235
+ count = count + countnode (temp->left );
236
+ count = count + countnode (temp->right );
237
+ return count;
238
+ }
0 commit comments