66 * \warning This program is a poor implementation and does not utilize any of
77 * the C++ STL features.
88 */
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
1212
13- typedef struct node {
13+ using node = struct node {
1414 int data;
1515 int height;
1616 struct node *left;
1717 struct node *right;
18- } node ;
18+ };
1919
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+ */
2125node *createNode (int data) {
2226 node *nn = new node ();
2327 nn->data = data;
2428 nn->height = 0 ;
25- nn->left = NULL ;
26- nn->right = NULL ;
29+ nn->left = nullptr ;
30+ nn->right = nullptr ;
2731 return nn;
2832}
2933
30- /* * Returns height of tree */
34+ /* *
35+ * @param[in] root the root of the tree
36+ * @return height of tree
37+ */
3138int height (node *root) {
32- if (root == NULL )
39+ if (root == nullptr ) {
3340 return 0 ;
41+ }
3442 return 1 + std::max (height (root->left ), height (root->right ));
3543}
3644
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+ */
3849int getBalance (node *root) { return height (root->left ) - height (root->right ); }
3950
40- /* * Returns Node after Right Rotation */
51+ /* *
52+ * @param root of the tree to be rotated
53+ * @return node after right rotation
54+ */
4155node *rightRotate (node *root) {
4256 node *t = root->left ;
4357 node *u = t->right ;
@@ -46,7 +60,10 @@ node *rightRotate(node *root) {
4660 return t;
4761}
4862
49- /* * Returns Node after Left Rotation */
63+ /* *
64+ * @param root of the tree to be rotated
65+ * @return node after left rotation
66+ */
5067node *leftRotate (node *root) {
5168 node *t = root->right ;
5269 node *u = t->left ;
@@ -55,55 +72,67 @@ node *leftRotate(node *root) {
5572 return t;
5673}
5774
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+ */
5979node *minValue (node *root) {
60- if (root->left == NULL )
80+ if (root->left == nullptr ) {
6181 return root;
82+ }
6283 return minValue (root->left );
6384}
6485
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+ */
6692node *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 ) {
7197 root->left = insert (root->left , item);
72- else
98+ } else {
7399 root->right = insert (root->right , item);
100+ }
74101 int b = getBalance (root);
75102 if (b > 1 ) {
76- if (getBalance (root->left ) < 0 )
103+ if (getBalance (root->left ) < 0 ) {
77104 root->left = leftRotate (root->left ); // Left-Right Case
78- return rightRotate (root); // Left-Left Case
105+ }
106+ return rightRotate (root); // Left-Left Case
79107 } else if (b < -1 ) {
80- if (getBalance (root->right ) > 0 )
108+ if (getBalance (root->right ) > 0 ) {
81109 root->right = rightRotate (root->right ); // Right-Left Case
82- return leftRotate (root); // Right-Right Case
110+ }
111+ return leftRotate (root); // Right-Right Case
83112 }
84113 return root;
85114}
86115
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 ) {
90124 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);
95130
96- else {
131+ } else {
97132 // 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;
107136 return temp;
108137 }
109138 // Node to be deleted have both left and right subtrees
@@ -115,26 +144,46 @@ node *deleteNode(node *root, int key) {
115144 return root;
116145}
117146
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+ */
119163void levelOrder (node *root) {
120164 std::queue<node *> q;
121165 q.push (root);
122166 while (!q.empty ()) {
123167 root = q.front ();
124168 std::cout << root->data << " " ;
125169 q.pop ();
126- if (root->left )
170+ if (root->left ) {
127171 q.push (root->left );
128- if (root->right )
172+ }
173+ if (root->right ) {
129174 q.push (root->right );
175+ }
130176 }
131177}
132178
133- /* * Main function */
179+ /* *
180+ * @brief Main function
181+ * @returns 0 on exit
182+ */
134183int main () {
135184 // Testing AVL Tree
136- node *root = NULL ;
137- int i;
185+ node *root = nullptr ;
186+ int i = 0 ;
138187 for (i = 1 ; i <= 7 ; i++) root = insert (root, i);
139188 std::cout << " LevelOrder: " ;
140189 levelOrder (root);
@@ -144,5 +193,6 @@ int main() {
144193 root = deleteNode (root, 4 ); // Deletin key with value 4
145194 std::cout << " \n LevelOrder: " ;
146195 levelOrder (root);
196+ deleteAllNodes (root);
147197 return 0 ;
148198}
0 commit comments