C语言编写非递归遍历代码,实现:给定一棵二叉树的先序遍历序列和中序遍历序列,创建这棵二叉树。 (1)输出二叉树的后序遍历的结点序列。 (2)输出二叉树的叶子结点。 (3)统计二叉树的结点个数。 (4)求二叉树的深度。 (5)输出二叉树指定结点的路径。
时间: 2024-05-20 12:13:13 浏览: 91
举例讲解C语言程序中对二叉树数据结构的各种遍历方式
好的,您的问题是如何用 C 语言编写非递归遍历代码,实现:给定一棵二叉树的先序遍历序列和中序遍历序列,创建这棵二叉树,并完成下列任务:
(1) 输出二叉树的后序遍历的结点序列。
(2) 输出二叉树的叶子结点。
(3) 统计二叉树的结点个数。
(4) 求二叉树的深度。
(5) 输出二叉树指定结点的路径。
以下是完整的代码实现:
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
typedef struct node {
char data;
struct node *left_child, *right_child;
}Node;
typedef struct stack {
Node* data[STACK_SIZE];
int top;
}Stack;
//创建一个节点
Node* create_node(char data){
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->left_child = new_node->right_child = NULL;
return new_node;
}
//把一颗二叉树的先序遍历和中序遍历分别存到两个数组中,重建这颗二叉树
Node* construct_binary_tree(char* pre_order, char* in_order, int num){
if(pre_order == NULL || in_order == NULL || num <= 0){
return NULL;
}
Node *root = create_node(*pre_order);
char* root_inorder = in_order;
while(*root_inorder != *pre_order && root_inorder < in_order + num - 1) {
root_inorder++;
}
if(*root_inorder != *pre_order) {
printf("Invalid Input\n");
return NULL;
}
int left_length = root_inorder - in_order;
int right_length = in_order + num - 1 - root_inorder;
root->left_child = construct_binary_tree(pre_order+1, in_order, left_length);
root->right_child = construct_binary_tree(pre_order+1+left_length, root_inorder+1, right_length);
return root;
}
//打印出一个节点的信息
void print_node(Node * p) {
printf("%c ", p->data);
}
//非递归先序遍历
void pre_order_traversal(Node *root) {
Stack stack;
stack.top = -1;
Node *p = root;
while(p != NULL || stack.top != -1) {
while(p != NULL) {
print_node(p);
stack.data[++stack.top] = p;
p = p->left_child;
}
if(stack.top != -1) {
p = stack.data[stack.top--];
p = p->right_child;
}
}
}
//非递归中序遍历
void in_order_traversal(Node *root) {
Stack stack;
stack.top = -1;
Node *p = root;
while(p != NULL || stack.top != -1) {
while(p != NULL) {
stack.data[++stack.top] = p;
p = p->left_child;
}
if(stack.top != -1) {
p = stack.data[stack.top--];
print_node(p);
p = p->right_child;
}
}
}
//非递归后序遍历
void post_order_traversal(Node *root) {
Stack stack;
stack.top = -1;
Node *p = root;
Node * last_visited_node = NULL;
while(p != NULL || stack.top != -1) {
while(p != NULL) {
stack.data[++stack.top] = p;
p = p->left_child;
}
p = stack.data[stack.top];
if(p->right_child == NULL || last_visited_node == p->right_child) {
print_node(p);
last_visited_node = p;
stack.top--;
p = NULL;
} else {
p = p->right_child;
}
}
}
//非递归求叶子节点
void leaf_traversal(Node* root) {
Stack stack;
stack.top = -1;
Node *p = root;
while(p != NULL || stack.top != -1) {
while(p != NULL) {
stack.data[++stack.top] = p;
p = p->left_child;
}
if(stack.top != -1) {
p = stack.data[stack.top--];
if(p->right_child == NULL && p->left_child == NULL){
printf("%c ", p->data);
}
p = p->right_child;
}
}
}
//递归求二叉树结点数目
int node_number(Node* root){
if(root == NULL){
return 0;
} else{
return node_number(root->left_child) + node_number(root->right_child) + 1;
}
}
//非递归求二叉树深度
int depth(Node* root) {
Stack stack;
stack.top = -1;
Node *p = root;
int dep = 0, max_depth = 0;
Node *last_visited_node = NULL;
while(p != NULL || stack.top != -1) {
while(p != NULL) {
stack.data[++stack.top] = p;
dep++;
if(dep > max_depth){
max_depth = dep;
}
p = p->left_child;
}
p = stack.data[stack.top];
if(p->right_child == NULL || last_visited_node == p->right_child) {
last_visited_node = p;
stack.top--;
p = NULL;
dep--;
} else {
p = p->right_child;
}
}
return max_depth;
}
//搜索一个节点,并将其路径压入栈中
Node* search_node(Node* root, char data, Stack* stack)
{
if(root == NULL){
return NULL;
}
if(root->data == data){
stack->data[++stack->top] = root;
return root;
}
Node* left_result = search_node(root->left_child, data, stack);
if(left_result == NULL){
Node* right_result = search_node(root->right_child, data, stack);
if(right_result == NULL){
stack->top--;
return NULL;
}
}
stack->data[++stack->top] = root;
return root;
}
// 输出栈中的路径
void print_path(Stack *stack) {
for(int i=stack->top; i>=0; i--){
print_node(stack->data[i]);
if(i != 0) {
printf("->");
}
}
}
//获取并输出给定节点的路径
void get_path(Node* root, char data) {
Stack stack;
stack.top = -1;
search_node(root, data, &stack);
print_path(&stack);
}
int main() {
char pre_order[] = {'A', 'B', 'D', 'E', 'C', 'F', 'G'};
char in_order[] = {'D', 'B', 'E', 'A', 'F', 'C', 'G'};
int len = sizeof(pre_order) / sizeof(pre_order[0]);
Node* root = construct_binary_tree(pre_order, in_order, len);
printf("先序遍历:");
pre_order_traversal(root);
printf("\n中序遍历:");
in_order_traversal(root);
printf("\n后序遍历:");
post_order_traversal(root);
printf("\n叶子结点:");
leaf_traversal(root);
printf("\n结点数目:%d\n", node_number(root));
printf("树的深度:%d\n",depth(root));
printf("结点'E'的路径:");
get_path(root, 'E');
printf("\n");
return 0;
}
阅读全文