#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); }
#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
原文地址:http://blog.csdn.net/u010691898/article/details/46641261