写一个递归算法确定二叉排序树中各结点的平衡因子,同时返回二叉树中非叶子结点个数。
//题目:计算二叉排序树中各结点的平衡因子,同时返回非叶子结点个数
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Node
{
int data;
int bf;
struct Node *llink,*rlink;
}Node,*Tree;
Status SearchBST(Tree T,int key,Tree f,Tree *p);
Status InsertBST(Tree *T,int e);
void Factor_Make(Tree T,int *h,int *unleaf);
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,unleaf;
h=unleaf=0;
Factor_Make(T,&h,&unleaf);
printf("树的高度为:%d.树的非叶子结点个数为:%d\n",h,unleaf);
return 0;
}
Status SearchBST(Tree T,int key,Tree f,Tree