这是优快云博主Hackbuteer1的文章!学习一下。
思路:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。
递归遍历就可以了,反正就是左孩子的key比根节点的key小,右孩子的key比根节点的key大,一旦有不满足条件的就判定不是。
完整的代码如下:
- #include "stdio.h"
- #include "stdlib.h"
- typedef struct node
- {
- int data;
- struct node *lchild,*rchild;
- }Bitree;
- Bitree *B[100];
- Bitree *CreateBiTree()
- {
- int num,i,n;
- Bitree *t,*s;
- t=NULL;
- printf("建立二叉树(1表示为虚结点,0表示输入结束):/n");
- num=0;
- scanf("%d",&n);
- while(n!=0)
- {
- s=(Bitree *)malloc(sizeof(Bitree));
- s->data=n;
- s->lchild=s->rchild=NULL;
- num++;
- if(!t)
- t=s;
- B[num]=s;
- scanf("%d",&n);
- }
- for(i=1;i<=num;i++)
- {
- if(B[i]->data!=1)
- {
- if(2*i<=num && B[2*i]->data!=1)
- B[i]->lchild=B[2*i];
- if(2*i+1<=num && B[2*i+1]->data!=1)
- B[i]->rchild=B[2*i+1];
- }
- }
- return t;
- }
- int IsSearchTree(const Bitree *t) //递归遍历二叉树是否为二叉排序树
- {
- if(!t) //空二叉树情况
- return 1;
- else if(!(t->lchild) && !(t->rchild)) //左右子树都无情况
- return 1;
- else if((t->lchild) && !(t->rchild)) //只有左子树情况
- {
- if(t->lchild->data>t->data)
- return 0;
- else
- return IsSearchTree(t->lchild);
- }
- else if((t->rchild) && !(t->lchild)) //只有右子树情况
- {
- if(t->rchild->data<t->data)
- return 0;
- else
- return IsSearchTree(t->rchild);
- }
- else //左右子树全有情况
- {
- if((t->lchild->data>t->data) || (t->rchild->data<t->data))
- return 0;
- else
- return ( IsSearchTree(t->lchild) && IsSearchTree(t->rchild) );
- }
- }
- int main(void)
- {
- int flag=0;
- Bitree *tree;
- tree=CreateBiTree();
- flag=IsSearchTree(tree);
- if(flag)
- printf("这棵树是二叉排序树!/n");
- else
- printf("这棵树不是二叉排序树!/n");
- system("pause");
- return 0;
- }