/**
* 实验题目:
* 判断一个序列是否是二叉排序树中的一个合法的查找序列
* 实验目的:
* 掌握二叉排序树查找过程及其算法设计
* 实验内容:
* 设计程序,构造一颗二叉排序树bt,判断一个序列a是否是
* 二叉排序树bt中的一个合法的查找序列。
*/
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
#define MAX_SIZE 100
typedef int key_type; // 定义关键字类型
typedef char info_type;
typedef struct node { // 记录类型
key_type key; // 关键字项
info_type data; // 其他数据域
struct node *lchild; // 左孩子指针
struct node *rchild; // 右孩子指针
}BSTNode;
/*----------------以括号表示法输出二叉排序树bt------------------*/
void disp_bst(BSTNode *bt); // 函数声明
bool insert_bst(BSTNode *&bt, key_type key) // 在以bt为根结点的BST中插入一个关键字为key的结点
{
if(bt == NULL) // 原树为空,新插入的记录为根结点
{
bt = (BSTNode *)malloc(sizeof(BSTNode));
bt->key = key;
bt->lchild = bt->rchild = NULL;
return true;
}
else if(key == bt->key)
return false;
else if(key < bt->key)
return insert_bst(bt->lchild, key); // 插入到bt结点的左子树中
else
return insert_bst(bt->rchild, key); // 插入到bt结点的右子树中
}
/*----------------由数组a中的关键字建立一颗二叉排序树------------------*/
BSTNode *create_bst(key_type a[], int n)
{
BSTNode *bt = NULL; // 初始时bt为空树
int i = 0;
while(i < n)
{
if(insert_bst(bt, a[i]) == 1) // 将a[i]插入二叉排序树bst中
{
printf(" 第%d步,插入%d:", i + 1, a[i]);
disp_bst(bt);
printf("\n");
i++;
}
}
return bt;
}
/*----------------以括号表示法输出二叉排序树bt------------------*/
void disp_bst(BSTNode *bt)
{
if(bt != NULL)
&nb