这是一个二叉搜索树的模板
主要功能有:
查找,插入,寻找树的最大节点,寻找树的最小节点,删除,先序输出
希望能对大家有所帮助
#include<bits/stdc++.h>
using namespace std;
struct Tree{
int data;
Tree*lchild;
Tree*rchild;
};Tree*find(Tree*bst,int value){//查找
//如果为空;
if(bst==NULL)return bst;
//当不为空时分两种情况,第一种比此节点值大,第二种比此节点值小。
else if(bst->data>value)return find(bst->lchild,value);
else if(bst->data<value)return find(bst->rchild,value);
else return bst;
}Tree*insertTree(Tree*bst,int value){//插入
//如果为空
if(bst==NULL){
bst=new Tree;
bst->data=value;
bst->rchild=NULL;
bst->lchild=NULL;
cout<<value<<"插入树中"<<endl;
return bst;
}if(value<bst->data)bst->lchild=insertTree(bst->lchild,value);
else if(value>bst->data)bst->rchild=insertTree(bst->rchild,value);
else cout<<value<<"已在树中&#