65,187
社区成员




// 416 二叉查找树类 Binary Search Tree
template <typename Compareable>
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(const BinarySearchTree & rhs);
~BinarySearchTree();
const Comparable & findMin() const;
const Comparable & findMax() const;
bool contains(const Comparable & x) const;
bool isempty() const;
void printTree() const;
void makeEmpty();
void insert( const Comparable & x);
void remove(const Comparable& x);
const BinarySearchTree & operator = ( const BinarySearchTree & rhs);
private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable & theElement, BinaryNode * lt, BinaryNode *rt)
: element(theElement), left(lt), right(rt){}
};
BinaryNode *root;
void insert (const comparable & x, BinaryNode * & t) const;
void remove(const Comparable & x, BinaryNode * & t)const;
BinaryNode * findMax(BinaryNode *t) const;
BinaryNode * findMin(BinaryNode *t) const;
bool contains (const Comparable & x, BinaryNode * t) const;
void makeEmpty(BinaryNode * & t);
void printTree(BinaryNode * t) const;
BinaryNode * clone(Binary * t) const;
};
// 以下定义全部省略 BinarySearchTree <Compareable>::
// Returns true if x is found in the tree.
bool contains (const Comparable & x) const
{
return contains(x, root);
}
/**
* Internal method to test if an item is in a subtree.
* x is item to search for.
* t is the node that roots the subtree.
*/
bool contains(const Comparable & x, BinaryNode * t) const
{
if ( t == NULL) // 空表
return false;
else if ( x < t -> element)
return contains(x, t -> left);
else if ( t -> element < x)
return contains(x, t -> right);
else
return true; // Match
}
// 这里的findMin使用递归实现,而findMax使用循环实现
/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
BinaryNode * findMin(BinaryNode * t) const
{
if (t == NULL) // 空表
return NULL;
if ( t -> left == NULL)
return t;
return findMin(t -> left);
}
/**
* Internal method to find the largest item in a subtree t
* Return node containing the largest item.
*/
BinaryNode * findMax(BinaryNode * t) const
{
if ( t != NULL)
while ( t -> right != NULL)
t = t - > right;
return t;
}
// Insert x into the tree; duplicates are ignored.
void insert(const Comparable & x)
{
insert(x, root);
}
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the subtree
* Set the new root of the subtree.
*/
void insert( const Comparable & x, BinartyTreeNode * & t)
{
if ( t == NULL) // 基准条件,找到位置后插入
t = new BinaryNode( x, NULL, NULL);
else if (x < t-> element) // 比较大小,找到合适的位置
insert(x, t -> left);
else if (t -> element < x)
insert(x, t -> right);
else
; // Duplicate; do nothing
}
// Remove x from the tree. Nothing is done if x is not found.
void remove(const Comparable & x)
{
remove(x, root);
}
// 同很多数据结构一样,最困难的操作是删除
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the subtree.
* Set the new root fo the subtree.
*/
void remove (const Comparable & x, BinaryNode * & t)
{
if (t == NULL) // 空表
return ; // Item not found; do nothing
if ( x < t -> element)
remove (x, t -> left);
else if ( t -> element < x)
remove (x, t -> right);
// 以下的这两个情况,都是已经找到跟x相等的元素的了
else if ( t -> left != NULL && t -> right != NULL) // two children
{
// 找出在x元素的结点以下的那些结点较小的一个作为元素替换
t -> element = findMax(t -> right) -> element;
// 替换工作完成后,可以继续实行递归,对其结点进行删除
// 这里将t指向的元素,替换后的元素作为实参
remove( t -> element, t -> right);
// 将t -> right指针移至最大值的位置,下次调用函数时,t即为最大值的指针
}
else
{
BinaryNode * oldNode = t;
t = ( ( t -> left != NULL) ? t -> left : t -> right) ;
delete oldNode;
}
}
/**
* Destructor for the tree
*/
~BinarySearchTree()
{
makeEmpty();
}
/**
* Internal method to make subtree empty.
*/
void makeEmpty(BinaryNode * & t)
{
if ( t != NULL );
{
makeEmpty( t -> left);
makeEmpty( t -> right);
delete t;
}
t = NULL;
}
/**
* Deep copy.
*/
const BinarySearchTree & operator = ( const BinarySearchTree & rhs)
{
if (this != &rhs)
{
makeEmpty();
root = clone (rhs.root);
}
return *this;
}
/**
* Internal method to clone subtree.
*/
BinaryNode * clone( BinaryNode *t ) const
{
if ( t == NULL)
return NULL;
return new BinaryNode(t -> element, clone (t -> left), clone (t -> right) );
}