试写一个判别给定二叉树是否为二叉排序树的算法

/*2018.11
*数据结构与算法-第7章习题T3算法设计题
*(2)试写一个判别给定二叉树是否为二叉排序树的算法
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef int keyType;
typedef int infoType;

typedef struct
{
	keyType key;
	infoType otherinfo;
}elemtype;//结点的结构体类型

typedef struct bitreenode
{
	elemtype data;
	struct bitreenode *lchild,*rchild;
}bitreenode,*bitree;//二叉树的结构体类型

int storenum=0;//用于存储中序遍历时父节点的值
int flag=1;//用于判别父节点的值是否大于子节点的值

void createbitree(bitree &t)/*以先序遍历的顺序建立二叉树*/
{
	elemtype e;
	printf("请输入两个值,分别代表key和其他数据项:");
	scanf("%d%d",&e.key,&e.otherinfo);
	if(e.key==-9999)
		t=NULL;
	else
	{
		t=(bitree)malloc(sizeof(bitreenode));
		t->data=e;
		createbitree(t->lchild);
		createbitree(t->rchild);
	}
}

int isbstree(bitree t)/*判断是否为二叉排序树*/
{
	if(t->lchild && flag)
		isbstree(t->lchild);
	if(t->data.key<storenum)
		flag=0;//左子树一支中若父节点的值小于子节点,则flag为0
	storenum=t->data.key;//更新storenum的值
	if(t->rchild && flag)
		isbstre
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值