×

线索二叉树的遍历 二叉树 线索二叉树

线索二叉树?C语言二叉树递归算法怎么做

admin admin 发表于2022-07-15 16:28:14 浏览87 评论0

抢沙发发表评论

线索二叉树


//二叉树的二叉线索存储表示
#include《stdio.h》
#include《stdlib.h》
typedef enum PointerTagpointerType;//Link==0,指针,Thread==1,线索
typedef char TElemType;
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild; //左右孩子指针
pointerType LTag,RTag; //左右标志
}BiThrNode,*BiThrTree;
BiThrTree pre; //前驱指针
void CreateBiThrTree(BiThrTree *T)//建树
{
char ch;
scanf(“%c“,&ch);
fflush(stdin);
if(ch==’ ’)
else
{
if(!(*T=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(EXIT_FAILURE);
(*T)-》data = ch;
(*T)-》lchild = (*T)-》rchild = NULL;
(*T)-》LTag = (*T)-》RTag = Link;
CreateBiThrTree(&(*T)-》lchild);
CreateBiThrTree(&(*T)-》rchild);
}
}
void InThreading(BiThrTree T) //建立线索
{
if(T)
{
InThreading(T-》lchild); //左子树线索化
if(!T-》lchild)//前驱线索
{
T-》LTag = Thread; T-》lchild = pre;
}
if(!pre-》rchild)//后继线索
{
pre-》RTag = Thread; pre-》rchild = T;
}
pre = T;
InThreading(T-》rchild);
}
}
void InOrderThreading(BiThrTree *Thrt,BiThrTree T)//建立线索头结点
{
//中序遍历二叉树T,并将其中序线索化,Thrt指向头结点。
if(!(*Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(1);
(*Thrt)-》LTag = Link; //建立头结点
(*Thrt)-》RTag = Thread;
(*Thrt)-》rchild = *Thrt; //右指针回指
if(!T) (*Thrt)-》lchild = *Thrt; //若二叉树空,左指针也回指
else
{
(*Thrt)-》lchild = T; pre = *Thrt;
InThreading(T); //进行中序线索化
pre-》rchild = *Thrt; pre-》RTag = Thread; //最后一个结点线索化
(*Thrt)-》rchild = pre;
}
}
void InOrderTraverse_Thr(BiThrTree T)//线索化中序遍历
{
//T指向头结点,头结点左链lchild指向根结点。
BiThrTree p;
p = T-》lchild; //p指向根结点

while(p!=T) //空树或遍历结束时,p==t
{
while(p-》LTag==Link) p = p-》lchild;
//左子树为空时访问
printf(“%c “,p-》data);
while(p-》RTag==Thread && p-》rchild!=T)
{
p = p-》rchild;
printf(“%c “,p-》data); //访问后继结点
}
p = p-》rchild;
}
}
int main(void)
{
BiThrTree tree,treeHead;

CreateBiThrTree(&tree);

InOrderThreading(&treeHead,tree);

printf(“\nInOrderTraverse:\n“);
InOrderTraverse_Thr(treeHead);
return 0;
}
-
+
a
this is blank
this is blank
*
b
this is blank
this is blank
-
c
《 ----这代表打空格
this is blank
this is blank
d
this is blank
this is blank
/
e
this is blank
this is blank
f
this is blank
this is blank
a + b * c - d - e / f
线索二叉树的遍历比较麻烦,没有把他都搞出来。上面是参考老严的代码。

C语言二叉树递归算法怎么做

#include 《stdio.h》
#include 《string.h》
struct treenode{
    int value;
    treenode* left;
    treenode* right;
};
typedef treenode* BiTree;
void visit(treenode* node)
{
    printf(“%2d “, node-》value);
}
//    结点总数 
int node(BiTree T)
{
    if( !T ){
        return 0;
    }
    return node(T-》left) + node(T-》right) + 1;
}
//    前序 
void preOrder(BiTree T)
{
    if( T ){
        visit(T);
        preOrder(T-》left);
        preOrder(T-》right);    
    }
}
//    中序
void inOrder(BiTree T)
{
    if( T ){
        inOrder(T-》left);
        visit(T);
        inOrder(T-》right);    
    }    

//    后序
void postOrder(BiTree T)
{
    if( T ){
        postOrder(T-》left);
        postOrder(T-》right);    
        visit(T);
    }    

//    叶子节点数
int leafnode(BiTree T)
{
    if( T ){
        if( !T-》left && !T-》right )
            return 1;
        else
            leafnode(T-》left) + leafnode(T-》right);    
    }else{
        return 0;
    }

int height(BiTree T)
{
    if( T ){
        int lh = height(T-》left);
        int rh = height(T-》right);
        return (lh》rh ? lh:rh) + 1;
    }else{
        return 0;
    }
}
int main()
{
    
    return 0;
}

想问一下线索二叉树的遍历的一个问题,书上这个代码,一直递归到最左边的子树后

您好,当您仅到达二进制树中的最左节点时,pre =0。由于pre是一个全局变量,一次到达最左节点后,pre = p,然后pre是pre是pre front。转到p-“ lchild = pre。