二叉查找树的类模板实现

用c++实现了一个BinarySearchTree的模板类

 

其他都很简单,重点说一下删除结点的方法。

如果结点是一片叶子,那么可以立即被删除;如果结点只有一个左儿子或一个右儿子,则左儿子或右儿子代替结点位置即可;

复杂的情况是有两个儿子,可以用被删除节点A的左子树的最右节点或者A的右子树的最左节点作为替代A的节点,并修改相应的最左或最右节点的父节点的指针。下面采用的是用右子树的最小数据(即右子树的最左结点)代替该结点的数据并递归地删除那个结点。

void BinarySearchTree<Comparable>::Remove(const Comparable &x, BinaryNode *&t) const
{
    if (t == NULL)
        return;
    if (x < t->element)
        Remove(x, t->left);
    else if (x > t->element)
        Remove(x, t->right);
    else if (t->left != NULL && t->right != NULL)//两个儿子的结点
    {
        t->element = FindMin(t->right)->element;
        Remove(t->element, t->right);
    }
    else
    {
        BinaryNode *oldNode = t;
        t = (t->left != NULL) ? t->left : t->right;
        delete oldNode;
    }
}

 

 

全部代码:

BinarySearchTree.h

  1 #include <iostream>
  2 using namespace std;
  3 
  4 #pragma once
  5 
  6 template <typename Comparable>
  7 class BinarySearchTree
  8 {
  9 public:
 10     BinarySearchTree();
 11     BinarySearchTree(const BinarySearchTree &rhs);
 12     ~BinarySearchTree();
 13 
 14     const Comparable &FindMin() const;
 15     const Comparable &FindMax() const;
 16     bool Contains(const Comparable &x) const;
 17     bool IsEmpty() const;
 18     void PrintTree() const;
 19 
 20     void MakeEmpty();
 21     void Insert(const Comparable &x);
 22     void Remove(const Comparable &x);
 23 
 24     const BinarySearchTree & operator=(const BinarySearchTree &rhs);
 25 
 26 private:
 27     struct BinaryNode
 28     {
 29         Comparable element;
 30         BinaryNode *left;
 31         BinaryNode *right;
 32 
 33         BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt)
 34             :element(theElement), left(lt), right(rt){}
 35     };
 36     BinaryNode *root;
 37     void Insert(const Comparable &x, BinaryNode *&t) const;
 38     void Remove(const Comparable &x, BinaryNode *&t) const;
 39     BinaryNode * FindMin(BinaryNode *t) const;
 40     BinaryNode * FindMax(BinaryNode *t) const;
 41     bool Contains(const Comparable &x, BinaryNode *t) const;
 42     void MakeEmpty(BinaryNode *&t);
 43     void PrintTree(BinaryNode *t) const;
 44     BinaryNode * Clone(BinaryNode *t) const;
 45 }; 
 46 
 47 
 48 
 49 template <typename Comparable>
 50 void BinarySearchTree<Comparable>::PrintTree() const
 51 {
 52     PrintTree(root);
 53 }
 54 
 55 template <typename Comparable>
 56 void BinarySearchTree<Comparable>::MakeEmpty(BinaryNode *&t)
 57 {
 58     if (t != NULL)
 59     {
 60         MakeEmpty(t->left);
 61         MakeEmpty(t->right);
 62         delete t;
 63     }
 64     t = NULL;
 65 }
 66 
 67 template <typename Comparable>
 68 BinarySearchTree<Comparable>::BinarySearchTree()
 69 {
 70 }
 71 
 72 template <typename Comparable>
 73 BinarySearchTree<Comparable>::~BinarySearchTree()
 74 {
 75     MakeEmpty(root);
 76 }
 77 
 78 template <typename Comparable>
 79 BinarySearchTree<Comparable>::BinarySearchTree(const BinarySearchTree &rhs)
 80 {
 81 
 82 }
 83 
 84 template <typename Comparable>
 85 bool BinarySearchTree<Comparable>::Contains(const Comparable &x) const
 86 {
 87     return Contains(x, root);
 88 }
 89 
 90 
 91 template <typename Comparable>
 92 void BinarySearchTree<Comparable>::Insert(const Comparable &x)
 93 {
 94     return Insert(x, root);
 95 }
 96 
 97 template <typename Comparable>
 98 void BinarySearchTree<Comparable>::Remove(const Comparable &x)
 99 {
100     return Remove(x, root);
101 }
102 
103 template <typename Comparable>
104 const Comparable & BinarySearchTree<Comparable>::FindMax() const
105 {
106     return FindMax(root)->element;
107 }
108 
109 template <typename Comparable>
110 const Comparable & BinarySearchTree<Comparable>::FindMin() const
111 {
112     return FindMin(root)->element;
113 }
114 
115 template <typename Comparable>
116 void BinarySearchTree<Comparable>::PrintTree(BinaryNode *t) const
117 {
118     if (t != NULL)
119     {
120         PrintTree(t->left);
121         std::cout << t->element << ' ';
122         PrintTree(t->right);
123     }
124 }
125 
126 template <typename Comparable>
127 bool BinarySearchTree<Comparable>::Contains(const Comparable &x, BinaryNode *t) const
128 {
129     if (t == NULL)
130         return false;
131     else if (x < t->element)
132         return Contains(x, t->left);
133     else if (x > t->element)
134         return Contains(x, t->right);
135     else
136         return true;//匹配到
137 }
138 
139 
140 template <typename Comparable>
141 void BinarySearchTree<Comparable>::Insert(const Comparable &x, BinaryNode *&t) const
142 {
143     if (t == NULL)
144         t = new BinaryNode(x, NULL, NULL);
145     else if (x < t->element)
146         Insert(x, t->left);
147     else if (x > t->element)
148         Insert(x, t->right);
149     else
150         return;//重复,不插入
151 
152 }
153 
154 template <typename Comparable>
155 typename BinarySearchTree<Comparable>::BinaryNode * BinarySearchTree<Comparable>::FindMax(BinaryNode *t) const
156 {
157     if (t == NULL)
158         return NULL;
159     else if (t->right == NULL)
160         return t;
161     else
162         return FindMax(t->right);
163 }
164 
165 template <typename Comparable>
166 typename BinarySearchTree<Comparable>::BinaryNode * BinarySearchTree<Comparable>::FindMin(BinaryNode *t) const
167 {
168     if (t == NULL)
169         return NULL;
170     else if (t->left == NULL)
171         return t;
172     else
173         return FindMin(t->left);
174 }
175 
176 template <typename Comparable>
177 void BinarySearchTree<Comparable>::Remove(const Comparable &x, BinaryNode *&t) const
178 {
179     if (t == NULL)
180         return;
181     if (x < t->element)
182         Remove(x, t->left);
183     else if (x > t->element)
184         Remove(x, t->right);
185     else if (t->left != NULL && t->right != NULL)//两个儿子的结点
186     {
187         t->element = FindMin(t->right)->element;
188         Remove(t->element, t->right);
189     }
190     else
191     {
192         BinaryNode *oldNode = t;
193         t = (t->left != NULL) ? t->left : t->right;
194         delete oldNode;
195     }
196 }
197 
198 
199 template <typename Comparable>
200 const BinarySearchTree<Comparable> & BinarySearchTree<Comparable>::operator=(const BinarySearchTree<Comparable> &rhs)
201 {
202     if (this != &rhs)
203     {
204         MakeEmpty();
205         root = Clone(rhs.root);
206     }
207     return *this;
208 }
209 
210 
211 template <typename Comparable>
212 typename BinarySearchTree<Comparable>::BinaryNode * BinarySearchTree<Comparable>::Clone(BinaryNode *t) const
213 {
214     if (t == NULL)
215         return NULL;
216     return new BinaryNode(t->element, Clone(t->left), Clone(t->right));
217 }

 

BinarySearchTree.cpp

#include "BinarySearchTree.h"


int main()
{
    int value;
    int tmp;
    BinarySearchTree<int> tree;
    cout << "请输入整数建立二叉树(-1结束):" << endl;
    while (cin >> value)
    {
        if (value == -1)
            break;
        tree.Insert(value);
    }
    cout << "二叉树中序遍历如下:";
    tree.PrintTree();
    cout << "\n最大值:" << tree.FindMax() << endl;
    cout << "最小值:" << tree.FindMin() << endl;
    cout << "请输入要查找的值:";
    cin >> tmp;
    if (tree.Contains(tmp))
        cout << "查找到结点" << endl;
    else
        cout << "未找到结点" << endl;
    cout << "请输入要查找的值:";
    cin >> tmp;
    if (tree.Contains(tmp))
        cout << "查找到结点";
    else
        cout << "未找到结点";
    cout << "请输入要删除的值:";
    cin >> tmp;
    tree.Remove(tmp);
    cout << "已删除,删除后的二叉树为:";
    tree.PrintTree();
}

 

测试结果:

posted @ 2016-01-25 23:57  zhangbaochong  阅读(1483)  评论(2编辑  收藏  举报