Skip to content

Commit cc33efd

Browse files
vil02github-actions[bot]Panquesito7
authored
[fix/feat]: remove memory leak in avltree.cpp (#2429)
* fix: remove memory leak by adding deleteAllNodes * clang-format and clang-tidy fixes for 9d76f8b * docs: explain usage of standard headers * docs: use doxygen syntax * docs: document parameters of the functions * style: use proper spelling * style: simplify logic in deleteNode * docs: add missing [in] * docs: add missing slashes * docs: document `main` Co-authored-by: David Leal <[email protected]> * updating DIRECTORY.md * clang-format and clang-tidy fixes for c852f62 --------- Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent dc8ecfb commit cc33efd

File tree

3 files changed

+110
-58
lines changed

3 files changed

+110
-58
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
2020
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
2121
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
22+
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
2223
* [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp)
2324
* [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp)
2425

bit_manipulation/power_of_2.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* @file
33
* @brief [Find whether a given number is power of 2]
4-
* (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/) implementation
4+
* (https://www.geeksforgeeks.org/program-to-find-whether-a-given-number-is-power-of-2/)
5+
* implementation
56
*
67
* @details
7-
* We are given a positive integer number. We need to check whether the number is power of
8-
* 2 or not.
8+
* We are given a positive integer number. We need to check whether the number
9+
* is power of 2 or not.
910
*
1011
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
1112
* set bit in computer terms.
@@ -27,16 +28,16 @@ namespace bit_manipulation {
2728
* @param n is the number who will be checked
2829
* @returns either true or false
2930
*/
30-
bool isPowerOfTwo(
31-
std ::int64_t n) { // int64_t is preferred over int so that
32-
// no Overflow can be there.
31+
bool isPowerOfTwo(std ::int64_t n) { // int64_t is preferred over int so that
32+
// no Overflow can be there.
3333

34-
return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1
35-
// then all unset bits after the only set bit become set; and the set bit becomes unset.
34+
return n > 0 && !(n & n - 1); // If we subtract a power of 2 numbers by 1
35+
// then all unset bits after the only set bit become set; and the set bit
36+
// becomes unset.
3637

3738
// If a number n is a power of 2 then bitwise and of n-1 and n will be zero.
38-
// The expression n&(n-1) will not work when n is 0.
39-
// To handle this case also, our expression will become n& (!n&(n-1))
39+
// The expression n&(n-1) will not work when n is 0.
40+
// To handle this case also, our expression will become n& (!n&(n-1))
4041
}
4142
} // namespace bit_manipulation
4243

data_structures/avltree.cpp

Lines changed: 98 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,52 @@
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+
*/
2125
node *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+
*/
3138
int 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+
*/
3849
int 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+
*/
4155
node *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+
*/
5067
node *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+
*/
5979
node *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+
*/
6692
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) {
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+
*/
119163
void 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+
*/
134183
int 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 << "\nLevelOrder: ";
146195
levelOrder(root);
196+
deleteAllNodes(root);
147197
return 0;
148198
}

0 commit comments

Comments
 (0)