第十四周实践项目三--验证是否为BST

本文介绍了如何使用C语言实现二叉排序树的创建和判断是否为排序二叉树的功能,包括插入节点、中序遍历以及验证排序性质。

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

问题及代码:

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef int KeyType;                    //定义关键字类型
typedef char InfoType;
typedef struct node                     //记录类型
{
    KeyType key;                        //关键字项
    InfoType data;                      //其他数据域
    struct node *lchild,*rchild;        //左右孩子指针
} BSTNode;
int path[MaxSize];                      //全局变量,用于存放路径
void DispBST(BSTNode *b);               //函数说明
int InsertBST(BSTNode *&p,KeyType k)    //在以*p为根节点的BST中插入一个关键字为k的节点
{
    if (p==NULL)                        //原树为空, 新插入的记录为根节点
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->key=k;
        p->lchild=p->rchild=NULL;
        return 1;
    }
    else if (k==p->key)
        return 0;
    else if (k<p->key)
        return InsertBST(p->lchild,k);  //插入到*p的左子树中
    else
        return InsertBST(p->rchild,k);  //插入到*p的右子树中
}
BSTNode *CreatBST(KeyType A[],int n)
//由数组A中的关键字建立一棵二叉排序树
{
    BSTNode *bt=NULL;                   //初始时bt为空树
    int i=0;
    while (i<n)
        InsertBST(bt,A[i++]);       //将A[i]插入二叉排序树T中
    return bt;                          //返回建立的二叉排序树的根指针
}

void DispBST(BSTNode *bt)
//以括号表示法输出二叉排序树bt
{
    if (bt!=NULL)
    {
        printf("%d",bt->key);
        if (bt->lchild!=NULL || bt->rchild!=NULL)
        {
            printf("(");
            DispBST(bt->lchild);
            if (bt->rchild!=NULL) printf(",");
            DispBST(bt->rchild);
            printf(")");
        }
    }
}

/*
int JudgeBST(BSTNode *bt)为判断一个树是否为排序二叉树设计的算法的实现
*/
KeyType predt=-32767; //predt为全局变量,保存当前节点中序前趋的值,初值为-∞
int JudgeBST(BSTNode *bt)   //判断bt是否为BST
{
    int b1,b2;
    if (bt==NULL)
        return 1;    //空二叉树是排序二叉树
    else
    {
        b1=JudgeBST(bt->lchild);   //返回对左子树的判断,非排序二叉树返回0,否则返回1
        if (b1==0 || predt>=bt->key)  //当左子树非排序二叉树,或中序前趋(全局变量)大于当前根结点时
            return 0;    //返回“不是排序二叉树”
        predt=bt->key;   //记录当前根为右子树的中序前趋
        b2=JudgeBST(bt->rchild);   //对右子树进行判断
        return b2;
    }
}

int main()
{
    BSTNode *bt;
    int a[]= {43,91,10,18,82,65,33,59,27,73},n=10;
    printf("创建排序二叉树:");
    bt=CreatBST(a,n);
    DispBST(bt);
    printf("\n");
    printf("bt%s\n",(JudgeBST(bt)?"是一棵BST":"不是一棵BST"));
    bt->lchild->rchild->key = 30;  //搞个破坏!
    printf("修改后的二叉树:");
    DispBST(bt);
    printf("\n");
    printf("bt%s\n",(JudgeBST(bt)?"是一棵BST":"不是一棵BST"));
    return 0;
}
运行结果:


<script> // <!-- 封装二叉搜索树 --> function binaryserachtree() { function node(key){ this.key==key this.left=null this.right=null } // 属性 this.root=null // 封装方法 // 1.insert向树中插入数据,对外调用的方法 binaryserachtree.prototype.insert=function(key){ // 先根据传入的这个数先生成一个节点 var newcode=new node(key) // 先判断根节点是否有值 if(this.root==null){ // 现在没有根节点 this.root=newcode } // 如果有根节点,和根节点进行一个大小比较 else{ // 和根节点进行比较 this.insertnode(this.root,newcode) } } // 第一次node是一个9 现在newnode是一个14 // 插入的递归函数 binaryserachtree.prototype.insertnode=function(node,newnode){ if(newnode.key<node.key){ if(node.left==null){ node.left=newnode } else{ this.insertnode(node.left,newnode) } }else{ if(node.right==null){ node.right=newnode } else{ this.insertnode(node.right,newnode) } } } // 遍历方式 ,树的遍历 // 1.先序遍历 binaryserachtree.prototype.preOrderTraversal=function(handler){ this.preOrderTraversalnode(this.root,handler) } binaryserachtree.prototype.preOrderTraversalnode=function(node,handler){ if(node!==null){ // 1.处理经过的节点 handler(node.key) // 处理经过节点的左子节点 this.preOrderTraversalnode(node.left,handler) // 处理经过节点的右子节点 this.preOrderTraversalnode(node.right,handler) } } } // 测试代码 var bst=new binaryserachtree() bst.insert(11) bst.insert(7) bst.insert(15) bst.insert(5) bst.insert(3) bst.insert(9) bst.insert(8) bst.insert(10) bst.insert(13) bst.insert(12) bst.insert(14) bst.insert(20) bst.insert(18) bst.insert(25) // 测试遍历 var resultstring="" bst.preOrderTraversal(function(key){ resultstring+=key+" " return resultstring }) // console.log(resultstring); alert(resultstring) </script>为什么结果是u
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值