template<typename Type>
class BinaryNode {
private:
Type _element;
Type *_left_node;
Type *_right_node;
public:
BinaryNode(Type const &);
Type retrieve() const;
BinaryNode *left() const;
BinaryNode *right() const;
bool is_empty() const;
bool is_leaf() const;
int size() const;
int height() const;
void clear();
};
BinarySearchNode
template <typename Comparable>
class BinarySearchNode:public BinaryNode<Comparable> {
private:
using BinaryNode<Comparable>::_element;
using BinaryNode<Comparable>::_left_node;
using BinaryNode<Comparable>::_right_node;
public:
BinarySearchNode(Type cnst &);
BinarySearchNode *left() const;
BinarySearchNode *right() const;
Comparable front() const;
Comparable back() const;
bool find(Comparable const &);
void clear();
void insert(Comparable const &);
void erase(Comparable const &, BinarySearchNode *&);
friend class BinarySearchTree<Comparable>;
}
Minimum: front of the left subtree
Maximum: back of the right subtree
front()
template<typename Comparable>
Comparable BinarySearchNode<Comparable>::front() {
if (is_empty())
throw underflow();
return (left()->is_empty()) ? retrieve() : left()->front();
}
back()
template<typename Comparable>
Comparable BinarySearchNode<Comparable>::back() {
if (is_empty())
throw underflow();
return (right()->is_empty()) ? retrieve() : right()->back();
}
erase()
on either left or right subtrees.template<typename Type>
bool Binary_search_node<Type>::erase(Type &obj, Binary_search_node *&ptr_to_this) {
if (empty()) {
return false;
} else if ( obj == retrieve() ) { // leaf node
if (is_leaf()) {
ptr_to_this = nullptr;
delete this;
} else if (!left()->empty() && !right()->empty()) { // full node
element = right()->front(); //min val in the right subtree --> (logical successor)
right()->erase(retrieve(), right_tree);
} else { // only one child
ptr_to_this = (!left()->empty()) ? left() : right();
delete this;
}
return true;
} else if (obj < retreive()) {
return left()->erase(obj, left_tree);
} else {
return right()->erase(obj, right_tree);
}
}
In-order traversal of a binary search tree will result in sorting the elements.
Pre-order:
void IntBinaryTree::displayPreOrder(TreeNode *tree) const {
}
Post-order:
void IntBinaryTree::displayPostOrder(TreeNode *tree) const {
}
In [ ]: