#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Tree
{
int data;
struct Tree *lchild;
struct Tree *rchild;
struct Tree *parent;
}Bit_Search_Tree,*BST;
//**************************************************
BST Tree_Search(BST root,int e) //二叉树递归查找
{
BST p;
p=root;
if(p==NULL||p->data==e)
return p;
if(e<p->data)
return Tree_Search(p->lchild,e);
else
return Tree_Search(p->rchild,e);
}
//**************************************************
BST Tree_Search2(BST root,int e) //二叉树非递归查找
{
BST p;
p=root;
while(p&&p->data!=e)
{
if(e<p->data)
p=p->lchild;
else
p=p->rchild;
}
return p;
}
//***************************************************
BST Tree_MINIMUM(BST root) //求子树最小值
{
BST p;
p=root;
while(p->lchild)
p=p->lchild;
return p;
}
//***************************************************
BST Tree_MAXIMUM(BST root) //求子树最大值
{
BST p;
p=root;
while(p->rchild)
p=p->rchild;
return p;
}
//************************************************
void Tree_INSERT(BST *root,int e) //在子树中插入元素
{
BST x=NULL,y=NULL,z=NULL;
//(*root)->lchild=(*root)->rchild=NULL;
x=*root;
if(x==NULL)
{
(*root)=(BST)malloc(sizeof(Bit_Search_Tree));
(*root)->data=e;
(*root)->parent=(*root)->lchild=(*root)->rchild=NULL;
}
y=x;
while(x!=NULL)
{
y=x; //y是指示x的路线
if(e<x->data) //寻找插入位置
x=x->lchild;
else
x=x->rchild;
}
z=(BST)malloc(sizeof(Bit_Search_Tree)); //生成节点
z->data=e; //将值赋给节点
z->parent=y;
z->lchild=z->rchild=NULL;
if(y==NULL) //如果y为空,即这是棵子树
(*root)=z;
else //判断插入位置
{
if(z->data<y->data)
y->lchild=z;
else
y->rchild=z;
}
}
//****************************************************
BST Tree_SUCCESSOR(BST root,BST x) //求x的后继
{
BST y;
if(x->rchild!=NULL)
return Tree_MINIMUM(x->rchild);
else
{
y=x->parent;
while(y&&x==y->rchild)
{
x=y;
y=y->rchild;
}
return y;
}
}
//****************************************************
void Tree_DELETE(BST *root,BST z) //删除节点
{
if(z->lchild==NULL&&z->rchild==NULL) //如节点没有子女
{
if(z==z->parent->lchild) //父节点置0
z->parent->lchild=NULL;
if(z==z->parent->rchild)
z->parent->rchild=NULL;
free(z);
}
else if(z->lchild==NULL) //如果节点没有左子节点
{
BST t;
t=z;
z=z->rchild;
free(t);
}
else if(z->rchild==NULL) //如果节点没有右子节点
{
BST t;
t=z;
z=z->lchild;
free(t);
}
else //如果节点有左右子节点
{
BST q,s;
q=z; //寻找z的后继
s=z->rchild;
while(s->lchild!=NULL)
{
q=s;
s=s->lchild;
}
z->data=q->data; //将后继替换给待删除节点
if(q!=z)
q->lchild=s->rchild;
else
q->rchild=s->rchild;
free(s);
}
}
void DisplayBST(BST root) //输出二叉查找树
{
if(root->lchild!=NULL)
DisplayBST(root->lchild);
printf("%4d",root->data);
if(root->rchild!=NULL)
DisplayBST(root->rchild);
}
//****************************************************
int main()
{
BST tree=NULL,p=NULL;
int data;
printf("crate a bit-search-tree:\n");
for(int i=0;i<10;i++) //输入十个数插入建立一棵二叉查找树
{
printf("input the data of elem:\n");
scanf("%d",&data);
Tree_INSERT(&tree,data);
}
printf("BST:\n");
DisplayBST(tree);
printf("\n");
printf("input the data you want search:\n");
scanf("%d",&data);
p=Tree_Search(tree,data); //搜索值为data的节点
printf("delete it\n");
Tree_DELETE(&tree,p); //删除p
printf("BST\n");
DisplayBST(tree);
getch();
return 0;
}
二叉查找树(自己写的版本)
最新推荐文章于 2025-05-19 10:07:58 发布
本文详细介绍了二叉查找树的基本操作,包括插入、搜索、删除等,并通过实例展示了如何构建并操作二叉查找树。
1303

被折叠的 条评论
为什么被折叠?



