2008秋-计算机软件基础- 第四章- 二叉排序树中查找
/*---------------------------------------------
Title: 二叉排序树中查找 Author: eman lee
----------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
//定义二叉树的节点结构
struct node
{
int data;//存放节点数据
struct node * lchild,* rchild;//指向左子树和右子树的指针变量
};
//中序遍历二叉树
void MiddleOrder (struct node *q)
{
if (q!=NULL)
{
MiddleOrder(q->lchild); /*中序遍历左子树*/
printf("%d ",q->data); /*访问根结点*/
MiddleOrder(q->rchild); /*中序遍历右子树*/
}
}
//二叉排序树中插入节点
void InsertNode(struct node *p,struct node * pn) /*插入一个新结点的算法*/
{
if(pn->data<p->data)//小于根节点
{
if (p->lchild==NULL)//左子树为空
p->lchild=pn;
else
InsertNode(p->lchild,pn);
}
else //大于或等于根节点
{
if (p->rchild==NULL) //右子树为空
p->rchild=pn;
else
InsertNode(p->rchild,pn);
}
}
//二叉排序树的生成算法
struct node * CreateBinSortTree()
{
int x;//用于暂时存放输入的数值
struct node *t;//指向根节点的指针变量
struct node *s;//指向新插入节点的指针变量
t=NULL;//最初为空树
printf("请输入结点的值(整数,用空格隔开),当输入-1时结束.\n");
scanf("%d",&x);
while(x!=-1)
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
s->lchild=s->rchild=NULL;
if (t==NULL)
t=s;
else
InsertNode(t,s);
scanf("%d",&x);
}
return (t);
}
struct node * bstsearch(struct node * t, int x)
/*在根结点为*t的二叉排序树中查找关键字为x的元素*/
{
while(t!=NULL)
{
if(x==t->data) return t;
/*查找成功时返回该结点的指针*/
else
{
if(x<t->data) t=t->lchild;
/*当待查值小于根结点关键字值时在左子树中查找*/
else t=t->rchild;
}
/*当待查值大于关键字值时在右子树中查找*/
}
return NULL; /*查找失败时返回空指针*/
} /*bstsearch*/
void main()
{
struct node * root;//定义指向根节点的指针变量
printf("创建二叉排序树:\n");
root= CreateBinSortTree();//创建二叉排序树
printf("遍历二叉排序树:\n");
MiddleOrder(root);//遍历二叉排序树
if(bstsearch(root,5)!=NULL)
printf("\n Found 5 \n");
else
printf("\n Not Found 5\n");
printf("\n");
}
Title: 二叉排序树中查找 Author: eman lee
----------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
//定义二叉树的节点结构
struct node
{
int data;//存放节点数据
struct node * lchild,* rchild;//指向左子树和右子树的指针变量
};
//中序遍历二叉树
void MiddleOrder (struct node *q)
{
if (q!=NULL)
{
MiddleOrder(q->lchild); /*中序遍历左子树*/
printf("%d ",q->data); /*访问根结点*/
MiddleOrder(q->rchild); /*中序遍历右子树*/
}
}
//二叉排序树中插入节点
void InsertNode(struct node *p,struct node * pn) /*插入一个新结点的算法*/
{
if(pn->data<p->data)//小于根节点
{
if (p->lchild==NULL)//左子树为空
p->lchild=pn;
else
InsertNode(p->lchild,pn);
}
else //大于或等于根节点
{
if (p->rchild==NULL) //右子树为空
p->rchild=pn;
else
InsertNode(p->rchild,pn);
}
}
//二叉排序树的生成算法
struct node * CreateBinSortTree()
{
int x;//用于暂时存放输入的数值
struct node *t;//指向根节点的指针变量
struct node *s;//指向新插入节点的指针变量
t=NULL;//最初为空树
printf("请输入结点的值(整数,用空格隔开),当输入-1时结束.\n");
scanf("%d",&x);
while(x!=-1)
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
s->lchild=s->rchild=NULL;
if (t==NULL)
t=s;
else
InsertNode(t,s);
scanf("%d",&x);
}
return (t);
}
struct node * bstsearch(struct node * t, int x)
/*在根结点为*t的二叉排序树中查找关键字为x的元素*/
{
while(t!=NULL)
{
if(x==t->data) return t;
/*查找成功时返回该结点的指针*/
else
{
if(x<t->data) t=t->lchild;
/*当待查值小于根结点关键字值时在左子树中查找*/
else t=t->rchild;
}
/*当待查值大于关键字值时在右子树中查找*/
}
return NULL; /*查找失败时返回空指针*/
} /*bstsearch*/
void main()
{
struct node * root;//定义指向根节点的指针变量
printf("创建二叉排序树:\n");
root= CreateBinSortTree();//创建二叉排序树
printf("遍历二叉排序树:\n");
MiddleOrder(root);//遍历二叉排序树
if(bstsearch(root,5)!=NULL)
printf("\n Found 5 \n");
else
printf("\n Not Found 5\n");
printf("\n");
}