P169 例8-3 编写程序,首先建立如图8-10(a)所示的不带头节点的二叉树,然后中序线索化二叉树,最后用循环结构输出该中序线索化二叉树各结点的序列信息。

P169 例8-3 编写程序,首先建立如图8-10(a)所示的不带头节点的二叉树,然后中序线索化二叉树,最后用循环结构输出该中序线索化二叉树各结点的序列信息。

头文件:InThreadIterator.h

#include<stdio.h>
#include<stdlib.h>

typedef char DataType;

typedef struct Node
{
	DataType data;
	int leftThread;
	struct Node *leftChild;
	struct Node *rightChild;
	int rightThread;
}ThreadBiNode;

void InThread(ThreadBiNode *current,ThreadBiNode **pre)
{
	if(current!=NULL)
	{
		InThread(current->leftChild,pre);
		
		if(current->leftChild==NULL)
		{
			current->leftThread=1;
			current->leftChild=*pre;
		}
		else
			current->leftThread=0;
	
	
		if(current->rightChild!=NULL)
			current->rightThread=0;
		else
			current->rightThread=1;
	
		if((*pre)->rightChild==NULL)
		{
			(*pre)->rightThread=1;
			(*pre)->rightChild=current;
		}
		else
			current->rightThread=0;
	
		*pre=current;
		InThread(current->rightChild,pre);
	}
}

void CreatInThread(ThreadBiNode **root)
{
	ThreadBiNode *t=*root;
	ThreadBiNode *current,*pre=*root;
	
	*root=(ThreadBiNode*)malloc(sizeof(ThreadBiNode));
	if(t==NULL)
	{
		(*root)->leftThread=0;
		(*root)->rightThread=1;
		(*root)->leftChild=*root;
		(*root)->rightChild=*root;
	}
	else
	{
		current=t;
		(*root)->leftChild=t;
		(*root)->leftThread=0;
		InThread(current,&pre);
		pre->rightChild=*root;
		pre->rightThread=1;
		(*root)->rightChild=pre;
		(*root)->rightThread=1;
	}
}

typedef struct
{
	ThreadBiNode *root;
	ThreadBiNode *current;
	int nextComplete;
}ThreadBiTree;

void ThreadInitiate(ThreadBiTree *tree,ThreadBiNode *root)
{
	tree->root=root;
	tree->current=root;
	if(root==NULL)
		tree->nextComplete=1;
	else
		tree->nextComplete=0;
}

void First(ThreadBiTree *tree)
{
	tree->current=tree->root;
	while(tree->current->leftThread==0)
		tree->current=tree->current->leftChild;
	if(tree->current==tree->root)
		tree->nextComplete=1;
	else
		tree->nextComplete=0;
}

void Next(ThreadBiTree *tree)
{
	ThreadBiNode *p=tree->current->rightChild;
	if(tree->nextComplete==1)
		return;
	if(tree->current->rightThread==0)
	{
		while(p->leftThread==0)
		{
			p=p->leftChild;
		}
	}
	tree->current=p;
	if(tree->current==tree->root)
		tree->nextComplete=1;
}

int EndOfNext(ThreadBiTree *tree)
{
	return tree->nextComplete;
}

ThreadBiNode *GetTreeNode(DataType item,ThreadBiNode *left,ThreadBiNode *right)
{
	ThreadBiNode *p;
	p=(ThreadBiNode *)malloc(sizeof(ThreadBiNode));
	p->data=item;
	p->leftChild=left;
	p->rightChild=right;
	return p;
}

void MakeCharTree(ThreadBiNode **root)
{
	ThreadBiNode *b,*c,*d,*e,*f,*g;
	g=GetTreeNode('G',NULL,NULL);
	d=GetTreeNode('D',NULL,g);
	b=GetTreeNode('B',d,NULL);
	e=GetTreeNode('E',NULL,NULL);
	f=GetTreeNode('F',NULL,NULL);
	c=GetTreeNode('C',e,f);
	*root=GetTreeNode('A',b,c);
}

源文件:例8-3.c

#include<stdio.h>
#include"InThreadIterator.h"

int main()
{
	ThreadBiNode *root;
	ThreadBiTree tree;
	MakeCharTree(&root);
	CreatInThread(&root);
	printf("二叉树中序正向遍历序列为:");
	ThreadInitiate(&tree,root);
	for(First(&tree);!EndOfNext(&tree);Next(&tree))
		printf("%c ",tree.current->data);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值