1-创建二叉树(完全二叉树)
2-递归先序遍历
3-迭代先序遍历
4-递归中序遍历
5-迭代中序遍历
6-递归后序遍历
7-迭代后序遍历
8-层序遍历
9-增加节点(层序增加)
10-递归求树深度
11-迭代求树深度
12-求叶子节点个数
//****************************************
//数据结构:二叉树(链式存储)
//算法:创建、增加、求深度和叶节点数、遍历
//首次编辑时间:2020/05/30
//最后修改时间:2020/06/02
//****************************************
#include<stdio.h>
#include<stdlib.h>
typedef struct binarytree
{
int data;
struct binarytree *left, *right;
}node_bt;
//此结构体用于:构建辅助链队列和辅助链栈
//辅助链队列用于:创建完全二叉树、层序遍历、增加二叉树节点
//辅助链栈用于:迭代(先、中、后)序遍历
typedef struct queue_stack
{
node_bt *node;
struct queue_stack *next;
}node_queue, node_stack;
void creat();//创建二叉树(完全二叉树)
void traverse_preorder_recurrence();//递归先序遍历
void traverse_preorder_iteration();//迭代先序遍历
void traverse_inorder_recurrence();//递归中序遍历
void traverse_inorder_iteration();//迭代中序遍历
void traverse_postorder_recurrence();//递归后序遍历
void traverse_postorder_iteration();//迭代后序遍历
void traverse_level();//层序遍历
void add();//增加节点(层序增加)
int depth_recurrence();//递归求树深度
void depth_iteration();//迭代求树深度
void number_leaf();//求叶子节点个数
int main()
{
node_bt *root = (node_bt*)calloc(1, sizeof(node_bt));
creat(root);
while (1)
{
int action;
printf("*****************************\n");
printf("选择:\n");
printf("0--结束退出\n");
printf("1--七种遍历\n");
printf("2--增加节点\n");
printf("3--求叶子数\n");
printf("4--递归求树深度\n");
printf("5--迭代求树深度\n");
printf("-----------------------------\n");
printf("输入选择序号:");
scanf_s("%d", &action);
printf("-----------------------------\n");
switch (action)
{
case 0:return 0;
case 1:
{
printf("递归先序遍历:");traverse_preorder_recurrence(root);printf("\n");
printf("迭代先序遍历:");traverse_preorder_iteration(root);printf("\n");
printf("递归中序遍历:");traverse_inorder_recurrence(root);printf("\n");
printf("迭代中序遍历:");traverse_inorder_iteration(root);printf("\n");
printf("递归后序遍历:");traverse_postorder_recurrence(root);printf("\n");
printf("迭代后序遍历:");traverse_postorder_iteration(root);printf("\n");
printf("层序遍历: ");traverse_level(root);printf("\n");break;
}
case 2:add(root);break;
case 3:number_leaf(root);break;
case 4:printf_s("二叉树深度为:%d\n", depth_recurrence(root));break;
case 5:depth_iteration(root);break;
default:printf("无效序号!");
}
}
return 0;
}
void creat(node_bt *root)//创建二叉树(完全二叉树)
{
int i;
node_bt *new_node_bt, *bbt;
node_queue *front, *rear, *pqu;
root->data = 1;
root->left = root->right = NULL;
for (i = 2;i < 6;i++)//创建时顺便再增加4个节点
{
new_node_bt = (node_bt*)malloc(sizeof(node_bt));//创建好节点,找到插入位置后直接插入
new_node_bt->data = i;
new_node_bt->left = new_node_bt->right = NULL;
rear = front = (node_queue*)malloc(sizeof(node_queue));
rear->node = root;
rear->next = NULL;
while (1)
{
bbt = front->node;
if (bbt->left == NULL)
{
bbt->left = new_node_bt;
while (front != NULL)//插入二叉树节点后,释放整个队列
{
pqu = front;
front = front->next;
free(pqu);
}
break;
}
else if (bbt->right == NULL)
{
bbt->right = new_node_bt;
while (front != NULL)//插入二叉树节点后,释放整个队列
{
pqu = front;
front = front->next;
free(pqu);
}
break;
}
else
{
pqu = (node_queue*)malloc(sizeof(node_queue));//左节点入队
pqu->node = bbt->left;
pqu->next = NULL;
rear->next = pqu;
rear = pqu;
pqu = (node_queue*)malloc(sizeof(node_queue));//右节点入队
pqu->node = bbt->right;
pqu->next = NULL;
rear->next = pqu;
rear = pqu;
pqu = front;//出队1个
front = front->next;
free(pqu);
}
}
}
}
void traverse_preorder_recurrence(node_bt *root)//递归先序遍历
{
if (root == NULL)
return;
printf("%d ", root->data);
traverse_preorder_recurrence(root->left);
traverse_preorder_recurrence(root->right);
}
void traverse_preorder_iteration(node_bt *root)//迭代先序遍历
{
node_bt *bt = root;
node_stack *st, *top = NULL;//构建辅助链栈
while (top != NULL || bt != NULL)
{
while (bt != NULL)//迭代访问节点的左子数节点,并入栈
{
printf("%d ", bt->data);
st = (node_stack*)malloc(sizeof(node_stack));//入栈
st->next = top;
st->node = bt;
top = st;
bt = bt->left;
}
bt = top->node->right;//出栈
st = top;
top = top->next;
free(st);
}
}
void traverse_inorder_recurrence(node_bt *root)//递归中序遍历
{
if (root == NULL)
return;
traverse_inorder_recurrence(root->left);
printf("%d ",root->data);
traverse_inorder_recurrence(root->right);
}
void traverse_inorder_iteration(node_bt *root)//迭代中序遍历
{
node_bt *bt = root;
node_stack *st, *top = NULL;//构建辅助链栈
while (bt!= NULL || top != NULL)
{
while (bt != NULL)//迭代访问节点的左子数节点,并入栈
{
st = (node_stack*)malloc(sizeof(node_stack));//入栈
st->next = top;
st->node = bt;
top = st;
bt = bt->left;
}
bt = top->node;//出栈
st = top;
top = top->next;
free(st);
printf("%d ", bt->data);
bt = bt->right;
}
}
void traverse_postorder_recurrence(node_bt *root)//递归后序遍历
{
if (root != NULL)
{
traverse_postorder_recurrence(root->left);
traverse_postorder_recurrence(root->right);
printf("%d ", root->data);
}
}
void traverse_postorder_iteration(node_bt *root)//迭代后序遍历
{
node_bt *bt = root, *last_bt = NULL;
node_stack *st, *top = NULL;//构建辅助链栈
while (bt != NULL || top != NULL)
{
while (bt != NULL)//迭代访问节点的左子数节点,并入栈
{
st = (node_stack*)calloc(1, sizeof(node_stack));//入栈
st->next = top;
st->node = bt;
top = st;
bt = bt->left;
}
bt = top->node;//出栈
st = top;
top = top->next;
free(st);
if (bt->right == NULL || bt->right == last_bt)
{
printf("%d ", bt->data);
last_bt = bt;
bt = NULL;
}
else
{
st = (node_stack*)calloc(1, sizeof(node_stack));//入栈
st->next = top;
st->node = bt;
top = st;
bt = bt->right;
}
}
}
void traverse_level(node_bt *root)//层序遍历
{
node_bt *bt;
node_queue *qu = (node_queue*)malloc(sizeof(node_queue)), *rear = qu, *front = qu;//构建辅助链队列
qu->next = NULL;
qu->node = root;
while (rear != NULL)
{
bt = rear->node;//出队
qu = rear;
rear = rear->next;
free(qu);
printf("%d ", bt->data);
if (bt->left != NULL)
{
qu = (node_queue*)malloc(sizeof(node_queue));//入队
qu->next = NULL;
qu->node = bt->left;
front->next = qu;
front = qu;
if (rear == NULL)//空队列入队时,对尾指针赋值
rear = qu;
}
if (bt->right != NULL)
{
qu = (node_queue*)malloc(sizeof(node_queue));//入队
qu->next = NULL;
qu->node = bt->right;
front->next = qu;
front = qu;
if (rear == NULL)//空队列入队时,对尾指针赋值
rear = qu;
}
}
}
void add(node_bt *root)//增加节点(层序增加)
{
node_bt *bt, *new_bt = (node_bt*)malloc(sizeof(node_bt));
node_queue *qu = (node_queue*)malloc(sizeof(node_queue)), *rear = qu, *front = qu;//构建辅助链队列
qu->next = NULL;
qu->node = root;
printf("请输入插入节点的数值:");
scanf_s("%d", &new_bt->data);
new_bt->left = new_bt->right = NULL;
while (rear != NULL)
{
bt = rear->node;//出队
qu = rear;
rear = rear->next;
free(qu);
if (bt->left != NULL)
{
qu = (node_queue*)malloc(sizeof(node_queue));//入队
qu->next = NULL;
qu->node = bt->left;
front->next = qu;
front = qu;
if (rear == NULL)//空队列入队时,对尾指针赋值
rear = qu;
}
else//子数节点为空,即为找到插入点,插入后直接退出
{
bt->left = new_bt;
break;
}
if (bt->right != NULL)
{
qu = (node_queue*)malloc(sizeof(node_queue));//入队
qu->next = NULL;
qu->node = bt->right;
front->next = qu;
front = qu;
if (rear == NULL)//空队列入队时,对尾指针赋值
rear = qu;
}
else//子数节点为空,即为找到插入点,插入后直接退出
{
bt->right = new_bt;
break;
}
}
}
void number_leaf(node_bt *root)//求叶子节点个数
{
//可以直接遍历,对度为0的节点计数,这里采用迭代先序遍历,其他遍历方式类似
int num = 0;
node_bt *bt = root;
node_stack *st, *top = NULL;//构建辅助链栈
while (top != NULL || bt != NULL)
{
while (bt != NULL)
{
if (bt->left == NULL && bt->right == NULL)
num++;
st = (node_stack*)malloc(sizeof(node_stack));//入栈
st->next = top;
st->node = bt;
top = st;
bt = bt->left;
}
bt = top->node->right;//出栈
st = top;
top = top->next;
free(st);
}
printf("叶子节点有:%d个\n", num);
}
int depth_recurrence(node_bt *root)//递归求树深度
{
int dl, dr;
if (root == NULL)
return 0;
dl = depth_recurrence(root->left);
dr = depth_recurrence(root->right);
return dr > dl ? dr + 1 : dl + 1;
}
void depth_iteration(node_bt *root)//迭代求树深度
{
typedef struct stack_depth//辅助链栈,比用于遍历的链栈多了一个深度项
{
node_bt *node;
struct stack_depth *next;
int depth;
}node_sd;
int max_depth = 0,current_depth = 0;
node_bt *bt = root;
node_sd *st, *top = NULL;
while (bt != NULL || top != NULL)
{
while (bt != NULL)
{
st = (node_sd*)malloc(sizeof(node_sd));//入栈
st->depth = ++current_depth;
st->next = top;
st->node = bt;
top = st;
bt = bt->left;
}
bt = top->node;//出栈
current_depth = top->depth;
st = top;
top = top->next;
free(st);
max_depth = max_depth > current_depth ? max_depth : current_depth;
bt = bt->right;
}
printf_s("二叉树深度为:%d\n", max_depth);
}