#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct tnode{
char data;
struct tnode *lchild;
struct tnode *rchild;
}TNode, *TLink;
typedef struct node{
TLink tree_add;
struct node *next;
}Node, *Link;
enum return_val {EMPTY_OK = 1000, EMPTY_NO};
void is_malloc_ok_node(Link node)
{
if(node == NULL)
{
printf("malloc error\n");
exit(-1);
}
}
void create_new_node(Link *node) //创建节点
{
*node = (Link)malloc(sizeof(Node));
is_malloc_ok_node(*node);
}
void create_stack(Link *stack) //创建栈
create_new_node(stack);
(*stack)->next = NULL;
}
void push_stack(Link stack, TLink tree_add) //进栈
{
Link node;
create_new_node(&node);
node->tree_add = tree_add;
node->next = stack->next;
stack->next = node;
}
int is_empty_stack(Link stack) //判断栈空
{
if(stack->next == NULL)
{
return EMPTY_OK;
}
}
int pop_stack(Link stack, TLink *tree_add) //出栈
{
Link p;
if(is_empty_stack(stack) == EMPTY_OK)
{
return EMPTY_OK;
}
*tree_add = stack->next->tree_add;
p = stack->next;
stack->next = p->next;
free(p);
return EMPTY_NO;
}
int get_stack_top(Link stack,TLink *tree_add) //获取栈顶元素
{
if(is_empty_stack(stack) == EMPTY_OK)
{
return EMPTY_OK;
}
*tree_add = stack->next->tree_add;
return EMPTY_NO;
}
void create_queue(Link *front, Link *rear) //创建队列
{
create_new_node(front);
*rear = *front;
(*front)->next = NULL;
}
void push_queue(Link *rear, TLink tree_add) //进队
{
Link node;
create_new_node(&node);
node->tree_add = tree_add;
node->next = (*rear)->next;
(*rear)->next = node;
*rear = node;
}
int is_empty_queue(Link front) //判断是否队空
{
if(front->next == NULL)
{
return EMPTY_OK;
}
}
int pop_queue(Link front, Link *rear, TLink *tree_add) //出队
{
Link p;
if(is_empty_queue(front) == EMPTY_OK)
{
return EMPTY_OK;
}
*tree_add = front->next->tree_add;
p = front->next;
front->next = p->next;
if(p->next == NULL)
{
*rear = front;
}
free(p);
return EMPTY_NO;
}
/*非递归遍历*/
void display_first(TLink tree_node) //先序
{
Link stack;
create_stack(&stack);
while(tree_node != NULL || is_empty_stack(stack) != EMPTY_OK)
{
while(tree_node != NULL)
{
push_stack(stack, tree_node);
printf(" %c", tree_node->data);
tree_node = tree_node->lchild;
}
pop_stack(stack,&tree_node);
tree_node = tree_node->rchild;
free(stack);
}
void display_mid(TLink tree_node) //中序
{
Link stack;
create_stack(&stack);
while(tree_node != NULL || is_empty_stack(stack) != EMPTY_OK)
{
while(tree_node != NULL)
{
push_stack(stack, tree_node);
tree_node = tree_node->lchild;
}
pop_stack(stack, &tree_node);
printf(" %c", tree_node->data);
tree_node = tree_node->rchild;
}
free(stack);
}
void display_last(TLink tree_node) //后序
{
Link stack;
TLink prior_add, root;
root = tree_node;
create_stack(&stack);
while(tree_node != NULL || is_empty_stack(stack) != EMPTY_OK)
{
while(tree_node != NULL)
{
push_stack(stack,tree_node);
tree_node = tree_node->lchild;
}
if(get_stack_top(stack, &tree_node) == EMPTY_OK)
{
break;
}
if(tree_node->rchild == NULL)
{
pop_stack(stack, &tree_node);
prior_add = tree_node;
printf(" %c", tree_node->data);
}
if(get_stack_top(stack, &tree_node) == EMPTY_OK)
{
break;
}
if(tree_node->rchild == prior_add)
{
pop_stack(stack, &tree_node);
prior_add = tree_node;
printf(" %c", tree_node->data);
}
if(get_stack_top(stack, &tree_node) == EMPTY_OK)
{
break;
}
if(tree_node == root && tree_node->rchild == prior_add)
{
pop_stack(stack, &tree_node);
printf(" %c", tree_node->data);
break;
}
else
{
tree_node = tree_node->rchild;
}
}
free(stack);
}
void display_level(TLink tree_node) //层序
{
Link front, rear;
int i = 0;
create_queue(&front, &rear);
push_queue(&rear, tree_node);
while(is_empty_queue(front) != EMPTY_OK)
{
pop_queue(front,&rear, &tree_node);
printf(" %c", tree_node->data);
if(tree_node->lchild != NULL)
{
push_queue(&rear,tree_node->lchild);
}
if(tree_node->rchild != NULL)
{
push_queue(&rear, tree_node->rchild);
}
}
free(front);
}
void is_malloc_ok_tree(TLink tree_node)
{
if(tree_node == NULL)
{
printf("malloc error\n");
exit(-1);
}
}
void create_tree(TLink *tree_node) //先序创建树
{
char n;
scanf("%c", &n);
getchar();
if(n == '#')
{
*tree_node = NULL;
}
else
{
*tree_node = (TLink)malloc(sizeof(TNode));
is_malloc_ok_tree(*tree_node);
(*tree_node)->data = n;
create_tree(&(*tree_node)->lchild);
create_tree(&(*tree_node)->rchild);
}
}
/*递归遍历*/
/*void display_first(TLink tree_node) //先序
{
if(tree_node != NULL)
{
printf(" %c", tree_node->data);
display_first(tree_node->lchild);
display_first(tree_node->rchild);
}
else
{
return;
}
}
void display_mid(TLink tree_node) //中序
{
if(tree_node != NULL)
{
display_mid(tree_node->lchild);
printf(" %c",tree_node->data);
display_mid(tree_node->rchild);
}
else
{
return;
}
}
void display_last(TLink tree_node) //后序
{
if(tree_node != NULL)
{
display_last(tree_node->lchild);
display_last(tree_node->rchild);
printf(" %c",tree_node->data);
}
else
{
return;
}
}*/
int main()
{
TLink root;
create_tree(&root);
display_first(root);
printf("\n");
display_mid(root);
printf("\n");
display_last(root);
printf("\n");
display_level(root);
printf("\n");
return 0;
}
二叉树先序创建,先序、中序、后序遍历递归算法和非递归算法
最新推荐文章于 2023-04-07 00:38:53 发布