请编程实现二叉树的操作
1.二叉树的创建
typedef struct a
{
char data;
struct a* lchild;
struct a* rchild;
} *Node;
Node create_node()
{
Node node = (Node)malloc(sizeof(struct a));
if (NULL == node)
{
return NULL;
}
node->lchild = NULL;
node->rchild = NULL;
return node;
}
Node create_tree()
{
char c;
printf("请输入数据:");
scanf("%c", &c);
getchar();
if ('#' == c)
{
return NULL;
}
Node node = create_node();
if (NULL == node)
{
return NULL;
}
node->data = c;
node->lchild = create_tree();
node->rchild = create_tree();
return node;
}
2.二叉树的先序遍历
void pre_order(Node root)
{
if (NULL == root)
{
return;
}
printf("%2c", root->data);
pre_order(root->lchild);
pre_order(root->rchild);
}
3.二叉树的中序遍历
void in_order(Node root)
{
if (NULL == root)
{
return;
}
in_order(root->lchild);
printf("%2c", root->data);
in_order(root->rchild);
}
4.二叉树的后序遍历
void post_order(Node root)
{
if (NULL == root)
{
return;
}
post_order(root->lchild);
post_order(root->rchild);
printf("%2c", root->data);
}
5.二叉树各个节点度的个数
void count(Node root, int* n0, int* n1, int* n2)
{
if (NULL == root)
{
return;
}
if (root->lchild && root->rchild)
{
(*n2)++;
}
else if (!root->lchild && !root->rchild)
{
(*n0)++;
}
else
{
(*n1)++;
}
count(root->lchild, n0, n1, n2);
count(root->rchild, n0, n1, n2);
}
6.二叉树的深度
int height(Node root)
{
if (NULL == root)
{
return 0;
}
int l = height(root->lchild);
int r = height(root->rchild);
return l > r ? l + 1 : r + 1;
}
结果
源代码
#include <stdio.h>
#include <stdlib.h>
typedef struct a
{
char data;
struct a* lchild;
struct a* rchild;
} *Node;
Node create_node()
{
Node node = (Node)malloc(sizeof(struct a));
if (NULL == node)
{
return NULL;
}
node->lchild = NULL;
node->rchild = NULL;
return node;
}
Node create_tree()
{
char c;
printf("请输入数据:");
scanf("%c", &c);
getchar();
if ('#' == c)
{
return NULL;
}
Node node = create_node();
if (NULL == node)
{
return NULL;
}
node->data = c;
node->lchild = create_tree();
node->rchild = create_tree();
return node;
}
void pre_order(Node root)
{
if (NULL == root)
{
return;
}
printf("%2c", root->data);
pre_order(root->lchild);
pre_order(root->rchild);
}
void in_order(Node root)
{
if (NULL == root)
{
return;
}
in_order(root->lchild);
printf("%2c", root->data);
in_order(root->rchild);
}
void post_order(Node root)
{
if (NULL == root)
{
return;
}
post_order(root->lchild);
post_order(root->rchild);
printf("%2c", root->data);
}
void count(Node root, int* n0, int* n1, int* n2)
{
if (NULL == root)
{
return;
}
if (root->lchild && root->rchild)
{
(*n2)++;
}
else if (!root->lchild && !root->rchild)
{
(*n0)++;
}
else
{
(*n1)++;
}
count(root->lchild, n0, n1, n2);
count(root->rchild, n0, n1, n2);
}
int height(Node root)
{
if (NULL == root)
{
return 0;
}
int l = height(root->lchild);
int r = height(root->rchild);
return l > r ? l + 1 : r + 1;
}
int main()
{
Node root = create_tree();
printf("先序:");
pre_order(root);
puts("");
printf("中序:");
in_order(root);
puts("");
printf("后序:");
post_order(root);
puts("");
int n0 = 0, n1= 0, n2 = 0;
count(root, &n0, &n1, &n2);
printf("n0 = %d, n1 = %d, n2 = %d\n", n0, n1, n2);
printf("高度:%d\n", height(root));
return 0;
}