1.全局变量
typedef struct ThreadNode {ELemType data;struct ThreadNode *lchild, *rchild;int ltag, rtag;
}ThreadNode, *ThreadTree;ThreadNode *pre = NULL;
1.1中序线索化
void CreateInThread(ThreadTree T) {pre = NULL;if(T !== NULL) {InThread(T);if(pre -> rchild == NULL)pre -> rtag = 1;}
}void InThread(ThreadTree T) {if(T != NULL) {InTread(T -> lchild);visit(T);InTread(T -> rchild);}
}void visit(ThreadNode *q) {if(q -> lchild == NULL) {q -> lchild = pre;q -> ltag = 1;}if(pre != NULL&& pre -> rchild == NULL) {pre -> rchild = q;pre -> rlog = 1;}pre = q;
}
1.2先序线索化
void CreatePreThread(ThreadTree T) {pre = NULL;if(T !== NULL) {PreThread(T);if(pre -> rchild == NULL)pre -> rtag = 1;}
}void InThread(ThreadTree T) {if(T != NULL) {visit(T);if(T -> ltag == 0)PreThread(T -> lchild);PreThread(T -> rchild);}
}void visit(ThreadNode *q) {if(q -> lchild == NULL) {q -> lchild = pre;q -> ltag = 1;}if(pre != NULL&& pre -> rchild == NULL) {pre -> rchild = q;pre -> rlog = 1;}pre = q;
}
1.3后序线索化
void CreatePostThread(ThreadTree T) {pre = NULL;if(T !== NULL) {PostThread(T);if(pre -> rchild == NULL)pre -> rtag = 1;}
}void InThread(ThreadTree T) {if(T != NULL) {PostThread(T -> lchild);PostThread(T -> rchild);visit(T);}
}void visit(ThreadNode *q) {if(q -> lchild == NULL) {q -> lchild = pre;q -> ltag = 1;}if(pre != NULL&& pre -> rchild == NULL) {pre -> rchild = q;pre -> rlog = 1;}pre = q;
}
2.引用
typedef struct ThreadNode {ELemType data;struct ThreadNode *lchild, *rchild;int ltag, rtag;
}ThreadNode, *ThreadTree;
2.1中序线索化
void CreateInThread(ThreadTree T) {ThreadTree pre = NULL;if(T != NULL) {InThread(T,pre);pre -> rchild = NULL;pre -> rtag = 1;}
}void InThread(ThreadTree &p, ThreadTree &pre) {if(p != NULL) {InThread(p -> lchild, pre);if(p -> lchild == NULL){p -> lchild == pre;p -> ltag == 1;}if(pre != NULL && pre -> rchild == NULL) {pre -> rchild = p;pre -> rtag = 1;}pre = p;InThread(p -> rchild, pre);}
}
2.2先序线索化
void CreatePreThread(ThreadTree T) {ThreadTree pre = NULL;if(T != NULL) {PreThread(T,pre);pre -> rchild = NULL;pre -> rtag = 1;}
}void PreThread(ThreadTree &p, ThreadTree &pre) {if(p != NULL) {if(p -> lchild == NULL){p -> lchild == pre;p -> ltag == 1;}if(pre != NULL && pre -> rchild == NULL) {pre -> rchild = p;pre -> rtag = 1;}pre = p;if(T -> ltag == 0)PreThread(p -> lchild, pre);PreThread(p -> rchild, pre);}
}
2.3后序线索化
void CreatePostThread(ThreadTree T) {ThreadTree pre = NULL;if(T != NULL) {PostThread(T,pre);pre -> rchild = NULL;pre -> rtag = 1;}
}void PostThread(ThreadTree &p, ThreadTree &pre) {if(p != NULL) {PostThread(p -> lchild, pre);PostThread(p -> rchild, pre);if(p -> lchild == NULL){p -> lchild == pre;p -> ltag == 1;}if(pre != NULL && pre -> rchild == NULL) {pre -> rchild = p;pre -> rtag = 1;}pre = p;}
}