二叉查找树

使二叉树成为二叉查找树的性质是,对于树中的每个结点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项。
这里写图片描述


BinarySearchTree类模板接口

template <typename Comparable>
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, BianryNode * & t) const;
    BinaryNode * findMin(BinaryNode *t) const;
    BinaryNode * findMax(BinaryNode *t) const;
    bool contains(const Comparable &x, BinaryNode *t) const;
    void makeEmpty(BinaryNode * & t);
    void printTree(BinaryNode *t) const;
    BinaryNode * clone(BinaryNode *t) const;
};

公有成员函数调用私有递归成员函数的示例

/**
 *Returns true if x is found in the tree
 */
bool contains(const Comparable & x) const
{
    return contains(x, root);
}

/**
 *Insert x into the tree; duplicates are ignored
 */
void insert(const Comparable& x)
{
    insert(x, root);
}

/**
 *Remove x from the tree.Nothing is done if x is not found.
 */
void remove(const Comparable& x)
{
    remove(x, root);
}

二叉查找树的contains操作

/**
 *Internal method to test if an item is in a subtree
 *x is an item to search for
 *t is the node that roots the suntree
 */
bool contains(const Comparable& x, binaryNode *t) contant
{
    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
}

使用函数对象,不使用Comparable项示例,

template <typename Object, typename Comparator=less<Object> >
class BinarySearchTree
{
  public:
    //Same methods, with Object replacing Comparable

  private:

    BinaryNode *root;
    Comparator isLessThan;

    //Same methods, with Object replacing Comparable

    /**
     *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 Object& x, BinaryNode *t)  const
   {
       if(t == NULL)
           return false;
       else if(isLessThan(x, t->element) )
           return contains(x, t->left);
       else if(isLessThan(t->element, x) )
           return contains(x, t->right);
       else
           return true;        //Match
   }  
};

用递归编写的findMin

/**
 *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);
}

用非递归实现findMax

/**
 *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;
 }

将元素插入到二叉查找树的例程

/**
 *Internal method to insert into a subtree t.
 *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, BinaryNode * & 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
}

二叉查找树的删除例程
在处理具有两个儿子结点的情况时,一般是用其右子树的最小的数据代替该结点的数据并递归地删除那个结点。右子树中的最小的结点不可能有左儿子。

/**
 *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 of 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);
    else if(t->left != NULL && t->right != NULL) //Two children
    {
        t->element = findMin(t->right)->element;
        remove(t->element, t->right)
    }
    else  //deleting operation
    {
        BinaryNode *oldNode = t;
        t = (t->left != NULL) ? t->left : t->right;
        delete oldNode;
    }
}

这里写图片描述

这里写图片描述


析构函数和递归makeEmpty函数

/**
 *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;
}

复制赋值操作符和递归的clone成员函数

/**
 *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));
}

参考Mark Allen Weiss《数据结构与算法分析c++描述》第三版,仅供学习交流之用,转发请注明原出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值