#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <queue> #include <stack> using namespace std; typedef struct BstNode { int data; BstNode *lchild, *rchild; }BstNode,*BstTree; void Bst_insert(BstTree &T,int z)//建树 { if(T==NULL) { T=(BstTree)malloc(sizeof(BstNode)); T->lchild=NULL; T->rchild=NULL; T->data=z; } else { if(T->data!=z) if(T->data>z) Bst_insert(T->lchild,z); else Bst_insert(T->rchild,z); } } void Bst_preorder(BstTree T) //中序递归遍历 { if(T!=NULL) { Bst_preorder(T->lchild); printf("%d\n",T->data); Bst_preorder(T->rchild); } } bool Bst_exist(BstTree T,int z)//寻找某数值是否存在 { if(T==NULL) return false; if(T->data==z) return true; if(T->data>z) Bst_exist(T->lchild,z); else Bst_exist(T->rchild,z); } BstTree Bst_search(BstTree T,int z)//寻找某数值的节点 { if(T==NULL) return NULL; if(T->data==z) return T; if(T->data>z) Bst_search(T->lchild,z); else Bst_search(T->rchild,z); } BstTree Bst_min(BstTree T)//返回最小值 { if(T==NULL) return NULL; BstTree y=T; while(y->lchild!=NULL) y=y->lchild; return y; } BstTree Bst_max(BstTree T)//返回最大值 { if(T==NULL) return NULL; BstTree y=T; while(y->rchild!=NULL) y=y->rchild; return y; } BstTree Bst_parents(BstTree T,BstTree z)//寻找某节点的父节点 { if(T==NULL) return NULL; BstTree y=T; if(y->data>z->data) { if(y->lchild->data==z->data) return y; else return Bst_parents(y->lchild,z); } else { if(y->rchild->data==z->data) return y; else return Bst_parents(y->rchild,z); } } void Bst_delete(BstTree &T,int z)//删除节点 { BstTree notez=Bst_search(T,z); BstTree parent,y; if(notez->lchild==NULL)//左为空 { if(T->data!=z) { parent=Bst_parents(T,notez); if(parent->lchild==notez) parent->lchild=notez->rchild; else parent->rchild=notez->rchild; } else T=T->rchild; } else if(notez->rchild==NULL)//右为空 { if(T->data!=z) { parent=Bst_parents(T,notez); if(parent->lchild==notez) parent->lchild=notez->lchild; else parent->rchild=notez->lchild; } else T=T->lchild; } else//左右都不为空 { y=Bst_min(notez->rchild); parent=Bst_parents(T,y); if(y->rchild==NULL) //当最小点为叶子节点的时候 if(parent->lchild==y) parent->lchild=NULL; else parent->rchild=NULL; else//当最小点还有右节点的时候,需要把右树贴到最小点的位置 { parent->lchild=y->rchild; } y->lchild=notez->lchild; y->rchild=notez->rchild; if(T->data!=z) { parent=Bst_parents(T,notez); if(parent->lchild==notez) parent->lchild=y; else parent->rchild=y; } else T=y; } } void Bst_PreOrderTraverse(BstTree T) { stack<BstTree> stack; if(!T) { printf("空树!\n"); return; } while(T!=NULL || !stack.empty()) { while(T!=NULL) { stack.push(T); printf("%c",T->data); T=T->lchild; } T=stack.top(); stack.pop(); T=T->rchild; } } void Bst_InOrderTraverse(BstTree T) { stack<BstTree> stack; if(!T) { printf("空树!\n"); return; } while(T!=NULL || !stack.empty())//当树的右端为空的时候,栈有东西可以开始吐出来 { while(T!=NULL) { stack.push(T); T=T->lchild; } T=stack.top(); stack.pop(); printf("%d\n",T->data); T=T->rchild; } } void Bst_PostOrderTraverse(BstTree T) { int flag[20]; stack<BstTree> stack; if(!T) { printf("空树!\n"); return; } while(T) { stack.push(T); flag[stack.size()]=0; T=T->lchild; } while(!stack.empty()) { T=stack.top(); while(T->rchild && flag[stack.size()]==0) { flag[stack.size()]=1; T=T->rchild; while(T) { stack.push(T); flag[stack.size()]=0; T=T->lchild; } T=stack.top(); } printf("%c",T->data); stack.pop(); } } void main() { BstTree T=NULL; Bst_insert(T,10); Bst_insert(T,6); Bst_insert(T,12); Bst_insert(T,3); Bst_insert(T,7); Bst_insert(T,11); Bst_insert(T,13); Bst_delete(T,3); Bst_preorder(T); Bst_InOrderTraverse(T); }
Bst的基本操作
这篇博客介绍了如何实现二叉搜索树(BST)的基本操作,包括插入、中序遍历、查找、删除以及找到最小和最大值的节点。还提供了C++代码示例来展示这些操作的具体实现。
摘要由CSDN通过智能技术生成