c语言实现二叉树的线索化和线索二叉树的遍历源码

#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
typedef char datatype;
typedef struct btnode {
    datatype data;
    btnode* lchild;
    btnode* rchild;
    int LTag,RTag;
}btnode;
typedef struct binarytree {
    btnode* root;
}binarytree;
void nulltree(binarytree* bt) {
    bt->root = NULL;
}
btnode* newnode(btnode* node, datatype x) {
    node = (btnode*)malloc(sizeof(btnode));
    node->data = x;
    return node;
}
binarytree* maketree(binarytree* bt, datatype x, binarytree* lt, binarytree* rt) {
    bt->root = newnode(bt->root, x);
    bt->root->lchild = lt->root;
    bt->root->rchild = rt->root;
    return bt;
}
btnode*pre=NULL;
void inthread(btnode*p){
    if(p){
        inthread(p->lchild);
        if(p->lchild==NULL){
            p->LTag=1;
            p->lchild=pre;
        }
        if(pre!=NULL&&pre->rchild==NULL){
            pre->RTag=1;
            pre->rchild=p;
        }
        pre=p;
        inthread(p->rchild);
    }
}
void inthreadtree(binarytree*bt){
    inthread(bt->root);
}
void inordertree(binarytree*bt){
    btnode*p=bt->root;
    if(p){
       while(p->lchild){
           p=p->lchild;
       }
       while(p->RTag==1){
           printf("%c",p->data);
           p=p->rchild;
       }
       printf("%c",p->data);
       while(p->rchild){
           p=p->rchild;
       }
       while(p->lchild&&p->lchild!=bt->root){
           p=p->lchild;
       }
       while(p){
           printf("%c",p->data);
           p=p->rchild;
       }
        printf("\n");
    }
}
int main() {
    binarytree bt1, bt2, bt3, bt4, bt5;
    nulltree(&bt5);
    maketree(&bt4, 'D', &bt5, &bt5);
    maketree(&bt3, 'C', &bt4, &bt5);
    maketree(&bt2, 'B', &bt5, &bt5);
    maketree(&bt1, 'A', &bt2, &bt3);
    inthreadtree(&bt1);
    inordertree(&bt1);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值