教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 行业范文 >

二叉排序树与平衡二叉树的判别程序

来源:网络收集 时间:2026-01-25
导读: 该程序主要实现建立二叉树,判别二叉树是否为二叉排序树与平衡二叉树 #includestdio.h #includestdlib.h #define max 10 typedef struct node{ int data; node *lchild,*rchild; }Bitree; Bitree *B[max]; int temp=0; int Btree[max]; static int x,y,q; //

该程序主要实现建立二叉树,判别二叉树是否为二叉排序树与平衡二叉树

#include<stdio.h>

#include<stdlib.h>

#define max 10

typedef struct node{

int data;

node *lchild,*rchild;

}Bitree;

Bitree *B[max];

int temp=0;

int Btree[max];

static int x,y,q; //定义3个静态变量,x,y用于记录二叉树左右子树深度,q记录此二叉树是

否为二叉排序树

Bitree *Creatree(){ //建立二叉树

Bitree *T,*S;

}

int ch; int front,rear,sign; sign=0; front=0; rear=-1; T=NULL; printf("建立二叉树(1表示虚结点,0表示输入结束):\n"); scanf("%d",&ch); while(ch!=0){ if(ch!=1){ //输入结点不是虚结点 S=(Bitree *)malloc(sizeof(Bitree)); S->data=ch; S->lchild=S->rchild=NULL; rear++; B[rear]=S; if(rear==front){ T=S; sign++; } else{ if(sign%2==1) //寻找父结点 } } sign++; B[front]->lchild=S; B[front]->rchild=S; front++; if(sign%2==0){ else{ //输入结点为虚结点

该程序主要实现建立二叉树,判别二叉树是否为二叉排序树与平衡二叉树

} if(sign%2==0) front++; sign++; } scanf("%d",&ch); return T;

}

void Inorder(Bitree *T){ //中序遍历二叉树,并将每个结点数据存入数组中

if(T!=NULL){ Inorder(T->lchild); printf("%d\t",T->data); Btree[temp]=T->data; temp++; Inorder(T->rchild); }

}

int Judgesort_bitree(int Btree[]){ //判断是否是二叉树

int i,sign=1; for(i=0;i<temp-1;i++){ } if(Btree[i]>Btree[i+1]){ } sign=0; break;

return sign;

}

void Judgeout(int a){ //判断输出

if(a==1)

printf("给定二叉树是二叉排序树!\n");

if(a==0) printf("给定二叉树不是二叉排序树!\n");

q=a;

}

void Depth(Bitree *T){ //求二叉树左右子树的深度

if(T!=NULL){ Inorder(T->lchild); printf("%d\t",T->data); Btree[temp]=T->data; temp++;

int x=temp;

Inorder(T->rchild);

temp++;

int y=temp-x;

该程序主要实现建立二叉树,判别二叉树是否为二叉排序树与平衡二叉树

}

}

int Judgesort_balanced(int Btree[]){ //判断是否是平衡二叉树

if(q==1){ if((x-y>1)||(x-y<-1))

{

printf("给定二叉树不是平衡二叉树!\n");

}

else {

printf("给定二叉树是平衡二叉树!\n");

}

}

else{

printf("给定二叉树不是平衡二叉树!\n");

}

return 0;

}

void main(){

Bitree *T; T=Creatree(); printf("中序遍历:\n"); Inorder(T); printf("\n"); Judgeout(Judgesort_bitree(Btree));

Judgesort_balanced(Btree);

}

二叉排序树与平衡二叉树的判别程序.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/980997.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)