线索二叉树小例

二叉树在c语言中的重要性不需再说,而线索二叉树则是二叉树中比较重要的一个应用。
一般的二叉树存储模式,空链的数目非常多,线索二叉树则利用这些空链使其指向节点的前驱和后继,改善二叉树的遍历效率。这些被重新利用的空链也就是“线索”了。

中序遍历线索二叉树:

#include <stdio.h>
#include <stdlib.h>
#define FALSE 0 /* point is not thread */
#define TRUE 1 /* point is thread */

typedef struct threaded_tree *threaded_pointer;
typedef struct threaded_tree{
    short int left_thread;
    threaded_pointer left_child;
    char data;
    threaded_pointer right_child;
    short int right_thread;
}threaded_tree;
threaded_pointer pre;

threaded_pointer createTp(threaded_pointer T){
/* create a binarytree */
    char ch;
    scanf("%c", &ch);
    if(ch == '#')
        return NULL;
    T = (threaded_pointer)malloc(sizeof(threaded_tree));
    T->data = ch;
    T->left_thread = FALSE;
    T->right_thread = FALSE;
    T->left_child = createTp(T->left_child);
    T->right_child = createTp(T->right_child);
    return T;
}
void inorderthread(threaded_pointer *p, threaded_pointer T){
/* add a root node to tree and thread tree */
    (*p) = (threaded_pointer)malloc(sizeof(threaded_tree));
    (*p)->left_thread = FALSE;
    (*p)->right_thread = FALSE;
    (*p)->right_child = (*p);

    if(!T){
        (*p)->left_child = p;
    }
    else{
        (*p)->left_child = T;
        pre = (*p);
        in_thread(T);
        pre->right_thread = TRUE;
        pre->right_child = (*p);
    }
}
void in_thread(threaded_pointer T){
/* inorder thread tree */
    if(T){
        in_thread(T->left_child);
        if(!T->left_child){
            T->left_thread = TRUE;
            T->left_child = pre;
        }
        if(!pre->right_child){
            pre->right_thread = TRUE;
            pre->right_child = T;
        }
        pre = T;
        in_thread(T->right_child);
    }
}
/* these two functions can be finded in the book : fundamentals of data structures in C */
threaded_pointer insucc(threaded_pointer tree){
/* find the inorder sucessor of tree in a threaded binary tree */
    threaded_pointer temp;
    temp = tree->right_child;
    if(!tree->right_thread)
        while(!temp->left_thread)
            temp = temp->left_child;
    return temp;
}
void tinorder(threaded_pointer tree){
/* traverse the threaded binary tree inorder */
    threaded_pointer temp = tree;
    for(;;){
        temp = insucc(temp);
        if(temp == tree)
            break;
        printf("%3c", temp->data);
    }
    printf("\n");
}
int main(){
    threaded_pointer p, threadTree;
    printf("input the binary tree (# == NULL):");
    threadTree = createTp(threadTree);
    inorderthread(&p, threadTree);
    printf("the threaded binary tree: ");
    tinorder(p);
    return 0;
}

程序结果:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值