BST(线索二叉树实现)

来源:互联网 发布:2017知乎用户调查报告 编辑:程序博客网 时间:2024/05/21 19:50

BST树是学习树结构中最基础的数据结构,也是通向AVL树和RB树的基础,用来学习非常的不错。好了废话不多说,直接代码实现:
项目地址:这里

  BinarySearchTree.h  1 #ifndef _BINARYSEARCHTREE_H  2 #define _BINARYSEARCHTREE_H  3  4 #include<iostream>  5 #include<stdlib.h>  6 using namespace std;  7  8 template<class E,class K>  9 class BST; 10 11 template<class E,class K> 12 class BSTNode                       //二叉树结点类 13 { 14     friend class BST<E,K>; 15 private: 16     E   data;                       //数据域 17     BSTNode<E,K>    *left,*right;   //左子女和右子女 18 public: 19     BSTNode():left(NULL),right(NULL){}  //构造函数 20     BSTNode(const E d,BSTNode<E,K> *L = NULL,BSTNode<E,K> *R = NULL):data(d),left(L),right(R) 21     {}              //构造函数 22     ~BSTNode()      //析构函数 23     {} 24     void setNode(E d)       //修改 25     { 26         data = d; 27     } 28     E getData()             //提取 29     { 30         return data; 31     } 32 }; 33 34 template<class E,class K> 35 class BST               //二叉搜索树定义 36 {  37 38 private: 39     BSTNode<E,K> *root;         //二叉搜索树的根指针 40     K   RefValue;               //输入停止标志,用于输入 41     BSTNode<E,K> *Search(const K x,BSTNode<E,K> *ptr);  //递归:搜索 42     void makeEmpty(BSTNode<E,K> *& ptr);            //递归,置空 43     void PrintTree(BSTNode<E,K> *ptr)const;         //递归:打印 44     BSTNode<E,K>* Copy(const BSTNode<E,K> *ptr);    //递归:复制 45     BSTNode<E,K>* Min(BSTNode<E,K> *ptr)const;      //递归:求最小 46     BSTNode<E,K>* Max(BSTNode<E,K> *ptr)const;      //递归:求最大 47     bool Insert(const E& el,BSTNode<E,K> *& ptr);   //递归:插入 48     bool Remove(const K x,BSTNode<E,K> *& ptr);     //递归:删除 49 public: 50     BST():root(NULL)        //构造函数 51     {} 52     BST(K value);           //构造函数 53     ~BST()          //析构函数 54     {} 55     bool Search(const K x)const         //搜索 56     { 57         return (Search(x,root) != NULL) ? true : false; 58     } 59     BST<E,K>& operator = (const BST<E,K>& R);       //赋值 60     void makeEmpty()                //置空 61     { 62         makeEmpty(root); 63         root = NULL; 64     } 65     void PrintTree()const           //输出 66     { 67         PrintTree(root); 68     } 69     E Min()                         //求最小 70     { 71         return Min(root->data); 72     } 73     E Max()                         //求最大 74     { 75         return Max(root->data); 76     } 77     bool Insert(const E& el)        //插入新元素 78     { 79         return Insert(el,root); 80     } 81     bool Remove(const K x)          //删除含x的结点 82     { 83         return Remove(x,root); 84     } 85 }; 86 87 //////////////////////////////// 88 89 template<class E,class K> 90 bool BST<E,K>::Insert(const E& el,BSTNode<E,K> *& ptr)  //在以ptr为根的二叉搜索树中插入el的结点,若在>    树中已经含有el的结点,则不插入 91 { 92     if(ptr == NULL)                 //不存在结点 新结点作为叶子结点插入 93     { 94         ptr = new BSTNode<E,K>(el); 95         if(ptr == NULL) 96         { 97             cerr<<"Out of Memory"<<endl; 98             exit(1); 99         }100         return true;101     }102     else if(el < ptr->data)             //左子树插入103         Insert(el,ptr->left);104     else if(el > ptr->data)             //右子树插入105         Insert(el,ptr->right);106     else107         return false;108 }109110 template<class E,class K>111 BST<E,K>::BST(K value)              //输入一个元素序列,建立一棵二叉搜索树112 {113     E x;114     root = NULL;                //置空树115     RefValue = value;116     cout<<"Enter nuber:";117     cin>>x;                     //输入数据118     while(x != RefValue)119     {120         Insert(x);121         cin>>x;                 //插入再输入数据122     }123 }124125 template<class E,class K>126 void BST<E,K>::PrintTree(BSTNode<E,K> *ptr)const        //打印二叉树127 {128     if(ptr != NULL)129     {130         PrintTree(ptr->left);       //打印左子树131         cout<<ptr->data;            //打印数据132         PrintTree(ptr->right);      //打印右子树133     }134 }135136 template<class E,class K>137 BSTNode<E,K>* BST<E,K>::Search(const K x,BSTNode<E,K> *ptr)     //私有递归函数,灾异ptr为根的二叉搜索>    树中搜索含x的结点,若找到,则函数返回该结点的地址,否则函数返回NULL值138 {139     if(ptr == NULL)140         return NULL;141     else if(x < ptr->data)142         return Search(x,ptr->left);         //到左子树中继续寻找143     else if(x > ptr->data)144         return Search(x,ptr->right);        //到右子树中继续寻找145     else146         return ptr;                 //搜索成功147 }148149 template<class E,class K>150 bool BST<E,K>::Remove(const K x,BSTNode<E,K> *&ptr) //在以ptr为根的二叉树中删除含根x的结点,若删除成功    则新根通过ptr返回151 {152     BSTNode<E,K> *temp;153     if(ptr != NULL)154     {155         if(x < ptr->data)156             Remove(x,ptr->left);            //在左子树中执行操作157         else if(x > ptr->data)158             Remove(x,ptr->right);           //在右子树中执行操作159         else if(ptr->left != NULL && ptr->right != NULL)160         {   //要删除的结点有两个子女161             temp = ptr->right;162             while(temp->left != NULL)       //在右子树中找到最左的结点163                 temp = temp->left;164             ptr->data = temp->data;         //用其替换掉根结点数据165             Remove(ptr->data,ptr->right);   //删除其结点166         }167         else        //要删除的结点只有一个子女或者没有子女168         {169             temp = ptr;170             if(ptr->left == NULL)171                 ptr = ptr->right;172             else173                 ptr = ptr->left;174             delete temp;175             return true;176         }177     }178     return false;179 }180181 #endif

测试代码如下:

  1 #include"./BinarySearchTree.h"  4 int main()  5 {  6     BST<int,int> BSTree(0);  7     BSTree.PrintTree();  8     cout<<endl<<"----------"<<endl;  9     BSTree.Remove(4); 10    BSTree.PrintTree(); 11    return 0; 12 }

如果有问题的话,希望有人指出,共同进步。

原创粉丝点击