线索二叉树,就好像是给排列在不同地方的对象用一根无形的绳子穿起来了,变成了“线形”的,这样每个结点的前驱和后继就能直接得到或者容易找到了。
线索化的过程需要用到遍历算法,一旦线索建立后,在此结构上遍历就可以不用递归、也不用栈了,实现更加容易了
头文件 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;
Status visit(Element e)
{
printf("%c ",e)

线索二叉树通过线索将原本非线性的二叉树结构转换为线性结构,便于直接获取节点的前驱和后继。在线索化过程中,利用遍历算法,建立线索后可以简化遍历操作,避免递归和使用栈。本文包含头文件BitTree.h、实现文件BitTree.cpp以及测试文件main.cpp。
最低0.47元/天 解锁文章
5977

被折叠的 条评论
为什么被折叠?



