二叉树之线索二叉树基本操作(C++实现)

#include<iostream>

using namespace std;

typedef char TElemType;               //TElemType 相当于 char

typedef struct BiThrNode
{
    TElemType data;
    struct BiThrNode *lchild,*rchild; //左右孩子指针
    int LTag,RTag;   //左右标志
} BiThrNode,*BiThrTree; 
  

	BiThrTree pre;
	BiThrTree p;
	BiThrTree Thrt;

void InThreading(BiThrTree p)  //以结点p为根的子树中序线索化 
{
	if(p)
	{
		InThreading(p->lchild);
		if(!p->lchild)
		{
			p->LTag = 1;
			p->lchild=pre;	
		}
		else p->LTag = 0;
		if(!pre->rchild)
		{
			pre->RTag=1;
			pre->rchild=p;
		}
		else p->RTag = 0;
		pre = p;
		InThreading(p->rchild);
	}
	
}


void InOrderThreading(BiThrTree &Thrt,BiThrTree T)  //带头结点的二叉树中序线索化 
{
	
	Thrt = new BiThrNode;
	Thrt-> LTag = 0;
	Thrt->RTag = 1;
	Thrt->rchild = Thrt;
	if(!T) Thrt->lchild = Thrt;
	else
	{
		Thrt->lchild = T;
		pre=Thrt;
		InThreading(T);
		pre->rchild=Thrt;
		pre->RTag=1;
		Thrt->rchild=pre;
	}
} 


void CreateTree(BiThrTree &T)//先序创建二叉树
{
    TElemType ch;
    scanf("%c", &ch);
    if(ch == '#')
    {
       T= NULL;
    }
    else
    {
        T = new BiThrNode;
        T->LTag=T->RTag=0;
        T->data = ch;
        CreateTree(T->lchild);
        CreateTree(T->rchild);
    }
}

void InOrderTraverse_Thr(BiThrTree T)// 遍历中序线索二叉树 
{
	p = T->lchild;
	while(p!=T)
	{
		while(p->LTag==0) p=p->lchild;
		cout<<p->data;
		while(p->RTag==1 && p->rchild!=T)
		{
			p = p->rchild;
			cout << p->data;
		}
		p = p->rchild;
	}
}




BiThrTree InPreNode(BiThrTree p)// 在中序线索二叉树上 p 的前驱结点
{
 
 	BiThrTree pre;  //前驱节点 
 	pre=p->lchild;
 	
	if(p->LTag!=1)
  	{
		while(pre->RTag==0)
   		{
    		pre=pre->rchild;
		}
	}	
 	return pre;
}

BiThrTree InPostNode(BiThrTree p)// 在中序线索二叉树上 p 的后继结点  
{
 
 	BiThrTree post;  //后继节点 
 	post=p->rchild;
 
 	if(p->RTag!=1)
   	{
		while(post->LTag==0)
    	{
			post=post->lchild;
		}	
 	}
	return post;
}

BiThrTree IprePostNode(BiThrTree T,BiThrTree p)// 在中序线索二叉树上找 p 的先序的后继结点
{
 
 	BiThrTree post; //先序的后继结点 
 	
	if(p->LTag==0)
   		post=p->lchild;
 	else
	{
   		post=p;
   		while(post->RTag==1&&post->rchild!=T)
    		 post=post->rchild;
   		post=post->rchild;
 	}
	return post;
}

BiThrTree IpostPretNode(BiThrTree T,BiThrTree p) // 在中序线索二叉树上找 p 的后序的前驱结点
{ 
 	BiThrTree pre; //后序的前驱结点 
 	
	if(p->RTag==0)
   		pre=p->rchild;
 	else
	{
   		pre=p;
   		while(pre->LTag==1 && pre->lchild!=T)
     		pre=pre->lchild;
   		pre=pre->lchild;
 	}
 	return pre;
}
BiThrTree Search(BiThrTree T,TElemType x)// 在中序线索二叉树上查找结点为 x 的结点
{ 
	BiThrTree p;
	p=T->lchild;
 
	while(p->LTag==0 && p!=T)
   	{
		p=p->lchild;
	}	
 	while(p!=T && p->data!=x)
   	{
   		p=InPostNode(p);
	}	
 	if(p==T)
	{
   		cout<<"查无此数据!"<<endl;
   		return NULL;
 	}
 	else
   		return p;
}



int main()
{
	BiThrTree T,aa;
	cout << "建立线索二叉树:" <<endl;  
	cout <<  "请按照先序顺序输入线索二叉树的每个结点:"; 
	CreateTree(T);cout << endl;
	cout << "将二叉树中序线索化..." << endl << endl; 
	InOrderThreading(Thrt,T);
	cout << "遍历中序线索二叉树:" ;
	InOrderTraverse_Thr(T); cout << endl<< endl;
	
	cout << "请输入目标结点:";
	char x;
	cin >> x;
	aa =  Search(T,x);
	cout << endl;
	cout << "中序线索二叉树上目标结点的前驱结点:"<<InPreNode(aa)->data << endl; 
	cout << "中序线索二叉树上目标结点的后继结点:" <<InPostNode(aa)->data << endl; 
	cout << "中序线索二叉树上目标结点的先序的后继结点:" <<IprePostNode(T,aa)->data << endl; 
	cout << "中序线索二叉树上目标结点的后序的前驱结点:" <<IpostPretNode(T,aa)->data  << endl; 
	cout << endl << endl;
	
	cout << "请输入要查询的结点:" ; 
	char y;
	cin >> y;
	Search(T,y);
	
	return 0;
}
原创文章 50 获赞 139 访问量 2万+

猜你喜欢

转载自blog.csdn.net/diviner_s/article/details/106035131