线索二叉树基础

#include<bits/stdc++.h>
using namespace std;

//定义
typedef struct ThreadNode
{
    char data;
    ThreadNode *lchild,*rchild;
    int ltag,rtag;//0代表孩子,1代表后继
}ThreadNode,*ThreadTree;

//建一棵二叉树
ThreadTree Create()
{
    ThreadTree tt;
    char ch;
    cin>>ch;
    if(ch=='#')
    {
        tt=NULL;
    }
    else
    {
        tt=(ThreadNode*)malloc(sizeof(ThreadNode));
        tt->data=ch;
        tt->lchild=Create();
        tt->rchild=Create();
        tt->ltag=0;
        tt->rtag=0;
    }
}

//二叉树线索化
void InThread(ThreadTree &p,ThreadTree &pre)//pre是刚刚访问过的节点
{
    if(p!=NULL)
    {
        InThread(p->lchild,pre);
        if(p->lchild==NULL)
        {
            p->lchild=pre;
            p->ltag=1;
        }
        if(pre!=NULL&&pre->rchild==NULL)
        {
            pre->rchild=p;
            pre->rtag=1;
        }
        pre=p;
        InThread(p->rchild,pre);
    }
}
void CreateInThread(ThreadTree tt)
{
    ThreadTree pre=NULL;
    if(tt!=NULL)
    {
        InThread(tt,pre);
        pre->rchild=NULL;
        pre->rtag=1;
    }
}

//中序线索二叉树的遍历
ThreadNode *FirstNode(ThreadNode *p)
{
    while(p->ltag==0)
    {
        p=p->lchild;
    }
    return p;
}
ThreadNode *NextNode(ThreadNode *p)
{
    if(p->rtag==0)
        return FirstNode(p->rchild);
    else
        return p->rchild;
}
void InOrder(ThreadNode *tt)
{
    for(ThreadNode *p=FirstNode(tt);p!=NULL;p=NextNode(p))
    {
        cout<<p->data<<" ";
    }
    cout<<endl;
}
int main()
{
    ThreadTree tt=Create();
    CreateInThread(tt);
    InOrder(tt);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遍历二叉树的方法有三种:先序遍历、中序遍历和后序遍历。其中,先序遍历是指先遍历根节点,再遍历左子树和右子树;中序遍历是指先遍历左子树,再遍历根节点和右子树;后序遍历是指先遍历左子树和右子树,再遍历根节点。 线索叉树是在二叉树基础上,通过添加线索(即前驱和后继指针)来实现遍历的优化。线索叉树的遍历方法有两种:中序遍历和后序遍历。其中,中序遍历是指按照节点的中序遍历顺序遍历线索叉树;后序遍历是指按照节点的后序遍历顺序遍历线索叉树。 举例说明: ```java //先序遍历二叉树 public void preOrder(TreeNode root) { if (root != null) { System.out.print(root.val + " "); preOrder(root.left); preOrder(root.right); } } //中序遍历二叉树 public void inOrder(TreeNode root) { if (root != null) { inOrder(root.left); System.out.print(root.val + " "); inOrder(root.right); } } //后序遍历二叉树 public void postOrder(TreeNode root) { if (root != null) { postOrder(root.left); postOrder(root.right); System.out.print(root.val + " "); } } //中序遍历线索叉树 public void inOrderThreaded(TreeNode root) { TreeNode cur = root; while (cur != null) { while (cur.left != null && !cur.left.isThreaded) { cur = cur.left; } System.out.print(cur.val + " "); if (cur.right != null && cur.isThreaded) { cur = cur.right; } else { cur = cur.right; while (cur != null && !cur.isThreaded) { cur = cur.left; } } } } //后序遍历线索叉树 public void postOrderThreaded(TreeNode root) { TreeNode cur = root; while (cur != null) { while (cur.left != null && !cur.left.isThreaded) { cur = cur.left; } while (cur.right != null && cur.isThreaded) { System.out.print(cur.val + " "); cur = cur.right; } System.out.print(cur.val + " "); cur = cur.right; } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值