1 线索二叉树的创建
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
typedef char DataType;
typedef struct BiTNode {
DataType data;
int LTag;
int RTag;
struct BiTNode * LChild;
struct BiTNode * RChild;
struct BiTNode * parent;
} BiTNode, * BiTree;
void Visit ( DataType data)
{
printf ( "%c, " , data) ;
}
void CreateBiTree ( BiTree * root)
{
char ch;
ch = getchar ( ) ;
if ( ch == '.' ) * root = NULL ;
else {
* root = ( BiTNode * ) malloc ( sizeof ( BiTNode) ) ;
( * root) -> data = ch;
( * root) -> LTag = 0 ;
( * root) -> RTag = 0 ;
( * root) -> parent = nullptr ;
CreateBiTree ( & ( ( * root) -> LChild) ) ;
CreateBiTree ( & ( ( * root) -> RChild) ) ;
}
}
void CreateBiTree2 ( BiTree * root, BiTNode * parent)
{
char ch = getchar ( ) ;
if ( ch == '.' ) * root = NULL ;
else {
* root = ( BiTNode * ) malloc ( sizeof ( BiTNode) ) ;
( * root) -> data = ch;
( * root) -> LTag = 0 ;
( * root) -> RTag = 0 ;
( * root) -> parent = parent;
CreateBiTree2 ( & ( ( * root) -> LChild) , * root) ;
CreateBiTree2 ( & ( ( * root) -> RChild) , * root) ;
}
}
2 【将一棵树中序线索化、求中序遍历的第一个节点、中序遍历的前驱、中序遍历的后继、中序线索二叉树的遍历】
BiTNode * pre = NULL ;
void InThread ( BiTree root)
{
if ( root == NULL ) return ;
InThread ( root-> LChild) ;
if ( root-> LChild == NULL ) {
root-> LTag = 1 ;
root-> LChild = pre;
}
if ( pre != NULL && pre-> RChild == NULL ) {
pre-> RTag = 1 ;
pre-> RChild = root;
}
pre = root;
InThread ( root-> RChild) ;
}
BiTNode * InFirst ( BiTree root)
{
if ( root == NULL ) return NULL ;
BiTNode * p = root;
while ( p-> LTag == 0 ) {
p = p-> LChild;
}
return p;
}
BiTNode * InPre ( BiTree root)
{
BiTNode * pre, * p;
if ( root-> LTag == 1 )
pre = root-> LChild;
else {
for ( p = root-> LChild, pre = p; p-> RTag == 0 ; ) {
p = p-> RChild;
pre = p;
}
}
return pre;
}
BiTNode * InNext ( BiTree root)
{
BiTNode * next, * p;
if ( root-> RTag == 1 ) next = root-> RChild;
else {
for ( p = root-> RChild, next = p; p&& p-> LTag == 0 ; ) {
p = p-> LChild;
next = p;
}
}
return next;
}
void TranverseInThread ( BiTree root)
{
BiTNode * p = InFirst ( root) ;
while ( p) {
Visit ( p-> data) ;
p = InNext ( p) ;
}
}
3 【将一棵树先序线索化、求先序遍历的第一个节点、先序遍历的前驱、先序遍历的后继、中序线索二叉树的遍历】
void PreThread ( BiTree root, BiTNode * & pre)
{
if ( root == NULL ) return ;
BiTNode * lchildTemp = root-> LChild;
if ( root-> LChild == NULL ) {
root-> LTag = 1 ;
root-> LChild = pre;
}
if ( pre != NULL && pre-> RChild == NULL ) {
pre-> RTag = 1 ;
pre-> RChild = root;
}
pre = root;
PreThread ( lchildTemp, pre) ;
PreThread ( root-> RChild, pre) ;
}
void CreatePreThread ( BiTree root)
{
BiTNode * pre = NULL ;
if ( root != NULL ) {
PreThread ( root, pre) ;
pre-> RChild = NULL ;
pre-> RTag = 1 ;
}
}
BiTNode* PreFirst ( BiTree root)
{
if ( root == NULL ) return NULL ;
else
return root;
}
void FillParent ( BiTree root)
{
if ( root != NULL )
{
if ( root-> LChild)
root-> LChild-> parent = root;
if ( root-> RChild)
root-> RChild-> parent = root;
FillParent ( root-> LChild) ;
FillParent ( root-> RChild) ;
}
}
BiTNode * PrePre ( BiTree root)
{
if ( ! root) return NULL ;
if ( root-> parent == NULL )
return NULL ;
BiTNode * parent = root-> parent;
if ( parent-> LChild == root)
return parent;
if ( parent-> RChild == root && parent-> LTag == 1 )
return parent;
if ( parent-> RChild == root && parent-> LTag == 0 ) {
BiTNode * leftRightMost = parent-> LChild;
while ( leftRightMost-> RTag == 0 )
leftRightMost = leftRightMost-> RChild;
return leftRightMost;
}
}
BiTNode * PreNext ( BiTree root)
{
if ( root == NULL ) return NULL ;
if ( root-> LTag == 0 ) return root-> LChild;
else
return root-> RChild;
}
void TranversePreThread ( BiTree root)
{
BiTNode * p = PreFirst ( root) ;
while ( p) {
printf ( "%c," , p-> data) ;
p = PreNext ( p) ;
}
}
4 【将一棵树先序线索化、求先序遍历的第一个节点、先序遍历的前驱、先序遍历的后继、中序线索二叉树的遍历】
void PostThread ( BiTree root , BiTNode * & pre)
{
if ( root == NULL ) return ;
PostThread ( root-> LChild, pre) ;
PostThread ( root-> RChild, pre) ;
if ( root-> LChild == NULL ) {
root-> LTag = 1 ;
root-> LChild = pre;
}
if ( pre != NULL && pre-> RChild == NULL ) {
pre-> RTag = 1 ;
pre-> RChild = root;
}
pre = root;
}
void CreatePostThread ( BiTree root)
{
BiTNode * pre = NULL ;
if ( root != NULL ) {
PostThread ( root, pre) ;
}
}
BiTNode* LeftDownMost ( BiTree root)
{
if ( root == NULL ) return NULL ;
BiTNode * p = root;
while ( p-> LTag == 0 )
p = p-> LChild;
return p;
}
BiTNode * PostFirst ( BiTree root)
{
if ( root == NULL ) return NULL ;
if ( root-> LTag == 0 )
return PostFirst ( root-> LChild) ;
else if ( root-> RTag == 0 )
return PostFirst ( root-> RChild) ;
else
return root;
}
BiTNode * PostPre ( BiTree root)
{
if ( root == NULL ) return NULL ;
if ( root-> RTag == 0 )
return root-> RChild;
else
return root-> LChild;
}
BiTNode * PostNext ( BiTree root)
{
if ( root == NULL ) return NULL ;
if ( root-> parent == NULL )
return NULL ;
BiTNode * parent = root-> parent;
if ( parent-> RChild == root)
return parent;
if ( parent-> LChild == root && parent-> RTag == 1 )
return parent;
if ( parent-> LChild == root && parent-> RTag == 0 )
return LeftDownMost ( parent-> RChild) ;
}
void TranversePostThread ( BiTree root)
{
BiTNode * p = PostFirst ( root) ;
while ( p) {
printf ( "%c," , p-> data) ;
p = PostNext ( p) ;
}
}
5 测试主程序
void PrintPreOrder ( BiTree root)
{
if ( root != NULL ) {
printf ( "%c," , root-> data) ;
PrintPreOrder ( root-> LChild) ;
PrintPreOrder ( root-> RChild) ;
}
}
int main ( )
{
BiTree root, nodePre;
CreateBiTree ( & root) ;
InThread ( root) ;
pre-> RTag = 1 ;
TranverseInThread ( root) ;
nodePre = InPre ( root-> LChild-> RChild) ;
if ( nodePre != NULL )
printf ( "The pre node of C is :%c\n" , nodePre-> data) ;
else
printf ( "\nThe node is the first one, no pre node!\n" ) ;
getchar ( ) ;
BiTree root2;
CreateBiTree ( & root2) ;
FillParent ( root2) ;
PrintPreOrder ( root2) ;
CreatePreThread ( root2) ;
printf ( "\nThe root2 PreThread tree is :\n" ) ;
TranversePreThread ( root2) ;
nodePre = PrePre ( root2) ;
if ( nodePre != NULL )
printf ( "\nThe pre node of D is :%c\n" , nodePre-> data) ;
else
printf ( "\nThe node is the first one, no pre node!\n" ) ;
getchar ( ) ;
BiTree root3;
CreateBiTree2 ( & root3, NULL ) ;
CreatePostThread ( root3) ;
TranversePostThread ( root3) ;
nodePre = PostPre ( root3) ;
if ( nodePre != NULL )
printf ( "\nThe pre node of A is :%c\n" , nodePre-> data) ;
else
printf ( "\nThe node is the first one, no pre node!\n" ) ;
}