1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
template<class ItemType, class OtherType>
class BinarySearchTree : public BinaryTreeInterface<ItemType, OtherType>
{
public:
BinarySearchTree(): root_(NULL) { };
BinarySearchTree(const BinarySearchTree<ItemType, OtherType>& tree);
BinarySearchTree& operator=(const BinarySearchTree<ItemType, OtherType>& right_hand_side);
virtual ~BinarySearchTree();
bool IsEmpty() const;
int GetHeight() const;
int GetNumberOfNodes() const;
bool Add(const ItemType& item, const OtherType& other);
bool Remove(const ItemType& data); // Removes a node
void Clear();
OtherType GetOther(const ItemType& an_item) const throw(NotFoundException);
OtherType &GetOtherReference(const ItemType& an_item) throw(NotFoundException);
bool Contains(const ItemType& an_item) const;
void PreorderTraverse(void visit(ItemType&)) const;
void InorderTraverse(void visit(ItemType&)) const;
void PostorderTraverse(void visit(ItemType&)) const;
private:
BinaryNode<ItemType, OtherType>* root_;
int GetHeightHelper(BinaryNode<ItemType, OtherType>* node_ptr) const;
int GetNumberOfNodesHelper(BinaryNode<ItemType, OtherType>* node_ptr) const;
void DestroyTree(BinaryNode<ItemType, OtherType>* node_ptr);
BinaryNode<ItemType, OtherType>* InsertInorder(BinaryNode<ItemType, OtherType>* subTreePtr, BinaryNode<ItemType, OtherType>* newNode);
BinaryNode<ItemType, OtherType>* RemoveValue(BinaryNode<ItemType, OtherType>* subTreePtr, const ItemType target, bool& success);
BinaryNode<ItemType, OtherType>* RemoveNode(BinaryNode<ItemType, OtherType>* nodePtr);
BinaryNode<ItemType, OtherType>* RemoveLeftmostNode(BinaryNode<ItemType, OtherType>* subTreePtr, ItemType& inorderSuccessor);
BinaryNode<ItemType, OtherType>* FindNode(BinaryNode<ItemType, OtherType>* treePtr, const ItemType& target) const;
BinaryNode<ItemType, OtherType>* CopyTree(const BinaryNode<ItemType, OtherType>*node_ptr) const;
void Preorder(void visit(ItemType&), BinaryNode<ItemType, OtherType>* node_ptr) const;
void Inorder(void visit(ItemType&), BinaryNode<ItemType, OtherType>* node_ptr) const;
void Postorder(void visit(ItemType&), BinaryNode<ItemType, OtherType>* node_ptr) const;
|