二叉搜索树的相关操作:插入节点,删除节点,排序,查找,最大值,最小值,前序,中序,后序遍历(主要用到递归的方法)

来源:互联网 发布:数据分析师月薪 编辑:程序博客网 时间:2024/05/22 06:04
#include<iostream>#include<assert.h>using namespace std;typedef int ElemType;typedef struct BSTNode//二叉搜索数的结点:左 右 值{ElemType data;BSTNode *LeftChild;BSTNode *RightChild;}BSTNode;typedef struct BSTree{BSTNode *root;}BSTree;void Init_BSTree(BSTree *bst);//初始化二叉搜索数(实现)bool insert(BSTree *bst,ElemType x);//插入节点(实现)bool insert(BSTNode *&t,ElemType x);ElemType max(BSTree *bst);//求最大值(实现)ElemType max(BSTNode *t);ElemType min(BSTree *bst);//求最小值(实现)ElemType min(BSTNode *t);void sort(BSTree *bst);//排序(实现)void sort(BSTNode *t);bool remove(BSTree *bst,ElemType x);//删除(实现)bool remove(BSTNode *&t,ElemType x);BSTNode* Find(BSTree *bst,ElemType x );//按值查找查找BSTNode* Find(BSTNode *t,ElemType x);void MidShowBSTree(BSTree *bst);//中序遍历显示(实现)void MidShowBSTree(BSTNode *t);void PreShowBSTree(BSTree *bst);//前序遍历显示(实现)void PreShowBSTree(BSTNode *t);void PostShowBSTree(BSTree *bst);//后序遍历结果显示(实现)void PostShowBSTree(BSTNode *t);BSTNode*  _BuyNode();//购买节点(实现)void main(){BSTree bst;BSTNode *p=NULL;Init_BSTree(&bst);int ar[]={23,45,19,22};int n=sizeof(ar)/sizeof(int);ElemType  maxdata,mindata;for(int i=0;i<n;i++){insert(&bst,ar[i]);//插入}//sort(&bst);//相当于中序遍历remove(&bst,23);cout<<"前序遍历结果为:"<<endl;PreShowBSTree(&bst);cout<<endl;cout<<"中序遍历结果为:"<<endl;MidShowBSTree(&bst);cout<<endl;cout<<"后序遍历结果为:"<<endl;PostShowBSTree(&bst);maxdata=max(&bst);mindata=min(&bst);cout<<endl;cout<<"最大数据为:"<<maxdata<<endl;cout<<"最小数据为:"<<mindata<<endl;p=Find(&bst,22);if(p==NULL){cout<<"没有找到"<<endl;}elsecout<<"找到"<<endl;}void Init_BSTree(BSTree *bst){bst->root=NULL;//初始化,让根节点为空}BSTNode* _BuyNode(ElemType x){BSTNode *s=(BSTNode*)malloc(sizeof(BSTNode));assert(s!=NULL);s->data=x;s->LeftChild=NULL;s->RightChild=NULL;return s;}bool insert(BSTree *bst,ElemType x){return insert(bst->root,x);}bool insert(BSTNode *&t,ElemType x){if(t==NULL){t=_BuyNode(x);   return true;}else if(t->data<x){insert(t->RightChild,x);}else if(t->data>x){insert(t->LeftChild,x);}return false;}ElemType max(BSTree *bt){return max(bt->root);}ElemType max(BSTNode *t){if(t==NULL)return -1;else{while(t->RightChild!=NULL){t=t->RightChild;}return t->data;}}ElemType min(BSTree *bt){return min(bt->root);}ElemType min(BSTNode *t){if(t==NULL)return -1;else{while(t->LeftChild!=NULL){t=t->LeftChild;}return t->data;}}void sort(BSTree *bst){sort(bst->root);}void sort(BSTNode  *t){if(t==NULL)return;else{sort(t->LeftChild);cout<<t->data<<" ";sort(t->RightChild);}}bool remove(BSTree *bt,ElemType x){return remove(bt->root,x);}bool remove(BSTNode *&t,ElemType x){if(t==NULL)return false;if(x<t->data)remove(t->LeftChild,x);else if(x>t->data)remove(t->RightChild,x);else//相等,找到,找到的情况分为三种:找到的节点有左右子树,找到的节点有左子树或者右子树,找到的节点既没有左子树也没有右子树{BSTNode *p=NULL;if(t->LeftChild!=NULL&&t->RightChild!=NULL){p=t->RightChild;while(p->LeftChild!=NULL){p=p->LeftChild;}t->data=p->data;remove(t->RightChild,p->data);}else{p=t;if(t->LeftChild==NULL){t=t->RightChild;}else if(t->RightChild==NULL){t=t->LeftChild;}free(p);p=NULL;}}return true;}void MidShowBSTree(BSTree *bst){MidShowBSTree(bst->root);}void MidShowBSTree(BSTNode *t){if(t==NULL)return;else{MidShowBSTree(t->LeftChild);cout<<t->data<<" ";MidShowBSTree(t->RightChild);}}void PreShowBSTree(BSTree *bst){PreShowBSTree(bst->root);}void PreShowBSTree(BSTNode *t){if(t==NULL)return;else{cout<<t->data<<" ";PreShowBSTree(t->LeftChild);PreShowBSTree(t->RightChild);}}void PostShowBSTree(BSTree *bt){PostShowBSTree(bt->root);}void PostShowBSTree(BSTNode *t){if(t==NULL){return;}else{PostShowBSTree(t->LeftChild);cout<<t->data<<" ";PostShowBSTree(t->RightChild);}}BSTNode* Find(BSTNode* t,ElemType x){if(t==NULL)return NULL;else{if(t->data>x)Find(t->LeftChild,x);else if(t->data<x){Find(t->RightChild,x);}elsereturn t;}}BSTNode* Find(BSTree *bt,ElemType x){return Find(bt->root,x);}

0 0
原创粉丝点击