二叉排序树
作者:互联网
声明:图片及内容基于https://www.bilibili.com/video/BV15f4y197v1?from=articleDetail
二叉排序树概念
二叉树的定义
类的定义
class BiSortTree{ private: BiNode *root; public: BiSortTree(DataType array[],int arrayLength); ~BiSortTree(){ release(root);} BiNode* searchBST(int key){ searchBST(root,key);} bool deleteBST(int key){ return deleteBST(root,key);} void inOrder(){ inOrder(root);} private: void insertBST(BiNode* &bt, DataType key); //插入 void release(BiNode *bt); //二叉树销毁 void inOrder(BiNode *bt); //中序遍历 bool deleteBST(BiNode* &bt,DataType key); //删除 void deleteNode(BiNode* &bt); //删除节点 BiNode *searchBST(BiNode *bt,DataType key); //查找 };
二叉树的插入
void BiSortTree::insertBST(BiNode* &bt,DataType key){ if(bt==NULL){ //二叉树为空时直接创建节点 bt=new BiNode; bt->data=key; bt->lchild=NULL; bt->rchild=NULL; } else{ if(key<bt->data) //左小右大 insertBST(bt->lchild,key); if(key>bt->data) insertBST(bt->rchild,key); } }
二叉树的构造
BiSortTree::BiSortTree(DataType array[],int length){ //循环建立二叉排序树 root=NULL; for(int i=0;i<length;i++){ insertBST(root,array[i]); } }
二叉树的查找
BiNode* BiSortTree::searchBST(BiNode *bt,DataType key){ if(bt==NULL) //递归终止条件 return NULL; else{ if(key==bt->data) //找到返回 return bt; else if(key<bt->data) //左小 return searchBST(bt->lchild,key); else //右大 return searchBST(bt->rchild,key); } }
二叉树的删除
bool BiSortTree::deleteBST(BiNode* &bt,DataType key){ if(bt==NULL) //递归终止条件 return false; else{ if(bt->data==key) //找到删除 deleteNode(bt); else if(key<bt->data) //左小 return deleteBST(bt->lchild,key); else //右大 return deleteBST(bt->rchild,key); return true; } } void BiSortTree::deleteNode(BiNode* &bt){ BiNode *p; if(bt->lchild==NULL&&bt->rchild==NULL){ //叶子节点 p=bt; bt=NULL; delete p; } else if(bt->rchild==NULL){ //右子树为空 p=bt; bt=bt->lchild; delete p; } else if(bt->lchild==NULL){ //左子树为空 p=bt; bt=bt->rchild; delete p; } else{ //左右子树均不为空 BiNode *parent=bt; BiNode *pre=bt->lchild; while(pre->rchild){ //找到左子树中最大的结点 parent=pre; pre=pre->rchild; } bt->data=pre->data; //pre指向要删结点的前驱,替换bt数据 if(parent!=bt) //判断是否执行了上述while循环 parent->rchild=pre->lchild; //执行了while循环,重新接pre右子树 else parent->lchild=pre->lchild; //未执行while循环,重接pre左子树 delete pre; } }
完整代码
#include<iostream> #define DataType int using namespace std; struct BiNode{ DataType data; BiNode *lchild; BiNode *rchild; }; class BiSortTree{ private: BiNode *root; public: BiSortTree(DataType array[],int arrayLength); ~BiSortTree(){ release(root);} BiNode* searchBST(int key){ searchBST(root,key);} bool deleteBST(int key){ return deleteBST(root,key);} void inOrder(){ inOrder(root);} private: void insertBST(BiNode* &bt, DataType key); //插入 void release(BiNode *bt); //二叉树销毁 void inOrder(BiNode *bt); //中序遍历 bool deleteBST(BiNode* &bt,DataType key); //删除 void deleteNode(BiNode* &bt); //删除节点 BiNode *searchBST(BiNode *bt,DataType key); //查找 }; void BiSortTree::inOrder(BiNode* bt){ //中序遍历 if(bt==NULL) return; else{ inOrder(bt->lchild); cout<<bt->data<<" "; inOrder(bt->rchild); } } void BiSortTree::release(BiNode *bt){ if (bt != NULL) { release(bt->lchild); //后序遍历销毁二叉树 release(bt->rchild); delete bt; bt = NULL; } } void BiSortTree::insertBST(BiNode* &bt,DataType key){ if(bt==NULL){ //二叉树为空时直接创建节点 bt=new BiNode; bt->data=key; bt->lchild=NULL; bt->rchild=NULL; } else{ if(key<bt->data) //左小右大 insertBST(bt->lchild,key); if(key>bt->data) insertBST(bt->rchild,key); } } BiSortTree::BiSortTree(DataType array[],int length){ //循环建立二叉排序树 root=NULL; for(int i=0;i<length;i++){ insertBST(root,array[i]); } } BiNode* BiSortTree::searchBST(BiNode *bt,DataType key){ if(bt==NULL) //递归终止条件 return NULL; else{ if(key==bt->data) //找到返回 return bt; else if(key<bt->data) //左小 return searchBST(bt->lchild,key); else //右大 return searchBST(bt->rchild,key); } } bool BiSortTree::deleteBST(BiNode* &bt,DataType key){ if(bt==NULL) //递归终止条件 return false; else{ if(bt->data==key) //找到删除 deleteNode(bt); else if(key<bt->data) //左小 return deleteBST(bt->lchild,key); else //右大 return deleteBST(bt->rchild,key); return true; } } void BiSortTree::deleteNode(BiNode* &bt){ BiNode *p; if(bt->lchild==NULL&&bt->rchild==NULL){ //叶子节点 p=bt; bt=NULL; delete p; } else if(bt->rchild==NULL){ //右子树为空 p=bt; bt=bt->lchild; delete p; } else if(bt->lchild==NULL){ //左子树为空 p=bt; bt=bt->rchild; delete p; } else{ //左右子树均不为空 BiNode *parent=bt; BiNode *pre=bt->lchild; while(pre->rchild){ //找到左子树中最大的结点 parent=pre; pre=pre->rchild; } bt->data=pre->data; //pre指向要删结点的前驱,替换bt数据 if(parent!=bt) //判断是否执行了上述while循环 parent->rchild=pre->lchild; //执行了while循环,重新接pre右子树 else parent->lchild=pre->lchild; //未执行while循环,重接pre左子树 delete pre; } } int main(){ int length; int *array; cout<<"请输入数组长度"<<endl; cin>>length; cout<<"请输入"<<length<<"个数据"<<endl; array=new int[length]; for(int i=0;i<length;i++) cin>>array[i]; BiSortTree bitree(array,length); cout<<"中序遍历结果:"<<endl; bitree.inOrder(); cout<<endl; cout<<"查找34"<<(bitree.searchBST(34)?"成功":"失败")<<endl; cout<<endl; cout<<"删除40:"<<(bitree.deleteBST(40)?"成功":"失败")<<endl; cout<<"删除34:"<<(bitree.deleteBST(34)?"成功":"失败")<<endl; cout<<"删除13:"<<(bitree.deleteBST(13)?"成功":"失败")<<endl; cout<<"删除38:"<<(bitree.deleteBST(38)?"成功":"失败")<<endl; cout<<"删除12:"<<(bitree.deleteBST(12)?"成功":"失败")<<endl; cout<<"删除4:"<<(bitree.deleteBST(4)?"成功":"失败")<<endl; return 0; }
输入:
10
38 12 34 56 13 6 98 17 40 78
输出:
中序遍历结果:
6 12 13 17 34 38 40 56 78 98
查找34成功
删除40:成功
删除34:成功
删除13:成功
删除38:成功
删除12:成功
删除4:失败
标签:lchild,BiNode,pre,NULL,二叉,bt,key,排序 来源: https://www.cnblogs.com/gonghr/p/14669586.html