二叉排序树操作(一)

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

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);
}

猜你喜欢

转载自www.cnblogs.com/Yshun/p/11329529.html