先序遍历二叉树的递归合非递归方式在我之前的博客中分享过了,博客加入了二叉树的线索化,构造先序线索二叉树的这么一种方式。关于中序线索二叉树的构建及其线索化方法在严蔚敏的书算法描述得很详尽了,后序线索二叉树的线索化过程,需要使用三叉链表的结构,这里只描述先序线索化方法,注释和相关描述比较详尽了,这些代码可直接上机调试Talking is cheap,just show code:
/*
* The Ways of PreOrderTraverse Binary Tree.
* Just Practice Building PreOrderThreading Binary Tree.
* Understand the Thinking of Binary Tree Threading.
*
* In My View,Building "PreOrderThread Tree" Needs the Fllowing Steps:
* 1.Create a Binary Tree
* 2.Threading
* 1) Create a "Head Node" of This Tree
* 2) Traversing the Tree in "PreOrder Way"
* -> Threading this Node.Just Threading The NULL Pointer(Building the Link Between "this(->lchild)" and "pre(->rchild)")
* -> Threading(this->lchild)
* -> Threading(this->rchild)
* 3) Don't Forget Threading The Final Node (rchild = Thrt)
* 3.Traversing(Dependence Its Traverseing Way)
*
*/
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_MAXSIZE 16
#define STACK_INCREMENT 10
#define DEBUG 1
typedef enum PointerTag{
Link,
Thread
}PointerTag;
typedef int Status;
typedef char Elemtype;
typedef struct BinThrNode{
Elemtype data;
struct BinThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BinThrNode,*BinThrTree;
typedef struct{
BinThrTree *base,*top;
int length;
}SqStack;
static BinThrTree pre;
/* ----- Stack Operation ----- */
static Status InitStack(SqStack *Stack)
{
if(!(Stack->base = (BinThrTree *)malloc(STACK_MAXSIZE * sizeof(BinThrTree))))
exit(OVE