Notes From CS Undergrad Courses FSU
This project is maintained by awa03
For Deleting nodes with more than one children (2+ children) Deletion Strategy: Replace the node with smallest node in the right sub-trees
void remove(const Comparable& x, BinaryNode *& t){
if(t == nullptr)
// item not found
return;
if(x < t->element)
remove(t, t->left)
else if(t->element < x)
remove(x, t->right)
else if(t->left != nullptr && t->right != nullptr){
// Two Children
t->element = findMin(t->right)->element;
remove(t->element, t->right);
}
else{
BinaryNode* oldNode =t;
t= (t->left != nullptr) ? t->left : t->right;
delete oldNode;
}
}
// inorder traversal -- recursive def
operator<<(os){
if(isEmpty())
os << "";
else
printTree(os);
}
void printTree(BinaryNode*t, os){
if(t != nullptr){
printTree(t->left);
os << t->element << endl;
printTree(t->right);
}
}
We have to use post order traversal for a destructor so as not to cause memory leaks, as a result of loosing access to the children upon the parents deletion
makeEmpty(t->left)
makeEmpty(t->right)
delete thisNode;
The copy constructor & assignment operator must be preorder traversal.
// Copy Const
BST(const BST& RHS){
root = clone(rhs.root);
}
// Helper Implementation -- Recursive
BinaryNode* clone(BinaryNode *t) const{
if(t == nullptr)
return;
else
return new BinaryNode{
t->element,
clone(t->left),
clone(t->right)
};
}
There will be extra overhead with AVL trees, since you must maintain information about height at each node. Insertion becomes more expensive, but it should still evaluate to LogN. Searches will be guaranteed to be between logN and logN + 1, reducing to LogN.