试编写一个算法,判断给定的二叉树是否是二叉排序树。
//参考博客:https://blog.csdn.net/weixin_44162361/article/details/119112155
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
//二叉排序树(Binary Sort Tree, BST),也称二叉查找树
typedef int ElemType;
using namespace std;
#define STR_SIZE 1000
#define MaxSize 100
#define ERROR 0
#define OK 1
//定义结构体
typedef struct BSTNode {
ElemType data;
struct BSTNode* lchild;
struct BSTNode* rchild;
}BSTNode, * BSTree;
typedef struct BiTNode
{
ElemType data;
BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
void draw(BSTNode* root);
void draw1(BiTNode* root);
bool Create_tree(BiTree& T) //递归创建二叉树
{
ElemType x = 0;
cin >> x;
if (x == 0)
{
T = NULL;
}
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (T == NULL)
{
cout << "内存无法分配!!!" << endl;
return ERROR;
}
T->data = x;
T->lchild = NULL;
T->rchild = NULL;
Create_tree(T->lchild);
Create_tree(T->rchild);
}
return OK;
}
//二叉树结点创建
BSTNode* CreateTreeNode(ElemType x) {
BSTNode* p = (BSTNode*)malloc(sizeof(BSTNode));//新建结点
if (p == NULL)
{
cout << "内存分配失败" << endl;
exit(0);
}
p->data = x;
p->lchild = NULL;
p->rchild = NULL;
return p;
}
//插入的递归算法
BSTNode* Insert(BSTNode* &root, ElemType x) {
if (root == NULL) {
root = CreateTreeNode(x);
return root;
}
if (x < root->data) {
root->lchild = Insert(root->lchild, x);
}
if (x > root->data) {
root->rchild = Insert(root->rchild, x);
}
return root;
}
void Create(BSTNode* &root, ElemType str[], int n) {
root = NULL;
for (int i = 0; i < n; i++) {
Insert(root, str[i]);
}
}
//---------------------------------核心代码---------------------------------//
int Min = -32768;
int flag = 1;//标记是否是二叉排序树(初始时,默认是二叉排序树)
void Judge_BST(BSTree T)
{
if (T!=NULL)
{
Judge_BST(T->lchild);
if (T->data < Min)
{
flag = 0;
}
Min = T->data;
Judge_BST(T->rchild);
}
}
int Min1 = -32768;
int flag1 = 1;
void Judge_BST1(BiTree T)
{
if (T != NULL)
{
Judge_BST1(T->lchild);
if (T->data < Min1)
{
flag1 = 0;
}
Min1 = T->data;
Judge_BST1(T->rchild);
}
}
//---------------------------------核心代码---------------------------------//
//测试1:二叉排序树序列:7 4 5 6 1 8 9 二叉树序列:1 2 0 0 4 5 0 0 0
int main(void)
{
BSTree root = NULL;
ElemType str[100];
int n = 0;
cout << "请输入数据的长度" << endl;
cin >> n;
cout << "请输入数据" << endl;
for (int i = 0; i < n; i++) {
cin >> str[i];
}
Create(root, str, n);
cout << "生成的二叉排序树如下" << endl;
draw(root);
Judge_BST(root);
if (flag == 1)
cout << "yes" << endl;
else
cout << "no" << endl;
BiTree T = NULL;
cout << "请输入二叉树数据" << endl;
Create_tree(T);
cout << "生成的二叉树如下" << endl;
draw1(T);
Judge_BST1(T);
if (flag1 == 1)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
//参考博客:https://blog.csdn.net/weixin_42109012/article/details/92250160
/*****************************************************************************
* @date 2020/4/19
* @brief 水平画树
* @param node 二叉树节点
* @param left 判断左右
* @param str 可变字符串
*****************************************************************************/
void draw_level(BSTNode* node, bool left, char* str) {
if (node->rchild) {
draw_level(node->rchild, false, strcat(str, (left ? "| " : " ")));
}
printf("%s", str);
printf("%c", (left ? '\\' : '/'));
printf("-----");
printf("%d\n", node->data);
if (node->lchild) {
draw_level(node->lchild, true, strcat(str, (left ? " " : "| ")));
}
// " " : "| " 长度为 6
str[strlen(str) - 6] = '\0';
}
/*****************************************************************************
* @date 2020/4/19
* @brief 根节点画树
* @param root 二叉树根节点
*****************************************************************************/
void draw(BSTNode* root) {
char str[STR_SIZE];
memset(str, '\0', STR_SIZE);
/**
* 1. 在 windows 下,下面是可执行的
* 2. 在 Linux 下,执行会报 Segmentation fault
* 需要使用中间变量
*/
if (root->rchild) {
draw_level(root->rchild, false, str);
}
printf("%d\n", root->data);
if (root->lchild) {
draw_level(root->lchild, true, str);
}
}
//参考博客:https://blog.csdn.net/weixin_42109012/article/details/92250160
/*****************************************************************************
* @date 2020/4/19
* @brief 水平画树
* @param node 二叉树节点
* @param left 判断左右
* @param str 可变字符串
*****************************************************************************/
void draw_level1(BiTNode* node, bool left, char* str) {
if (node->rchild) {
draw_level1(node->rchild, false, strcat(str, (left ? "| " : " ")));
}
printf("%s", str);
printf("%c", (left ? '\\' : '/'));
printf("-----");
printf("%d\n", node->data);
if (node->lchild) {
draw_level1(node->lchild, true, strcat(str, (left ? " " : "| ")));
}
// " " : "| " 长度为 6
str[strlen(str) - 6] = '\0';
}
/*****************************************************************************
* @date 2020/4/19
* @brief 根节点画树
* @param root 二叉树根节点
*****************************************************************************/
void draw1(BiTNode* root) {
char str[STR_SIZE];
memset(str, '\0', STR_SIZE);
/**
* 1. 在 windows 下,下面是可执行的
* 2. 在 Linux 下,执行会报 Segmentation fault
* 需要使用中间变量
*/
if (root->rchild) {
draw_level1(root->rchild, false, str);
}
printf("%d\n", root->data);
if (root->lchild) {
draw_level1(root->lchild, true, str);
}
}