判断二叉树是否为二叉排序树

本文介绍了一种判断二叉树是否为二叉排序树的方法,并提供了完整的C语言实现代码。通过创建二叉树和二叉排序树,演示了如何使用中序遍历来判断树的性质。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断二叉树是否为二叉排序树

输入的原始数组为,a[7]={49,99,65,97,76,13,27},可以先在草稿纸上模拟,再看程序
实现代码:
#include <stdio.h>
#include <stdlib.h>
int predit=-1000;//保存当前节点中序前驱值,开始假设为最小值 
typedef struct BTNode{
	int data;
	struct BTNode *lchild,*rchild;
}BTNode;
//创建二叉排序树 
int BSTInsert(BTNode *&bt,int x){
	if(bt==NULL){
		bt=(BTNode*)malloc(sizeof(BTNode));
		bt->data=x;
		bt->lchild=bt->rchild=NULL;
	}else if(bt->data==x){
		return 0;
	}else if(bt->data>x){
		return BSTInsert(bt->lchild,x);
	}else{
		return BSTInsert(bt->rchild,x);
	}
}
//创建二叉树 
int CreateBTree(BTNode *&tree,int x){
	if(tree==NULL){
		tree=(BTNode *)malloc(sizeof(BTNode));
		tree->data=x;
		tree->rchild=tree->lchild=NULL;
	}else {
		CreateBTree(tree->lchild,x);
		CreateBTree(tree->rchild,x);
	}
} 
//判断二叉树是否为二叉排序树 
int JudgeBST(BTNode *bt){
	int b1,b2;
	if(bt==NULL){
		return 1;
	}else{
		b1=JudgeBST(bt->lchild);
		if(b1=0||predit>bt->data){
			return 0;
		}
		predit=bt->data;
		b2=JudgeBST(bt->rchild);
			return b2;
	}
}
int main(int argc, char *argv[])
{
	struct BTNode *bt1=NULL,*bt2=NULL;
	int i,a[7]={49,99,65,97,76,13,27};
 	for(i=0;i<7;++i){
	 	BSTInsert(bt1,a[i]);
	 	CreateBTree(bt2,a[i]);
	 }
 	printf("%d\n",JudgeBST(bt1));//输出1表示是二叉排序树 
 	printf("%d\n",JudgeBST(bt2));//输出0表示不是 
	return 0;
}
输出结果:
1
0
请按任意键继续. . .



好的,我来为您介绍如何使用 C 语言建立二叉排序树二叉排序树也称为二叉查找树,是一种特殊的二叉树结构。它的每个节点都包含一个键值,而且对于任何节点,它的左子树中所有节点的键值都小于该节点的键值,而它的右子树中所有节点的键值都大于该节点的键值。 以下是使用 C 语言建立二叉排序树的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉排序树的节点结构体 struct node { int key; struct node *left, *right; }; // 插入节点函数 struct node* insert(struct node* node, int key) { // 如果树为空,则新建一个节点 if (node == NULL) { node = (struct node*) malloc(sizeof(struct node)); node->key = key; node->left = node->right = NULL; } // 如果插入的值小于当前节点的值,则插入到左子树中 else if (key < node->key) { node->left = insert(node->left, key); } // 如果插入的值大于等于当前节点的值,则插入到右子树中 else { node->right = insert(node->right, key); } return node; } // 中序遍历函数,按升序输出二叉排序树中的所有节点 void inorder(struct node* node) { if (node != NULL) { inorder(node->left); printf("%d ", node->key); inorder(node->right); } } int main() { struct node *root = NULL; int keys[] = {5, 3, 7, 1, 4, 6, 8, 2}; int n = sizeof(keys) / sizeof(keys[0]); // 将所有键值插入到二叉排序树中 for (int i = 0; i < n; i++) { root = insert(root, keys[i]); } // 按升序输出二叉排序树中的所有节点 inorder(root); return 0; } ``` 在上面的代码中,我们先定义了一个 `node` 结构体,它包含了一个键值 `key` 和两个指针 `left` 和 `right`,分别指向左子树和右子树。然后,我们定义了一个 `insert` 函数,用于向二叉排序树中插入新节点。在插入节点时,我们按照二叉排序树的定义,将键值小于当前节点的值的节点插入到左子树中,将键值大于等于当前节点的值的节点插入到右子树中。最后,我们定义了一个 `inorder` 函数,用于按升序输出二叉排序树中的所有节点。 在 `main` 函数中,我们首先定义了一个空的二叉排序树 `root`,然后将所有键值插入到二叉排序树中。最后,我们调用 `inorder` 函数,按升序输出二叉排序树中的所有节点。 希望这个例子对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值