线索二叉树的完整代码实现

线索二叉树的完整代码,可直接运行
代码如下:

//线索二叉树
#include<stdlib.h>
#include<stdio.h>

typedef char TElemType;

typedef enum {Link,Thread} PointerTag;

typedef  struct BiThrNode
{
    TElemType data;
    struct BiThrNode *lchild,*rchild;
    PointerTag LTag;
    PointerTag RTag;
}BiThrNode, *BiThrTree;

BiThrTree pre=NULL;


//中序遍历线索化的递归函数
void InThreading(BiThrTree p)
{
    if(p)
    {
        InThreading(p->lchild);
        printf("%c",p->data);
        if(!p->lchild)
        {
            p->LTag=Thread;
            p->lchild=pre;
        }
        if(!p->rchild)
        {
            pre->RTag=Thread;
            pre->rchild=p;
        }
        pre=p;
        InThreading(p->rchild);
    }
}

//建立头指针,使其左指针指向根结点,右指针指向遍历的最后一个结点
void InOrder_Thr(BiThrTree *Thr,BiThrTree T)
{
    *Thr=(BiThrTree)malloc(sizeof(BiThrNode));
    (*Thr)->LTag=Link;
    (*Thr)->RTag=Thread;
    (*Thr)->rchild=*Thr;    

    if(!T)
        (*Thr)->lchild=*Thr;
    else
    {
        (*Thr)->lchild=T;
        pre=*Thr;
        InThreading(T);
        pre->RTag=Thread;
        pre->rchild=*Thr;
        (*Thr)->rchild=pre;
    }
}

//按前序输入二叉树
void CreateBiTree(BiThrTree *T)
{
    TElemType ch;
    scanf("%c",&ch);
    if(ch=='#')
        *T=NULL;
    else
    {
        *T=(BiThrTree)malloc(sizeof(BiThrNode));
        (*T)->data=ch;
        CreateBiTree(&(*T)->lchild);
        CreateBiTree(&(*T)->rchild);
    }
}

void main()
{
    BiThrTree T,Thr=NULL;
    printf("请输入你想建立的二叉树");
    CreateBiTree(&T);
    InOrder_Thr(&T,T);
}


以上是最基本的代码,是看《大话数据结构》时根据书上代码编写的,适合最最基础的人看,有问题可以评论,同是菜鸟,我们一起讨论嘛~

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是线索二叉树的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义线索二叉树结构体 typedef struct ThreadedBinaryTreeNode { int data; struct ThreadedBinaryTreeNode *left, *right; int ltag, rtag; // 0表示指向左右子树,1表示指向前驱或后继 } ThreadedBinaryTreeNode, *ThreadedBinaryTree; // 中序遍历线索化 void InThread(ThreadedBinaryTree p, ThreadedBinaryTree *pre) { if (p != NULL) { InThread(p->left, pre); if (p->left == NULL) { p->left = *pre; p->ltag = 1; } if (*pre != NULL && (*pre)->right == NULL) { (*pre)->right = p; (*pre)->rtag = 1; } *pre = p; InThread(p->right, pre); } } // 创建线索二叉树 void CreateInThread(ThreadedBinaryTree *root) { ThreadedBinaryTree pre = NULL; if (*root != NULL) { InThread(*root, &pre); pre->right = NULL; pre->rtag = 1; } } // 中序遍历线索二叉树 void InOrderTraverse(ThreadedBinaryTree root) { ThreadedBinaryTree p = root; while (p != NULL) { while (p->ltag == 0) { p = p->left; } printf("%d ", p->data); while (p->rtag == 1 && p->right != NULL) { p = p->right; printf("%d ", p->data); } p = p->right; } } // 测试 int main() { // 创建线索二叉树 ThreadedBinaryTree root = (ThreadedBinaryTree)malloc(sizeof(ThreadedBinaryTreeNode)); root->data = 1; root->ltag = root->rtag = 0; root->left = (ThreadedBinaryTree)malloc(sizeof(ThreadedBinaryTreeNode)); root->left->data = 2; root->left->ltag = root->left->rtag = 0; root->left->left = root->left->right = NULL; root->right = (ThreadedBinaryTree)malloc(sizeof(ThreadedBinaryTreeNode)); root->right->data = 3; root->right->ltag = root->right->rtag = 0; root->right->left = (ThreadedBinaryTree)malloc(sizeof(ThreadedBinaryTreeNode)); root->right->left->data = 4; root->right->left->ltag = root->right->left->rtag = 0; root->right->left->left = root->right->left->right = NULL; root->right->right = (ThreadedBinaryTree)malloc(sizeof(ThreadedBinaryTreeNode)); root->right->right->data = 5; root->right->right->ltag = root->right->right->rtag = 0; root->right->right->left = root->right->right->right = NULL; CreateInThread(&root); // 中序遍历线索二叉树 InOrderTraverse(root); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值