从下往上开始计算每个结点的深度及平衡因子。
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Node
{
int data;
int bf;
int depth;
struct Node *llink,*rlink;
}Node,*Tree;
Status SearchBST(Tree T,int key,Tree f,Tree *p);
Status InsertBST(Tree *T,int e);
int Factor_Depth_Make(Tree T);
int main()
{
Tree T;
T=(Tree)malloc(sizeof(Node));
T=NULL;
int e;
printf("What number you want to search--0 to quit:");
scanf("%d",&e);
while(e)
{
if(!InsertBST(&T,e))
printf("Done!");
else
printf("It`s been inserted for the first time.");
printf("\nWhat number you want to search--0 to quit:");
scanf("%d",&e);
}
int h;
h=0;
h=Factor_Depth_Make(T);
printf("树的高度为:%d.\n",h);
return 0;
}
Status SearchBST(Tree T,int key,Tree f,Tree *p)
{
if(!T)
{
*p=f;
return FALSE;
}
else if(key==T->data)

本文介绍了一种自底向上的方法,详细阐述如何计算二叉树中每个节点的深度,并计算其平衡因子,这对于理解二叉树的平衡状态至关重要。

994

被折叠的 条评论
为什么被折叠?



