#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
char data;
struct tree * lc, * rc;
}bit;
void creat(bit * & );
void inorder(bit * );//先序遍历
int depth(bit * );
int count(bit * );
int main(void)
{
bit * root;
creat(root);
inorder(root);
printf("\n%d\n", depth(root));
printf("%d\n", count(root));
return 0;
}
int count(bit * node)
{
if (node == NULL)
return 0;
else
return count(node->lc) + count(node->rc) + 1;
}
int depth(bit * node)
{
int m, n;
if (node == NULL)
return 0;
else
{
m = depth(node->lc);
n = depth(node->rc);
if (m > n)
return m + 1;
else
return n + 1;
}
}
void inorder(bit * node)
{
if (node)
{
printf("%c ", node->data);
inorder(node->lc);
inorder(node->rc);
}
}
void creat(bit * &node)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
node = NULL;
else
{
node = (bit * )malloc(sizeof(bit));
node->data = ch;
creat(node->lc);
creat(node->rc);
}
}
数据结构--二叉树
最新推荐文章于 2022-11-16 22:39:59 发布