线索二叉树

文章目录

一、线索二叉树的概念

二、线索二叉树结构的实现

三、代码实现

一、线索二叉树的概念

  • 线索:指向前驱和后继的指针
  • 线索链表:加上线索的二叉链表
  • 线索二叉树:加上线索的二叉树(相当于一个双向链表)
  • 线索化:对二叉树以某种次序遍历使其变为线索二叉树的过程
    在这里插入图片描述
我们在每个结点新增设两个标志域:Ltag和rtag分别用来指向前驱和后继
  • Ltag和rtag只是存放0或1数字的布尔型变量
  • Ltag为0时指向该节点的左孩子,为1时指向该结点的前驱
  • Ltag为0时指向该节点的右孩子,为1时指向该结点的后继

二、线索二叉树结构的实现:

//Link = 0,表示左右孩子指针
//Thead = 1,表示前驱或后继
typedef enum { Link, Thread }  Pointertag;

typedef struct BiThrTree
{
	char data;
	struct BiThrTree* lchild, * rchild;
	Pointertag ltag;//左右标志
	Pointertag rtag;
}BiThrNode,*BiThrTree;
中序遍历进行中序线索化
BiThrTree pre;//全局变量,始终指向刚刚访问过的结点
//中序遍历进行中序线索化
void InThreading(BiThrTree p)
{
	if(p)
	{
	InThreading(p->lchild);//递归左子树线索化
		if(!p->lchild)
		{
			p->ltag=Thread;
			p->lchild=pre;
		}
		if(!pre->rchild)
		{
			pre->rtag=Thread;
			pre->rchild=p;
		} 
	pre=p;
	InThreading(p->rchild);
	}

}
和双向链表结构一样,在二叉树线索链表上添加一个头结点

在这里插入图片描述

void Inorder(BiThrTree T)
{
	//让p指向根结点
	BiThrTree p ;
	p=T->lchild;	
	while (p!=T)//空树或遍历结束时,p==T 
	{
		
		while (p->ltag == Link)//当ltag==0时,循环到中序序列第一个结点 
		{
			p = p->lchild;
		}
		printf("%c", p->data);//显示结点数据,可以更改为其他对结点的操作 
		while (p->rtag == Thread && p->rchild!=T)
		{
			p = p->rchild;
			printf("%c", p->data);
		}
		/*
		* 当右孩子不是线索时(没有后继),
		* 说明该结点右孩子存在,
		* 所以接着右孩子的遍历
		*/
		p = p->rchild;//p进至其右子树根
	}
}
如果所用的二叉树需经常遍历或查找结点时需要某种遍历序列中的前驱和后继,那么采用线索二叉链表!

三、代码实现

typedef struct AVLNode *Position;
typedef Position AVLTree; /* AVL树类型 */
struct AVLNode{
    ElementType Data; /* 结点数据 */
    AVLTree Left;     /* 指向左子树 */
    AVLTree Right;    /* 指向右子树 */
    int Height;       /* 树高 */
};
 
int Max ( int a, int b )
{
    return a > b ? a : b;
}
 
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B */
  /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */     
 
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
    B->Height = Max( GetHeight(B->Left), A->Height ) + 1;
  
    return B;
}
 
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
  /* 将A、B与C做两次单旋,返回新的根结点C */
     
    /* 将B与C做右单旋,C被返回 */
    A->Left = SingleRightRotation(A->Left);
    /* 将A与C做左单旋,C被返回 */
    return SingleLeftRotation(A);
}
 
/*************************************/
/* 对称的右单旋与右-左双旋请自己实现 */
/*************************************/
 
AVLTree Insert( AVLTree T, ElementType X )
{ /* 将X插入AVL树T中,并且返回调整后的AVL树 */
    if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right = NULL;
    } /* if (插入空树) 结束 */
 
    else if ( X < T->Data ) {
        /* 插入T的左子树 */
        T->Left = Insert( T->Left, X);
        /* 如果需要左旋 */
        if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
            if ( X < T->Left->Data ) 
               T = SingleLeftRotation(T);      /* 左单旋 */
            else 
               T = DoubleLeftRightRotation(T); /* 左-右双旋 */
    } /* else if (插入左子树) 结束 */
     
    else if ( X > T->Data ) {
        /* 插入T的右子树 */
        T->Right = Insert( T->Right, X );
        /* 如果需要右旋 */
        if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
            if ( X > T->Right->Data ) 
               T = SingleRightRotation(T);     /* 右单旋 */
            else 
               T = DoubleRightLeftRotation(T); /* 右-左双旋 */
    } /* else if (插入右子树) 结束 */
 
    /* else X == T->Data,无须插入 */
 
    /* 别忘了更新树高 */
    T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
     
    return T;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值