二叉树的非递归的先序/中序/后序的遍历方法

#include <stdio.h>
#include <stdlib.h>
#define bj 0
typedef struct node_2{
int value;
struct node_2 *left_child;
struct node_2 *right_child;
}BinaryTree;


typedef struct node {
BinaryTree *value;
struct node *next;
}stack;




typedef struct node_1{
int count;
stack *top;
}Stack;


void Stack_init(Stack **p){
*p=(Stack*)malloc(sizeof(Stack));
(*p)->count=0;
(*p)->top=NULL;
}


void Stack_push(Stack *p,BinaryTree* date){
if(p==NULL)  return ;
stack *u=NULL;
u=(stack*)malloc(sizeof(stack));
u->value=date;
u->next=p->top;
p->top=u;
p->count++;
}


BinaryTree* Stack_pop(Stack *p){
if(p==NULL|| p->top==NULL)  return NULL;
BinaryTree *u=NULL;

stack *t=NULL;
t=p->top;

u=t->value;
p->top=p->top->next;
free(t);
t=NULL;
p->count--;
return u;
}




void create_BinaryTree(BinaryTree **p){
int date;
scanf("%d",&date);
if(date==bj)  return ;
*p=(BinaryTree*)malloc(sizeof(BinaryTree));
(*p)->value=date;
(*p)->left_child=NULL;
(*p)->right_child=NULL;
create_BinaryTree(&(*p)->left_child);
create_BinaryTree(&(*p)->right_child);
}




BinaryTree* create_1(){
BinaryTree *p=NULL;
p=(BinaryTree*)malloc(sizeof(BinaryTree));
p->value=1;

p->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->value=2;

p->left_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->left_child->value=4;
p->left_child->left_child->left_child=NULL;
p->left_child->left_child->right_child=NULL;


p->left_child->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->left_child->right_child->value=5;
p->left_child->right_child->left_child=NULL;
p->left_child->right_child->right_child=NULL;
    
    p->right_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->value=3;

p->right_child->left_child=(BinaryTree*)malloc(sizeof(BinaryTree));
p->right_child->left_child->value=6;
p->right_child->left_child->left_child=NULL;
p->right_child->left_child->right_child=NULL;

p->right_child->right_child=NULL;

return p;
}


void Pretraver(BinaryTree *p){
if(p==NULL)  return ;
printf("%d ",p->value);
Pretraver(p->left_child);
Pretraver(p->right_child);
}

//二叉树的非递归的先序遍历方式
void No_PreTraver_BinaryTree(BinaryTree *pTree)
{
if(pTree == NULL)return;


Stack *pStack = NULL;
Stack_init(&pStack);


while(1)
{
while(pTree)
{
printf("%d ",pTree->value);
Stack_push(pStack,pTree);
pTree = pTree->left_child;
}
pTree = Stack_pop(pStack);
if(pTree == NULL)break;
pTree = pTree->right_child;
}
}

//二叉树的非递归的中序的遍历方式
void No_PmTraver_BinaryTree(BinaryTree *b){
if(b==NULL)  return ;
Stack *s=NULL;
Stack_init(&s);
while(1){
if(b==NULL)  break ;
while(b->left_child){
Stack_push(s,b);
b=b->left_child;
}
if(b!=NULL){
printf("%d ",b->value);
}
else{
break;
}
b=Stack_pop(s);


if(b==NULL)  break;
else{
printf("%d ",b->value);
b=b->right_child;
}
}
}

//二叉树的非递归的后序的遍历方式
void No_PhTraver_BinaryTree(BinaryTree *b){
if(b==NULL)  return ;
Stack *s=NULL;
Stack_init(&s);
BinaryTree *bj_c=NULL;
BinaryTree *u=NULL;
u=b;
int bj_int=0;
while(1){
if(b==NULL)  break;
if(b==u && bj_int==1){
printf("%d ",b->value);
break;
}
while(b->left_child){
Stack_push(s,b);
b=b->left_child;
}
if(b!=NULL){
printf("%d ",b->value);
}
b=Stack_pop(s);
if(b==bj_c){
printf("%d ",b->value);
b=Stack_pop(s);
if(b==u&& bj_int==1){
printf("%d ",b->value);
break;
}
}
bj_c=b;
if(b->right_child!=NULL){
Stack_push(s,bj_c);
if(bj_c==u){
bj_int++;
}
b=b->right_child;
}
else{
printf("%d ",b->value);
b=Stack_pop(s);
}
}
}


int main()
{
BinaryTree *p=NULL;
create_BinaryTree(&p);
Pretraver(p);
printf("\n***************************************************\n");
BinaryTree *q=NULL;
q=create_1();
No_PreTraver_BinaryTree(q);
printf("\n***************************************************\n");
No_PmTraver_BinaryTree(q);
printf("\n***************************************************\n");
No_PhTraver_BinaryTree(q);
printf("\n***************************************************\n");
BinaryTree *u=NULL;
create_BinaryTree(&u);
// No_PmTraver_BinaryTree(u);
printf("\n***************************************************\n");
No_PhTraver_BinaryTree(u);
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42211587/article/details/80950543