6
6
* \warning This program is a poor implementation and does not utilize any of
7
7
* the C++ STL features.
8
8
*/
9
- #include < algorithm>
10
- #include < iostream>
11
- #include < queue>
9
+ #include < algorithm> // / for std::max
10
+ #include < iostream> // / for std::cout
11
+ #include < queue> // / for std::queue
12
12
13
- typedef struct node {
13
+ using node = struct node {
14
14
int data;
15
15
int height;
16
16
struct node *left;
17
17
struct node *right;
18
- } node ;
18
+ };
19
19
20
- /* * Create and return a new Node */
20
+ /* *
21
+ * @brief creates and returns a new node
22
+ * @param[in] data value stored in the node
23
+ * @return newly created node
24
+ */
21
25
node *createNode (int data) {
22
26
node *nn = new node ();
23
27
nn->data = data;
24
28
nn->height = 0 ;
25
- nn->left = NULL ;
26
- nn->right = NULL ;
29
+ nn->left = nullptr ;
30
+ nn->right = nullptr ;
27
31
return nn;
28
32
}
29
33
30
- /* * Returns height of tree */
34
+ /* *
35
+ * @param[in] root the root of the tree
36
+ * @return height of tree
37
+ */
31
38
int height (node *root) {
32
- if (root == NULL )
39
+ if (root == nullptr ) {
33
40
return 0 ;
41
+ }
34
42
return 1 + std::max (height (root->left ), height (root->right ));
35
43
}
36
44
37
- /* * Returns difference between height of left and right subtree */
45
+ /* *
46
+ * @param[in] root of the tree
47
+ * @return difference between height of left and right subtree
48
+ */
38
49
int getBalance (node *root) { return height (root->left ) - height (root->right ); }
39
50
40
- /* * Returns Node after Right Rotation */
51
+ /* *
52
+ * @param root of the tree to be rotated
53
+ * @return node after right rotation
54
+ */
41
55
node *rightRotate (node *root) {
42
56
node *t = root->left ;
43
57
node *u = t->right ;
@@ -46,7 +60,10 @@ node *rightRotate(node *root) {
46
60
return t;
47
61
}
48
62
49
- /* * Returns Node after Left Rotation */
63
+ /* *
64
+ * @param root of the tree to be rotated
65
+ * @return node after left rotation
66
+ */
50
67
node *leftRotate (node *root) {
51
68
node *t = root->right ;
52
69
node *u = t->left ;
@@ -55,55 +72,67 @@ node *leftRotate(node *root) {
55
72
return t;
56
73
}
57
74
58
- /* * Returns node with minimum value in the tree */
75
+ /* *
76
+ * @param root of the tree
77
+ * @returns node with minimum value in the tree
78
+ */
59
79
node *minValue (node *root) {
60
- if (root->left == NULL )
80
+ if (root->left == nullptr ) {
61
81
return root;
82
+ }
62
83
return minValue (root->left );
63
84
}
64
85
65
- /* * Balanced Insertion */
86
+ /* *
87
+ * @brief inserts a new element into AVL tree
88
+ * @param root of the tree
89
+ * @param[in] item the element to be insterted into the tree
90
+ * @return root of the updated tree
91
+ */
66
92
node *insert (node *root, int item) {
67
- node *nn = createNode (item);
68
- if (root == NULL )
69
- return nn;
70
- if (item < root->data )
93
+ if (root == nullptr ) {
94
+ return createNode (item);
95
+ }
96
+ if (item < root->data ) {
71
97
root->left = insert (root->left , item);
72
- else
98
+ } else {
73
99
root->right = insert (root->right , item);
100
+ }
74
101
int b = getBalance (root);
75
102
if (b > 1 ) {
76
- if (getBalance (root->left ) < 0 )
103
+ if (getBalance (root->left ) < 0 ) {
77
104
root->left = leftRotate (root->left ); // Left-Right Case
78
- return rightRotate (root); // Left-Left Case
105
+ }
106
+ return rightRotate (root); // Left-Left Case
79
107
} else if (b < -1 ) {
80
- if (getBalance (root->right ) > 0 )
108
+ if (getBalance (root->right ) > 0 ) {
81
109
root->right = rightRotate (root->right ); // Right-Left Case
82
- return leftRotate (root); // Right-Right Case
110
+ }
111
+ return leftRotate (root); // Right-Right Case
83
112
}
84
113
return root;
85
114
}
86
115
87
- /* * Balanced Deletion */
88
- node *deleteNode (node *root, int key) {
89
- if (root == NULL )
116
+ /* *
117
+ * @brief removes a given element from AVL tree
118
+ * @param root of the tree
119
+ * @param[in] element the element to be deleted from the tree
120
+ * @return root of the updated tree
121
+ */
122
+ node *deleteNode (node *root, int element) {
123
+ if (root == nullptr ) {
90
124
return root;
91
- if (key < root->data )
92
- root->left = deleteNode (root->left , key);
93
- else if (key > root->data )
94
- root->right = deleteNode (root->right , key);
125
+ }
126
+ if (element < root->data ) {
127
+ root->left = deleteNode (root->left , element);
128
+ } else if (element > root->data ) {
129
+ root->right = deleteNode (root->right , element);
95
130
96
- else {
131
+ } else {
97
132
// Node to be deleted is leaf node or have only one Child
98
- if (!root->right ) {
99
- node *temp = root->left ;
100
- delete (root);
101
- root = NULL ;
102
- return temp;
103
- } else if (!root->left ) {
104
- node *temp = root->right ;
105
- delete (root);
106
- root = NULL ;
133
+ if (!root->right || !root->left ) {
134
+ node *temp = !root->right ? root->left : root->right ;
135
+ delete root;
107
136
return temp;
108
137
}
109
138
// Node to be deleted have both left and right subtrees
@@ -115,26 +144,46 @@ node *deleteNode(node *root, int key) {
115
144
return root;
116
145
}
117
146
118
- /* * LevelOrder (Breadth First Search) */
147
+ /* *
148
+ * @brief calls delete on every node
149
+ * @param root of the tree
150
+ */
151
+ void deleteAllNodes (const node *const root) {
152
+ if (root) {
153
+ deleteAllNodes (root->left );
154
+ deleteAllNodes (root->right );
155
+ delete root;
156
+ }
157
+ }
158
+
159
+ /* *
160
+ * @brief prints given tree in the LevelOrder
161
+ * @param[in] root of the tree
162
+ */
119
163
void levelOrder (node *root) {
120
164
std::queue<node *> q;
121
165
q.push (root);
122
166
while (!q.empty ()) {
123
167
root = q.front ();
124
168
std::cout << root->data << " " ;
125
169
q.pop ();
126
- if (root->left )
170
+ if (root->left ) {
127
171
q.push (root->left );
128
- if (root->right )
172
+ }
173
+ if (root->right ) {
129
174
q.push (root->right );
175
+ }
130
176
}
131
177
}
132
178
133
- /* * Main function */
179
+ /* *
180
+ * @brief Main function
181
+ * @returns 0 on exit
182
+ */
134
183
int main () {
135
184
// Testing AVL Tree
136
- node *root = NULL ;
137
- int i;
185
+ node *root = nullptr ;
186
+ int i = 0 ;
138
187
for (i = 1 ; i <= 7 ; i++) root = insert (root, i);
139
188
std::cout << " LevelOrder: " ;
140
189
levelOrder (root);
@@ -144,5 +193,6 @@ int main() {
144
193
root = deleteNode (root, 4 ); // Deletin key with value 4
145
194
std::cout << " \n LevelOrder: " ;
146
195
levelOrder (root);
196
+ deleteAllNodes (root);
147
197
return 0 ;
148
198
}
0 commit comments