二叉搜索树的相关操作:插入节点,删除节点,排序,查找,最大值,最小值,前序,中序,后序遍历(主要用到递归的方法)
来源:互联网 发布:数据分析师月薪 编辑:程序博客网 时间: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
- 二叉搜索树的相关操作:插入节点,删除节点,排序,查找,最大值,最小值,前序,中序,后序遍历(主要用到递归的方法)
- 二叉树的遍历 插入 查找 删除 最大值 最小值 前驱 后继节点的查找
- 二叉树的查找,插入,删除,遍历,最小值,最大值 操作
- 数据结构(C#)--二叉查找树的先序,中序,后序的遍历问题以及最大值,最小值,插入,删除
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- 二叉排序树,完成创建节点,插入节点,删除节点,查找节点,中序遍历的功能
- 二叉查找树带父节点指针的创建,销毁,查找,删除,插入,找前驱后继,找最小值最大值(递归和非递归的实现)
- 初探二叉树之动态创建,前.中,后序遍历的递归及非递归实现,层次遍历,树状输出,叶节点及节点总数的统计
- 二叉排序数(创建,插入,删除,查找及前序、中序、后序遍历)
- 【数据结构树表的查找】二叉排序树详解和代码(生成、插入、查找、最大值、最小值、删除、中序遍历、销毁)
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
- 二叉树的各种操作:前序、中序、后序、层序遍历,二叉树搜索、插入和删除等操作
- 二叉树的创建 前序 中序 后序遍历以及最大值与最小值的差
- 二叉树的相关操作:创建、查找、求高度和深度、各种遍历(前、中、后、层序)等等
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 二叉搜索树的节点插入,查找。
- 二叉树插入、删除、递归求高度、度、前序、中序、后序遍历
- jsp request乱码
- 10种使测试人员陷入困境的行为趋势
- 反射相关
- JZOJ 3506. 【NOIP2013模拟11.4A组】善良的精灵
- SQ的工具应用代码
- 二叉搜索树的相关操作:插入节点,删除节点,排序,查找,最大值,最小值,前序,中序,后序遍历(主要用到递归的方法)
- 计算机二级复习趣谈
- SpringMVC与Struts2的区别
- Android SQLite 数据库增删改查dos命令行语句
- 年终奖(DP)
- MySql配置、介绍
- CSU-1007
- NY45---棋盘覆盖
- C++新特性