线索二叉树

本文介绍了如何使用InThreading_Thrt函数实现二叉树的并行构建,通过递归和线程同步,展示了如何在C++中创建和遍历具有左右标记的二叉树。重点在于理解Threading_Thrt函数的工作原理及其在处理二叉树时的角色。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<vector>
#include<string.h>
#include<map>
#include<cmath>
#include<sstream>
#include<stack>
#include<queue>
#define ll long long
#define maxn 200005
#define ull unsigned long long
using namespace std;
typedef char ElemType;
typedef struct BiThrNode
{
       ElemType data;
       BiThrNode *LChild, *RChild;
       int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrTree pre;
void InThreading_Thrt(BiThrTree T)
{
    if(T)//root
    {
        InThreading_Thrt(T->LChild);
        if(!T->LChild)
        {
            T->LTag=1;
            T->LChild=pre;
        }
        if(!pre->RChild)
        {
            pre->RTag=1;
            pre->RChild=T;
        }
        pre=T;
        InThreading_Thrt(T->RChild);
    }
}
int InThreading(BiThrTree *thrt,BiThrTree T)
{
    //ro
    if(!(*thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(-2);
    (*thrt)->LTag=0;
    (*thrt)->RTag=1;
    (*thrt)->RChild=(*thrt);
    if(!T) (*thrt)->LChild=(*thrt);
    else {
        (*thrt)->LChild=T;
         pre=(*thrt);//ro
        InThreading_Thrt(T);
        pre->RChild=(*thrt);
        pre->RTag=1;
        (*thrt)->RChild=pre;
    }
    return 1;
}
int InThreading_Traverse(BiThrTree T)
{
    BiThrNode *p=T->LChild;
    while(p!=T)
    {
        while(p->LTag==0) p=p->LChild;
        cout<<p->data<<"("<<p->LTag<<","<<p->RTag<<")"<<" ";
        while(p->RTag==1&&p->RChild!=T)
        {
            p=p->RChild;
            cout<<p->data<<"("<<p->LTag<<","<<p->RTag<<")"<<" ";
        }
        p=p->RChild;
    }
    return 1;
}
//建立二叉树的存储结构
int CreateBinaryTree(BiThrTree *B,ElemType empty){
        ElemType ch;
        cin>>ch;
        if (ch ==empty ) *B = NULL;//root
        else
        {
            if(!((*B)=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(-2);
            (*B)->data=ch;
            (*B)->LTag=0;
            (*B)->RTag=0;
            CreateBinaryTree(&(*B)->LChild,empty);
            CreateBinaryTree(&(*B)->RChild,empty);
        }
        return 1;
}

int main()
{
    char str;
    cin>>str;
    BiThrTree root,ro;//root ro是指针
    CreateBinaryTree(&root,str);
    InThreading(&ro,root);
    InThreading_Traverse(ro);
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

When_3*four

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

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

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

打赏作者

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

抵扣说明:

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

余额充值