二叉排序树 求树的深度 各个结点的深度
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
q[i] = T;
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
while(T->left && ls[i] == 0)
{
T =T->left;
ls[i] = 1;
i++; q[i] = T;k++;
ls[i] =0; rs[i] = 0;
||(T->right ==NULL && ls[i] == 1 ) ||( rs[i] ==1&& ls[i] == 1 ) )
{
i--; k++;
T = q[i];
}
}
}
void main()
{
tree p =NULL; int num; scanf("%d",&num);
p =(tree) malloc( sizeof(struct Tree) );
i++; q[i] = T;k++;
ls[i] =0; rs[i] = 0;//每次进栈的新结点,其左子树和右子树均为访问
num++;if(max <=num) max =num;
}
if(( T->left ==NULL && T->right && rs[i] ==0 ) || (ls[i] ==1 && T->right&& rs[i] ==0 ) )
else { tmp = T; T= T->left; sign =0;}
if(T ==NULL && sign == 1) { tmp ->right = q;}
if(T ==NULL && sign == 0) { tmp ->left = q;}
}
}
int TreeDepth(tree p)//统计"根“的深度
{
tree T = p; int i; tree q[100]; i =1; q[0]= T;int k ; k = 0;
int rs[100];int ls[100]; int j;
for(j=0; j<100 ;j++)
{
rs[j] =0;ls[j] =0;
}
while( i != 0)
{
if(i==1 && k == 0)
{
T = T->right; rs[i] =1;//访问了当前结点的右结点,则标记Rs[i]=1;
k++;i++;q[i] = T;
ls[i] =0; rs[i] = 0;
num++;if(max <=num) max =num;
}
if( (T->left ==NULL && rs[i] == 1 ) ||(T->left ==NULL && T->right == NULL)
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
if(( T->left ==NULL && T->right && rs[i] ==0 ) || (ls[i] ==1 && T->right&& rs[i] ==0 ) )
{
T = T->right;
i=0; tree q,tmp;
q =(tree ) malloc( sizeof(struct Tree) );
q->data = num; q->left =NULL; q->right =NULL;
while(T)
{
if(num > T->data ) { tmp = T; T= T->right;sign =1;}
{
tree T = p; int i; tree q[100]; i =1; q[0]= T;int k ; k = 0;
int rs[100];int ls[100]; int j; int num =1; int max=1;
for(j=0; j<100 ;j++)
{
rs[j] =0;ls[j] =0;
||(T->right ==NULL && ls[i] == 1 ) ||( rs[i] ==1&& ls[i] == 1 ) )
{
i--; k++; num--;
T = q[i];
}
}
return max;
}
void AllTreeDepth(tree p)//所有“结点”的深度,利用“每个结点“重新作为”根“,重复运用”求根的深度“函数则可…
//printf("\n");
AllTreeDepth(p);
printf("\n");
}
p->data = num; p->left =NULL; p->right =NULL;
int i;
for(i=1; i<=4;i++)
{scanf("%d",&num); InsertTree(p,num);}
printf("\n");
printf("树的深度为:%d\n",TreeDepth(p));
}
while( i != 0)
//采取先序遍历的方法,依次进栈、出栈(进栈的数目最大值则为:根的深度)
{
if(i==1 &&Baidu Nhomakorabeak == 0)
{
q[i] = T;
}
while(T->left && ls[i] == 0)
{
T =T->left;ls[i] = 1;//访问了当前结点的左结点,则标记Ls[i]=1;
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree
{
int data;
struct Tree *left;
struct Tree *right;
}*tree;
void InsertTree(tree p,int num)
{
tree T; T = p; int i; int sign;
rs[i] =1;
k++;
i++;q[i] = T;
ls[i] =0; rs[i] = 0;
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
if( (T->left ==NULL && rs[i] == 1 ) ||(T->left ==NULL && T->right == NULL)
q[i] = T;
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
while(T->left && ls[i] == 0)
{
T =T->left;
ls[i] = 1;
i++; q[i] = T;k++;
ls[i] =0; rs[i] = 0;
||(T->right ==NULL && ls[i] == 1 ) ||( rs[i] ==1&& ls[i] == 1 ) )
{
i--; k++;
T = q[i];
}
}
}
void main()
{
tree p =NULL; int num; scanf("%d",&num);
p =(tree) malloc( sizeof(struct Tree) );
i++; q[i] = T;k++;
ls[i] =0; rs[i] = 0;//每次进栈的新结点,其左子树和右子树均为访问
num++;if(max <=num) max =num;
}
if(( T->left ==NULL && T->right && rs[i] ==0 ) || (ls[i] ==1 && T->right&& rs[i] ==0 ) )
else { tmp = T; T= T->left; sign =0;}
if(T ==NULL && sign == 1) { tmp ->right = q;}
if(T ==NULL && sign == 0) { tmp ->left = q;}
}
}
int TreeDepth(tree p)//统计"根“的深度
{
tree T = p; int i; tree q[100]; i =1; q[0]= T;int k ; k = 0;
int rs[100];int ls[100]; int j;
for(j=0; j<100 ;j++)
{
rs[j] =0;ls[j] =0;
}
while( i != 0)
{
if(i==1 && k == 0)
{
T = T->right; rs[i] =1;//访问了当前结点的右结点,则标记Rs[i]=1;
k++;i++;q[i] = T;
ls[i] =0; rs[i] = 0;
num++;if(max <=num) max =num;
}
if( (T->left ==NULL && rs[i] == 1 ) ||(T->left ==NULL && T->right == NULL)
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
if(( T->left ==NULL && T->right && rs[i] ==0 ) || (ls[i] ==1 && T->right&& rs[i] ==0 ) )
{
T = T->right;
i=0; tree q,tmp;
q =(tree ) malloc( sizeof(struct Tree) );
q->data = num; q->left =NULL; q->right =NULL;
while(T)
{
if(num > T->data ) { tmp = T; T= T->right;sign =1;}
{
tree T = p; int i; tree q[100]; i =1; q[0]= T;int k ; k = 0;
int rs[100];int ls[100]; int j; int num =1; int max=1;
for(j=0; j<100 ;j++)
{
rs[j] =0;ls[j] =0;
||(T->right ==NULL && ls[i] == 1 ) ||( rs[i] ==1&& ls[i] == 1 ) )
{
i--; k++; num--;
T = q[i];
}
}
return max;
}
void AllTreeDepth(tree p)//所有“结点”的深度,利用“每个结点“重新作为”根“,重复运用”求根的深度“函数则可…
//printf("\n");
AllTreeDepth(p);
printf("\n");
}
p->data = num; p->left =NULL; p->right =NULL;
int i;
for(i=1; i<=4;i++)
{scanf("%d",&num); InsertTree(p,num);}
printf("\n");
printf("树的深度为:%d\n",TreeDepth(p));
}
while( i != 0)
//采取先序遍历的方法,依次进栈、出栈(进栈的数目最大值则为:根的深度)
{
if(i==1 &&Baidu Nhomakorabeak == 0)
{
q[i] = T;
}
while(T->left && ls[i] == 0)
{
T =T->left;ls[i] = 1;//访问了当前结点的左结点,则标记Ls[i]=1;
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree
{
int data;
struct Tree *left;
struct Tree *right;
}*tree;
void InsertTree(tree p,int num)
{
tree T; T = p; int i; int sign;
rs[i] =1;
k++;
i++;q[i] = T;
ls[i] =0; rs[i] = 0;
printf("结点值为%d的深度:%d\n",q[i]->data,TreeDepth(q[i]));
}
if( (T->left ==NULL && rs[i] == 1 ) ||(T->left ==NULL && T->right == NULL)