Skip to content

Commit

Permalink
add bst destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankchheda committed Apr 19, 2020
1 parent 65405d7 commit a758dad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions BinarySearchTree/BST/BST.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
#include "BST.hpp"
#include <iostream>

/**
* Delete tree nodes recursively
*/
void BST::DeleteTreeRecursive(Node* node)
{
if (node == nullptr)
return;

if (node->left != nullptr)
DeleteTreeRecursive(node->left);
if (node->right != nullptr)
DeleteTreeRecursive(node->right);

delete node;
}

/**
* inserts new integer data into the tree at the position, so that
Expand Down
2 changes: 2 additions & 0 deletions BinarySearchTree/BST/BST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class BST {
Node* insert(Node* root, int data);
bool search(Node* root, int data);
int height(Node* root);
void DeleteTreeRecursive(Node* node);

public:
BST() { root = nullptr; }
~BST() { DeleteTreeRecursive(root); }
Node* getRoot() const { return root; }
void insert(int data);
bool search(int data);
Expand Down

0 comments on commit a758dad

Please sign in to comment.