线索二叉树的生成

n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域。利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前趋和后继结点的指针(这种附加的指针称为"线索")。

这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种。
线索链表解决了无法直接找到该结点在某种遍历序列中的前趋和后继结点的问题,出现了二叉链表找左、右孩子困难的问题。

二叉树的遍历本质上是将一个复杂的非线性结构转换为线性结构,使每个结点都有了唯一前驱和后继(第一个结点无前驱,最后一个结点无后继)。对于二叉树的一个结点,查找其左右子女是方便的,其前驱后继只有在遍历中得到。为了容易找到前驱和后继,有两种方法。一是在结点结构中增加向前和向后的指针fwd和bkd,这种方法增加了存储开销,不可取;二是利用二叉树的空链指针。现将二叉树的结点结构重新定义如下:
lchild
ltag
data
rtag
rchild
其中:ltag=0 时lchild指向左子女;
ltag=1 时lchild指向前驱;
rtag=0 时rchild指向右子女;
rtag=1 时rchild指向后继;




现在利用中序遍历生成线索二叉树



上图解释了中序遍历是的压栈出栈顺序,这样我们就可以了解程序的执行流程,那么这个程序就好设计了。


#include "stdafx.h"
#include<iostream>
using namespace std;


typedef unsigned char BYTE;
struct BiNOde
{
	int ele;
	BiNOde* lnode;
	BiNOde* rnode;
	BYTE ltag;
	BYTE rtag;
};


BiNOde*pre = NULL;
BiNOde*rnode = NULL;

BiNOde*create_tree()
{
	BiNOde * root = new BiNOde;
	BiNOde*node1 = new BiNOde;
	BiNOde*node2 = new BiNOde;
	BiNOde*node3 = new BiNOde;
	BiNOde*node4 = new BiNOde;
	BiNOde*node5 = new BiNOde;
	BiNOde*node6 = new BiNOde;
	BiNOde*node7 = new BiNOde;
	BiNOde*node8 = new BiNOde;
	BiNOde*node9 = new BiNOde;
	BiNOde*node10 = new BiNOde;
	BiNOde*node11 = new BiNOde;
	BiNOde*node12 = new BiNOde;
	root->ele = 45;
	node1->ele = 38;
	node2->ele = 55;
	node3->ele = 33;
	node4->ele = 41;
	node5->ele = 19;
	node6->ele = 16;
	node7->ele = 52;
	node8->ele = 58;
	node9->ele = 50;
	node10->ele = 44;
	node11->ele = 35;
	node12->ele = 42;
	root->ltag = 0;
	root->rtag = 0;
	node1->ltag = 0;
	node2->ltag = 0;
	node3->ltag = 0;
	node4->ltag = 0;
	node5->ltag = 0;
	node6->ltag = 0;
	node7->ltag = 0;
	node8->ltag = 0;
	node9->ltag = 0;
	node10->ltag = 0;
	node11->ltag = 0;
	node12->ltag = 0;
	node1->rtag = 0;
	node2->rtag = 0;
	node3->rtag = 0;
	node4->rtag = 0;
	node5->rtag = 0;
	node6->rtag = 0;
	node7->rtag = 0;
	node8->rtag = 0;
	node9->rtag = 0;
	node10->rtag = 0;
	node11->rtag = 0;
	node12->rtag = 0;
	root->lnode = node1;
	root->rnode = node2;
	node1->lnode = node3;
	node1->rnode = node4;
	node2->lnode = node7;
	node2->rnode = node8;
	node3->lnode = node5;
	node3->rnode = node11;
	node4->rnode = node10;
	node4->lnode = NULL;
	node5->lnode = node6;
	node5->rnode = NULL;
	node6->lnode = NULL;
	node6->rnode = NULL;
	node7->lnode = node9;
	node7->rnode = NULL;
	node8->lnode = NULL;
	node8->rnode = NULL;
	node9->lnode = NULL;
	node9->rnode = NULL;
	node10->lnode = node12;
	node10->rnode = NULL;
	node11->lnode = NULL;
	node11->rnode = NULL;
	//BiNOde*node12 = new BiNOde;      
	//node12->ele = 12;      
	node12->lnode = NULL;
	node12->rnode = NULL;
	//node6->lnode = node11;      
	return root;
}

void Threading(BiNOde*node)
{
	if (node == NULL)
		return;
	if (node->lnode != NULL)
	{
		//cout << node->lnode->ele<<endl;
		Threading(node->lnode);
	}
	else
		if (pre!=NULL)
	{
		node->lnode = pre;
		node->ltag = 1;
	}
	cout << node->ele << endl;
	pre = node;
	if (rnode != NULL&&rnode->rtag==0)
	{
		rnode->rnode = node;
		rnode->rtag = 1;
	}
	
	if (node->rnode != NULL)
	{
		//cout << node->rnode->ele << endl;
		Threading(node->rnode);
	    
	}
	if (node->rnode == NULL)
	{
		rnode = node;
	}


}

int _tmain(int argc, _TCHAR* argv[])
{
	BiNOde*root = create_tree();
	Threading(root);


	system("pause");
	return 0;
}


版权声明:

posted on 2015-08-03 14:28  moffis  阅读(428)  评论(0编辑  收藏  举报

导航