mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix: remove memory leak by adding deleteAllNodes
This commit is contained in:
parent
7c090481da
commit
9d76f8bd7f
@ -64,9 +64,8 @@ node *minValue(node *root) {
|
|||||||
|
|
||||||
/** Balanced Insertion */
|
/** Balanced Insertion */
|
||||||
node *insert(node *root, int item) {
|
node *insert(node *root, int item) {
|
||||||
node *nn = createNode(item);
|
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
return nn;
|
return createNode(item);
|
||||||
if (item < root->data)
|
if (item < root->data)
|
||||||
root->left = insert(root->left, item);
|
root->left = insert(root->left, item);
|
||||||
else
|
else
|
||||||
@ -115,6 +114,15 @@ node *deleteNode(node *root, int key) {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Calls delete on every node */
|
||||||
|
void deleteAllNodes(const node *const root) {
|
||||||
|
if (root) {
|
||||||
|
deleteAllNodes(root->left);
|
||||||
|
deleteAllNodes(root->right);
|
||||||
|
delete root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** LevelOrder (Breadth First Search) */
|
/** LevelOrder (Breadth First Search) */
|
||||||
void levelOrder(node *root) {
|
void levelOrder(node *root) {
|
||||||
std::queue<node *> q;
|
std::queue<node *> q;
|
||||||
@ -144,5 +152,6 @@ int main() {
|
|||||||
root = deleteNode(root, 4); // Deletin key with value 4
|
root = deleteNode(root, 4); // Deletin key with value 4
|
||||||
std::cout << "\nLevelOrder: ";
|
std::cout << "\nLevelOrder: ";
|
||||||
levelOrder(root);
|
levelOrder(root);
|
||||||
|
deleteAllNodes(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user