您的位置:首页 > 其它

非递归前中后序遍历二叉树

2015-06-25 21:25 92 查看
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
写在前面:
最近准备找工作,捡起原来学习过的各种知识,加上一些自己的理解,梳理一下流程,巩固自己的认识,一步两步,一步两步。。。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二叉树的遍历是树操作的基础,一般的前中后序递归遍历比较简单,这里就不列出了,主要是非递归实现,不GG,直接上代码

一、test.cpp文件
#include <stdio.h>
#include <malloc.h>
#include "order.h"
#include "stdlib.h"

//创建树的时候注意,依次输入:
//比如,一棵3个节点的完全二叉树输入为:ab空格空格c空格空格回车
int create_BitTree(BitTree &T)
{
char temp;
scanf("%c",&temp);

if(temp==' ')
{
T=NULL;
//printf("create_BitTree is 0\n");
}
else
{
if (!(T=(BitTree)malloc(sizeof(BitNode))))
{
printf("create_BitTree is wrong\n");
exit(1);
}
T->data = temp;
create_BitTree(T->leftchild);
create_BitTree(T->rightchild);

}
return 1;
}

int main(void)
{
BitTree t;
create_BitTree(t);
printf("create_BitTree is ok\n");
pre_order(t,pre_order_print_char);
in_order(t,in_order_print_char);
post_order(t,post_order_print_char);

}
二、order.cpp文件
#include "order.h"
#include "stdio.h"
#include "stdlib.h"
#include <string.h>

//先序遍历
void pre_order(BitTree T,void (*visit)(TElemType e))
{
Stack ss;
init_stack(ss); //初始化栈
BitTree p=T;

printf("先序非递归遍历为:\n");
while ( p || !stack_is_empty(ss))
{
if( p )
{
visit(p->data);
push_stack(ss,p);
p = p->leftchild;
}
else
{
pop_stack(ss,p);
p = p ->rightchild;
}

}

}

//中序遍历
void in_order(BitTree T,void (*visit)(TElemType e))
{
Stack ss;
BitTree p=T;

init_stack(ss);
printf("\n中序非递归遍历为:\n");
while (p||!stack_is_empty(ss))
{
if (p)
{
push_stack(ss,p);
p = p ->leftchild;
}
else
{
pop_stack(ss,p);
visit(p->data);
p = p ->rightchild;
}
}

}

//后序遍历
void post_order(BitTree T,void (*visit)(TElemType e))
{
BitTree p=T,pre,temp2;
BitNode temp;
Stack ss;
init_stack(ss);
printf("\n后序非递归遍历为:\n");
while (p || !stack_is_empty(ss))
{
if (p)
{
push_stack(ss,p);
p = p->leftchild;
}
else
{
temp2 = get_top_stack(ss);
if (!temp2->rightchild || temp2->rightchild==pre)//后面个条件表示右子树已经扫描过了
{
pop_stack(ss,p);
visit(p->data);//输出
pre=temp2;
p = NULL;
}
else
{
p = temp2->rightchild;
}
}

}

}

void init_stack(Stack &s)
{
s.base=(BitTree*)malloc(sizeof(BitTree)*STACK_INIT_SIZE);
memset(s.base, 0, sizeof(BitTree)*STACK_INIT_SIZE);
if (!s.base)
{
printf("init_stack is wrong");
exit(1);//退出
}

s.top=s.base;
s.stack_size=STACK_INIT_SIZE;
}

int stack_is_empty(Stack s)
{
if (s.top==s.base)
{
return 1;
}
else
return 0;
}

void push_stack(Stack &s, BitTree t)
{
if (s.top-s.base>=s.stack_size)
{
s.base=(BitTree*)realloc(s.base,sizeof(BitTree)*(STACK_INIT_SIZE*2));//分配更大的空间
}
*s.top++ = t;
}

void pop_stack(Stack &s,BitTree	&t)
{
if (s.top==s.base)
{
printf("pop_stack is wrong");
exit(1);//退出
}
t=*--s.top;
//	printf("%c",t->data);

}

BitTree get_top_stack(Stack s)
{
if (s.top-s.base>0)
{
return *--s.top;
}
else
exit(1);
}
void pre_order_print_char(TElemType CC)
{
printf("%c -> ",CC);
}

void in_order_print_char(TElemType CC)
{
printf("%c -> ",CC);
}

void post_order_print_char(TElemType CC)
{
printf("%c -> ",CC);
}


三、order.h文件
#ifndef ORDER_H
#define ORDER_H
#define STACK_INIT_SIZE 30
typedef char TElemType;

typedef struct Node
{
TElemType data;
struct Node *leftchild,*rightchild;
}BitNode,*BitTree;

typedef struct stack
{
BitTree *top;
BitTree *base;
int stack_size;
}Stack;

typedef void (*visit)(TElemType);	//定义一个函数指针类型
void pre_order(BitTree T,void (*visit)(TElemType e));
void in_order(BitTree T,void (*visit)(TElemType e));
void post_order(BitTree T,void (*visit)(TElemType e));

void pre_order_print_char(TElemType CC);
void in_order_print_char(TElemType CC);
void post_order_print_char(TElemType CC);
BitTree get_top_stack(Stack s);
void printChar(TElemType CC);
void init_stack(Stack &s);
void push_stack(Stack &s, BitTree t);
void pop_stack(Stack &s,BitTree	&t);
int stack_is_empty(Stack s);
#endif


四、测试
用网上的一个例子作为程序的输入



输入的时候:ABD空格空格EH空格空格空格CF空格I空格空格G空格空格

五、运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: