Added BST remove functionality
This commit is contained in:
parent
c410c92343
commit
a74b3e2aca
@ -10,12 +10,12 @@ private:
|
||||
|
||||
BinaryTreeNode<T>* _search(BinaryTreeNode<T>* treePtr, const T& target) const;
|
||||
|
||||
//BinaryNode<ItemType>* _remove(BinaryNode<ItemType>* nodePtr, const ItemType target, bool& success);
|
||||
BinaryTreeNode<T>* _remove(BinaryTreeNode<T>* nodePtr, const T& target);
|
||||
|
||||
public:
|
||||
void insert(const T& item);
|
||||
|
||||
void remove(const T &item) { throw std::logic_error("Not implemented: BinarySearchTree.remove()"); };
|
||||
bool remove(const T& item);
|
||||
|
||||
bool search(const T& target, T& returnedItem) const;
|
||||
};
|
||||
@ -54,6 +54,60 @@ BinaryTreeNode<T>* BinarySearchTree<T>::_insert(BinaryTreeNode<T>* nodePtr, Bina
|
||||
return nodePtr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BinarySearchTree<T>::remove(const T& item) {
|
||||
BinaryTreeNode<T>* removed = _remove(this->root, item);
|
||||
if (removed) {
|
||||
this->size--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
BinaryTreeNode<T>* BinarySearchTree<T>::_remove(BinaryTreeNode<T>* nodePtr, const T& target) {
|
||||
BinaryTreeNode<T>* parNode = nullptr;
|
||||
BinaryTreeNode<T>* currNode = nodePtr;
|
||||
|
||||
while (currNode) {
|
||||
if (currNode->getItem() == target) {
|
||||
if (!currNode->getLeftPtr() && !currNode->getRightPtr()) { // if target is leaf node
|
||||
if (!parNode) this->root = nullptr;
|
||||
else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(nullptr);
|
||||
else parNode->setRightPtr(nullptr);
|
||||
}
|
||||
else if (!currNode->getRightPtr()) { // if target has only left child
|
||||
if (!parNode) this->root = currNode->getLeftPtr();
|
||||
else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(currNode->getLeftPtr());
|
||||
else parNode->setRightPtr(currNode->getLeftPtr());
|
||||
}
|
||||
else if (!currNode->getLeftPtr()) { // if target has only right child
|
||||
if (!parNode) this->root = currNode->getRightPtr();
|
||||
else if (parNode->getLeftPtr() == currNode) parNode->setLeftPtr(currNode->getRightPtr());
|
||||
else parNode->setRightPtr(currNode->getRightPtr());
|
||||
}
|
||||
else { // if target has both left and right child
|
||||
BinaryTreeNode<T>* sucNode = currNode->getRightPtr();
|
||||
while (sucNode->getLeftPtr())
|
||||
sucNode = sucNode->getLeftPtr();
|
||||
T sucData = sucNode->getItem();
|
||||
_remove(this->root, sucData);
|
||||
currNode->setItem(sucData);
|
||||
}
|
||||
return currNode;
|
||||
}
|
||||
else if (currNode->getItem() < target) {
|
||||
parNode = currNode;
|
||||
currNode = currNode->getRightPtr();
|
||||
}
|
||||
else {
|
||||
parNode = currNode;
|
||||
currNode = currNode->getLeftPtr();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BinarySearchTree<T>::search(const T& target, T& returnedItem) const
|
||||
{
|
||||
|
@ -32,7 +32,7 @@ public:
|
||||
|
||||
// abstract functions to be implemented by derived class
|
||||
virtual void insert(const T& newData) = 0;
|
||||
//virtual bool remove(const T &data) = 0;
|
||||
virtual bool remove(const T &data) = 0;
|
||||
virtual bool search(const T& target, T& returnedItem) const = 0;
|
||||
|
||||
private:
|
||||
|
Loading…
x
Reference in New Issue
Block a user