游客 Signup | Login
中文 | En

1356 - 算法6-5~6-7:线索二叉树

通过次数

0

提交次数

0

Time Limit : 1 秒 Memory Limit : 32 MB
在遍历二叉树的过程中,是按照一定的规则将二叉树中的结点排列成一个线性序列,从而得到二叉树中结点的先序序列或中序序列或后序序列。但是,当以二叉链表作为存储结构时,只能找到结点的左右孩子信息,而不能直接得到结点在任意一个序列中的前驱和后继的信息,而这种信息只有在遍历的动态过程中才能够得到。
为了保存这种信息,就需要使用线索链表。其中指向结点的前驱和后继的指针,叫做线索。添加上线索的二叉树称之为线索二叉树。其结点定义如下:

<span style="font-family:宋体;">下面给出按照中序遍历将二叉树中序线索化的算法:</span>

<img src="http://tk.hustoj.com:80/upload/pimg1758_2.png" width="501" height="343" alt="" />

<span style="font-family:宋体;">在已经线索化的二叉线索树中,进行中序遍历的算法如下所示:</span>

<img src="http://tk.hustoj.com:80/upload/pimg1758_3.png" width="541" height="288" alt="" />

<span style="font-family:宋体;">本题中,将会给出一个按照先序遍历得出的字符串,空格代表空的子节点,大写字母代表节点内容。请通过这个字符串建立二叉树,并按照题目描述中算法,中序遍历二叉树并中序线索化二叉树,之后中序遍历输出二叉线索树。</span>

<span></span>

<span></span>

<span></span>

Input

输入只有一行,包含一个字符串S,用来建立二叉树。保证S为合法的二叉树先序遍历字符串,节点内容只有大写字母,且S的长度不超过100。

Output

共一行,包含一串字符,表示按中序遍历二叉线索树得出的节点内容,每个字母后输出一个空格。请注意行尾输出换行。

Examples

Input Format

ABC  DE G  F   

Output Format

C B E G D F A 

Solution C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct BiThrNode{
	char data;
	struct BiThrNode *lchild,*rchild;
	int ltag,rtag;//0为左右子树,1为线索
}BiThrNode,*BiThrTree;
BiThrTree pre;
char preoder[101];
int index=0;
void create(BiThrTree *t){
	if (preoder[index]==' ')
	{
		index++;
		*t=NULL;
		return ;
	}else{
		*t=(BiThrTree)malloc(sizeof(BiThrNode));
		(*t)->data=preoder[index];
		index++;
		create(&(*t)->lchild);
		create(&(*t)->rchild);
	}
}
void inthreading(BiThrTree t){
	if (t)
	{
		inthreading(t->lchild);
		if (t->lchild)
		{
			t->ltag=0;
		}else{
			t->ltag=1;
			t->lchild=pre;
		}
		if (pre->rchild)
		{
			pre->rtag=0;
		}else{
			pre->rtag=1;
			pre->rchild=t;
		}
		pre=t;
		inthreading(t->rchild);
	}
}
void inorderthreading(BiThrTree t,BiThrTree *thrt){
	*thrt=(BiThrTree)malloc(sizeof(BiThrNode));
	(*thrt)->rtag=1;
	(*thrt)->rchild=*thrt;//右线索先回指,最后再把它指向最后一个节点
	(*thrt)->ltag=0;//ltag和rtag是确定的
	if (!t)
	{
		(*thrt)->lchild=(*thrt);
	}else{
		(*thrt)->lchild=t;
		pre=*thrt;//记录下头结点
		inthreading(t);//中序遍历进行中序线索化
		//最后一个节点线索化
		pre->rtag=1;
		pre->rchild=(*thrt);
		(*thrt)->rtag=1;
		(*thrt)->rchild=pre;//头结点线索化
	}
}
void inoder_thr(BiThrTree thrt){
	BiThrTree p=thrt->lchild;
	while (p!=thrt)
	{
		while (p->ltag==0)
		{
			p=p->lchild;
		}
		printf("%c ",p->data);
		while (p->rtag==1&&p->rchild!=thrt)
		{
			p=p->rchild;
			printf("%c ",p->data);
		}
		p=p->rchild;
	}
	printf("\n");
}
void print(BiThrTree t){
	if (t)
	{
		print(t->lchild);
		printf("%c ",t->data);
		print(t->rchild);
	}
}
void clean_thr(BiThrTree thrt){
	if (thrt)
	{
		if (thrt->ltag==0)
		{
			thrt->ltag=1;//否则会重复
			clean_thr(thrt->lchild);
		}
		if (thrt->rtag==0)
		{
			thrt->rtag=1;//
			clean_thr(thrt->rchild);
		}
		free(thrt);
	}
}
void clean(BiThrTree t){
	if (t)
	{
		clean(t->lchild);
		clean(t->rchild);
		free(t);
	}
}
int main(){
	BiThrTree t,thrt;
//	freopen("1.txt","r",stdin);
	while (scanf("%[^\n]",preoder)!=EOF)
	{
		index=0;
		create(&t);
	//	print(t);
	//	printf("\n");
		inorderthreading(t,&thrt);
	//	print(t);
	//	printf("\n");
		inoder_thr(thrt);
		clean_thr(thrt);
		getchar();
	}
//	fclose(stdin);
	return 0;
}

Solution C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */
#define STACKINCREMENT 2 /* 存储空间分配增量 */
typedef int Status; /* 函数结果状态代码,如OK等 */
typedef char TElemType;
TElemType Nil=' '; /* 字符型以空格符为空 */
typedef enum{Link,Thread}PointerTag; /* Link(0):指针,Thread(1):线索 */
typedef struct BiThrNode {
	TElemType data;
	struct BiThrNode *lchild,*rchild; /* 左右孩子指针 */
	PointerTag LTag,RTag; /* 左右标志 */
}BiThrNode,*BiThrTree;
Status visit(TElemType c) {
	printf("%c ",c);
	return OK;
}
/* bo6-3.c 二叉树的二叉线索存储(存储结构由c6-3.h定义)的基本操作 */
Status CreateBiThrTree(BiThrTree *T) {
/* 按先序输入二叉线索树中结点的值,构造二叉线索树T */
/* 空格表示空结点 */
	TElemType h;
	scanf("%c",&h);
	if(h==Nil)
		*T=NULL;
	else {
		*T=(BiThrTree)malloc(sizeof(BiThrNode));
		if(!*T) exit(OVERFLOW);
		(*T)->data=h; /* 生成根结点(先序) */
		CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树 */
		if((*T)->lchild) /* 有左孩子 */
			(*T)->LTag=Link;
		CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树 */
		if((*T)->rchild) /* 有右孩子 */
			(*T)->RTag=Link;
	}
	return OK;
}

BiThrTree pre; /* 全局变量,始终指向刚刚访问过的结点 */
void InThreading(BiThrTree p) { /* 中序遍历进行中序线索化。算法6.7 */
	if(p) {
		InThreading(p->lchild); /* 递归左子树线索化 */
		if(!p->lchild) { /* 没有左孩子 */
			p->LTag=Thread; /* 前驱线索 */
			p->lchild=pre; /* 左孩子指针指向前驱 */
		}
		if(!pre->rchild) { /* 前驱没有右孩子 */
			pre->RTag=Thread; /* 后继线索 */
			pre->rchild=p; /* 前驱右孩子指针指向后继(当前结点p) */
		}
		pre=p; /* 保持pre指向p的前驱 */
		InThreading(p->rchild); /* 递归右子树线索化 */
	}
}
Status InOrderThreading(BiThrTree *Thrt,BiThrTree T) {
/* 中序遍历二叉树T,并将其中序线索化,Thrt指向头结点。算法6.6 */
	*Thrt=(BiThrTree)malloc(sizeof(BiThrNode));
	if(!*Thrt) exit(OVERFLOW);
	(*Thrt)->LTag=Link; /* 建头结点 */
	(*Thrt)->RTag=Thread;
	(*Thrt)->rchild=*Thrt; /* 右指针回指 */
	if(!T) /* 若二叉树空,则左指针回指 */
		(*Thrt)->lchild=*Thrt;
	else {
		(*Thrt)->lchild=T;
		pre=*Thrt;
		InThreading(T); /* 中序遍历进行中序线索化 */
		pre->rchild=*Thrt;
		pre->RTag=Thread; /* 最后一个结点线索化 */
		(*Thrt)->rchild=pre;
	}
	return OK;
}
Status InOrderTraverse_Thr(BiThrTree T,Status(*Visit)(TElemType)) {
/* 中序遍历二叉线索树T(头结点)的非递归算法。算法6.5 */
	BiThrTree p;
	p=T->lchild; /* p指向根结点 */
	while(p!=T) { /* 空树或遍历结束时,p==T */
		while(p->LTag==Link) p=p->lchild;
		if(!Visit(p->data)) /* 访问其左子树为空的结点 */
			return ERROR;
		while(p->RTag==Thread&&p->rchild!=T) {
			p=p->rchild;
			Visit(p->data); /* 访问后继结点 */
		}
		p=p->rchild;
	}
	return OK;
}
int main() {
	BiThrTree H,T;
	CreateBiThrTree(&T); /* 按先序产生二叉树 */
	InOrderThreading(&H,T); /* 中序遍历,并中序线索化二叉树 */
	InOrderTraverse_Thr(H,visit); /* 中序遍历(输出)二叉线索树 */
	printf("\n");
	return 0;
}