Determining whether a given binary tree binary sort tree
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) { QA [ ++ q.rear] = BST-> rchild; } the else {In Flag = 0 ; BREAK ;} } } IF (In Flag == 0 ) {the printf ( " not binary sort tree \ n- " );} the else {the printf ( " a binary sort tree \ n- " );} }
Level specified node in the binary sort tree
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); }