数据结构——二叉树的线索化以及线索化输出

本文详细介绍了二叉树的线索化过程,通过中序遍历将空指针转换为指向前驱或后继节点的线索,以便于快速遍历。程序实现了二叉树的线索化、插入、清除、中序遍历和输出等功能,并通过示例展示了具体操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的线索化过程,其遍历过程和中序遍历一致,只不过在遍历过程中顺手将节点的左空指针赋值为前一个节点地址,右空指针顺手赋值为下一个节点地址,所以需要一个pre变量来保存当前遍历的节点,方便对后一时刻的root进行操作。
对于如下二叉树:
在这里插入图片描述
线索化程序如下:

void TreeThreaded(Node* root){
    static Node *pre=NULL;//静态局部变量只在第一次使用时初始化,之后只进行调用
    if(root!=NULL){
        TreeThreaded(root->lchild);//递归一直遍历到根节点的最左子孩子
        if(root->lchild==NULL){
            root->lchild=pre;
            root->ltag=THREAD;
        }
        if(pre !=NULL && pre->rchild==NULL){
            pre->rchild = root;
            pre->rtag = THREAD;
        }
        pre=root;//pre节点总是赋值为前一个root节点,即root节点在下一个时刻变成pre
        TreeThreaded(root->rchild);
    }
    return;
}

程序的运行过程为:
在这里插入图片描述

程序运行:TreeThreaded(root->lchild),一直遍历到最左子节点H

在这里插入图片描述

此时root=H;
运行完:TreeThreaded(H->lchild),执行if(root->lchild==NULL)条件语句,
不满足if(pre !=NULL && pre->rchild==NULL),跳过,
执行pre=H;
执行TreeThreaded(H->rchild),因为H->rchild为空,什么也不干,程序返回
程序返回,从栈中弹出H
程序运行结果如下:

在这里插入图片描述

此时root=D,pre=H;
不满足if(root->lchild==NULL);
满足if(pre !=NULL && pre->rchild==NULL);
将H->left赋值为D;
执行pre=D。
程序运行过程如下:

在这里插入图片描述
在这里插入图片描述

执行 TreeThreaded(D->rchild),TreeThreaded(I);
执行TreeThreaded(I->left),因为I->left为空,什么也不干;
执行pre=root=I;
执行TreeThreaded(I->rchild),什么也不干;
从堆栈中弹出I;
从堆栈中弹出D;

在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_OP 8
#define NORMAL 0
#define THREAD 1

typedef struct Node{
    int data;
    int ltag,rtag;
    struct Node *lchild,*rchild;
}Node;
static Node *pre=NULL;
Node* getNewNode(int value){
    Node* node=(Node*)malloc(sizeof(Node));
    node->data=value;
    node->lchild=node->rchild=NULL;
    node->rtag=node->ltag=NORMAL;
    return node;
}
void TreeThreaded(Node* root){
    if(root==NULL)return;
    //static Node *pre=NULL;//静态局部变量只在第一次使用时初始化,之后只进行调用
    TreeThreaded(root->lchild);//递归一直遍历到根节点的最左子孩子
    if(root->lchild==NULL){
        root->lchild=pre;
        root->ltag=THREAD;
    }
    if(pre != NULL && pre->rchild == NULL){
        pre->rchild = root;
        pre->rtag = THREAD;
    }
    pre=root;
    TreeThreaded(root->rchild);
    return;
}
void clear(Node* root){
    if(root==NULL)return;
    if(root->ltag==NORMAL)clear(root->lchild);
    if(root->rtag==NORMAL)clear(root->rchild);
    free(root);
}
Node *leftMost(Node *node){
    while(node != NULL && node->ltag == NORMAL && node->lchild != NULL)node = node->lchild;
    return node;
}
void output(Node* root){
    Node* node=leftMost(root);
    while(node !=NULL){
        printf("%d ",node->data);
        if(node->rtag == THREAD){
            node = node->rchild;
        }else{
            node = leftMost(node->rchild);
        }
    }
    printf("\n");
    return;
}
Node* insert(Node *root,int value){//排序二叉树的插入操作
    if(root==NULL)return getNewNode(value);
    if(root->data==value)return root;
    if(root->data<value)root->rchild=insert(root->rchild,value);
    if(root->data>value)root->lchild=insert(root->lchild,value);
    return root;
}
void inOrder(Node* root){
    if(root==NULL)return;
    if(root->ltag==NORMAL)inOrder(root->lchild);
    printf("%d ",root->data);
    if(root->rtag==NORMAL)inOrder(root->rchild);
    return;
}
int main() {
    srand(time(0));
    Node* root=NULL;
    for(int i=0;i<MAX_OP;i++){
        int value=rand()%50;
        printf("%d\n",value);
        root=insert(root,value);
    }
    TreeThreaded(root);
    inOrder(root);
    printf("\n");
    output(root);
    printf("\n");
    clear(root);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karwen2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值