二叉排序树操作(一)

mac2022-06-30  71

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

 

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)