线索二叉树

        在具体应用中,有时需要访问二叉树中的结点在某种遍历序列中的前驱和后继,为方便并效率地使用,我们可以利用原二叉树上没有使用的空指针域。若左指针为空,则指向前驱,若右指针为空则指向后继。线索表的结点结构类型为:

enum flag{Child,Thread};
template<class Type>
struct ThrNode
{
      Type data;
      ThrNode<Type> * lchild,* rchild;
      flag ltag,rtag;
};
         二叉树的遍历有四种,故线索化二叉树也有四种:前序线索链表,中序线索链表,后序线索链表,层序线索链表。下面的例子以中序线索链表为例。

建立二叉链表(带线索标志):

ThrNode<Type> * InThrBiTree<Type>::Create(ThrNode<Type> * cur)
{
	Type ch;
	if (ch == '#')
		cur = NULL;
	else
	{
		cur = new ThrNode;
		cur->data = ch;
		cur->ltag = 0;
		cur->rtag = 0;
		cur->lchild = Create(cur->lchild);
		cur->rchild = Create(cur->rchild);
	}
	return cur;
}

中序线索化链表:

template<class Type>
void InThrBiTree<Type>::ThrBiTree(ThrNode<Type> * cur,ThrNode<Type> * pre)
{
	if (cur == NULL)
		return;
	ThrBiTree(cur->lchild,pre);
	if (cur->lchild == NULL)
	{
		cur->ltag = 1;
		cur->lchild = pre;
	}
	if (cur->rchild == NULL)
		cur->rtag = 1;
	if (pre->rtag == 1)
		pre->rchild = cur;
	pre = cur;
	ThrBiTree(cur->rchild,pre);
}

中序线索链表查找后继算法:

template<class Type>
ThrNode<Type> * InThrBiTree<Type>::Next(ThrNode<Type> * cur)
{
	ThrNode<Type> * temp;
	if (cur->rtag == 1)
		temp = cur->rchild;
	else
	{
		temp = cur->rchild;
		while (temp->ltag == 0)//查找最左下结点
			temp = temp->lchild;
	}
	return temp;

遍历:

template<class Type>
void InThrBiTree<Type>::InOrder()
{
	ThrNode<Type> * temp;
	if (root == NULL)
		return ;
	temp = root;
	while (temp->ltag == 0)
		temp = temp->lchild;
	cout<<temp->data;
	while (temp->rchild != NULL)//存在后继则继续访问后继结点
	{
		temp = Next(temp);
		cout<<temp->data;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值