线索二叉树,就好像是给排列在不同地方的对象用一根无形的绳子穿起来了,变成了“线形”的,这样每个结点的前驱和后继就能直接得到或者容易找到了。
线索化的过程需要用到遍历算法,一旦线索建立后,在此结构上遍历就可以不用递归、也不用栈了,实现更加容易了
头文件 BitTree.h
#ifndef BITTREE_H
#define BITTREE_H
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef enum{Link,Thread} PointTag;
typedef char Element;
typedef int Status;
typedef struct Node{
Element data;
struct Node* left;
struct Node* right;
PointTag Ltag;
PointTag Rtag;
}BitNode,*BitTree;
Status visit(Element e);
Status CreateTree(BitTree &T);
void InThreading(BitTree p);
void InOrderThreading(BitTree &H,BitTree T);
BitTree Parent(BitTree Thr,BitTree p);
void InOrderTraverse_Thr(BitTree L);
#endif //BITTREE_H
实现文件 BitTree.cpp
#include "BitTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
BitTree pre;