二叉排序树操作(一)

判断给定的二叉树是否是二叉排序树

 

void JudegBST(BSTree &T){
    Queue q;
    BSTree bst;
    int flag=1;
    q.front=-1;
    q.rear=-1;
    q.a[++q.rear]=T;
    while(q.front<q.rear){
        bst=q.a[++q.front];
        if(bst->lchild){
            if(bst->lchild->data<=bst->data){
                q.a[++q.rear]=bst->lchild;
            }
            else{flag=0;break;}
        }
        if(bst->rchild){
            if(bst->rchild->data>=bst->data){
                q.a[++q.rear]=bst->rchild;
            }
            else{flag=0;break;}
        }
    }
    if(flag==0){printf("不是二叉排序树\n");}
    else{printf("是二叉排序树\n");}
}

 

指定节点在二叉排序树中的层次

void BSTLevel(BSTree &T,ElemType key){
    int number=1;
    BSTree bst;
    bst=T;
    while(bst&&bst->data!=key){
        if(key<=bst->data){
            number=number+1;
            bst=bst->lchild;
        }
        else{
            number=number+1;
            bst=bst->rchild;
        }
    }
    printf("指定节点的层次:%d\n",number);
}

 

由大到小输出不小于k的关键字

void Putk(BSTree &T,ElemType key){
    if(T){
        if(T->rchild){
            Putk(T->rchild,key);
        }
        if(T->data>=key){
            visit(T);
        }
        if(T->lchild){
            Putk(T->lchild,key);
        }
    }
}

 

转载于:https://www.cnblogs.com/Yshun/p/11329529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值