树-线索化-我就不信搜不到

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

typedef struct Node
{
	char data;
	struct Node *LChild;
	struct Node *RChild;
}BiTNode, *BiTree;

int depth = 0;
BiTree Creat(BiTree *bt)
{
	char ch;
	ch = getchar();
	if(ch =='.')
		*bt = NULL;
	else
	{
		*bt = (BiTree)malloc(sizeof(BiTNode));
		(*bt)->data = ch;
		Creat(&((*bt)->LChild));
		Creat(&((*bt)->RChild));
	} 
}

BiTree Preorder(BiTree *bt)
{
	if((*bt) != NULL)
	{
		printf("%c", (*bt)->data);
		Preorder(&((*bt)->LChild));
		Preorder(&((*bt)->RChild));
	}
}

BiTree printLeaf(BiTree bt)
{
	if((bt) != NULL)
	{
		if((bt->LChild == NULL) && (bt->RChild == NULL))
			printf("%c", bt->data);
		printLeaf(bt->LChild);
		printLeaf(bt->RChild);
	}
}

int Leaf(BiTree bt)
{
	int count;
	if(bt == NULL)
		count = 0;
	else if((bt->LChild == NULL) && (bt->RChild == NULL))
		count = 1;
	else
		count = Leaf(bt->LChild) + Leaf(bt->RChild);
	return count;
}

int postDepth(BiTree bt)
{
	int hl,hr,max;
	if(bt != NULL)
	{
		hl = postDepth(bt->LChild);
		hr = postDepth(bt->RChild);
		max = hl>hr?hl:hr;
		return (max+1);
	}
	else
		return 0;	
}

int preDepth(BiTree bt, int h)
{
	
	if(bt != NULL)
	{
		if(h>depth)
			depth = h;
		preDepth(bt->LChild, h+1);
		preDepth(bt->RChild, h+1);
	}
	return depth;
}

void PrintTree(BiTree Boot, int nLayer) //shuxiang
{
	int i;
	if(Boot == NULL) 
		return; 
	PrintTree(Boot->RChild, nLayer+1); 
	for(i=0; i<nLayer; i++)
		printf(" "); 
	printf("%c \n", Boot->data); 
	PrintTree(Boot->LChild, nLayer+1); 
} 

int main()
{
	int count, h = 1, height1, height2,  nLayer = 5;
	BiTree bt;
	Creat(&bt);
	//	Preorder(&bt);
	count = Leaf(bt);
	//	printf("\n%d\n", count);
	//   printLeaf(bt);
	//	height1 = postDepth(bt);
	height2 = preDepth(bt, h);
	//printf("%d",  height2);
	PrintTree( bt, nLayer) ;
	return 0;
}

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

typedef struct Node  //定义线索二叉树结点类型 
{
    char data;	//元素数据
    char Ltag ; //左标志 
    char Rtag ; //右标志 
    struct Node *LChild;
	struct Node *RChild;
}BiTNode, *BiTree;


BiTree Creat(BiTree *bt)
{
	char ch;
	ch = getchar();
	if(ch =='.')
		*bt = NULL;
	else
	{
		*bt = (BiTree)malloc(sizeof(BiTNode));
		(*bt)->data = ch;
		Creat(&((*bt)->LChild));
		Creat(&((*bt)->RChild));
	} 
}

BiTree pre = NULL;
void Inthread(BiTree root)/* 对root所指的二叉树进行中序线索化,其中全局变量pre始终指向刚访问过的结点,其初值为NULL*/
{ 
	if(root!=NULL)
	{
		Inthread(root->LChild); /* 线索化左子树 */
		if(root->LChild==NULL)
		{
			root->Ltag=1;
			root->LChild=pre;/*置前驱线索 */
		}
		if(pre!=NULL&& pre->RChild==NULL) 
		{
			pre->Rtag=1;
			pre->RChild=root ;/*置后继线索 */ 
		}
		pre=root;
	
		Inthread(root->RChild); 
		/*线索化右子树*/
	}
}


BiTNode *InPre(BiTNode *p,BiTree pre)/* 在中序线索二叉树中查找p的中序前驱,并用pre指针返回结果 */
{  
	BiTree q;
	if(p->Ltag==1) 
		pre= p->LChild; /*直接利用线索*/
	else/*在p的左子树中查找“最右下端”结点 */
	{	
		for(q= p->LChild; q->Rtag!=1; q=q->RChild); 
		pre=q;
	}
	//printf("%c", pre->data);
	return pre;
} 

BiTNode *InSucc(BiTNode *p) /*在中序线索二叉树中查找p的中序后继结点,并用succ指针返回结果*/
{
	BiTree succ, q;
	if(p->Rtag==1 || p->RChild==NULL) 
		succ= p-> RChild;  /*直接利用线索*/
	else/*在p的右子树中查找“最左下端”结点*/
	{
		for(q=p->RChild;q->Ltag!=1; q=q->LChild); 
		succ=q;
	}
	printf("%c", succ->data);
	return succ;
} 


void TraverseInorderThrTree(BiTree p)//遍历中序线索二叉树
{
	if(p)//树非空
	{
		while(p->Ltag != 1)
		{
			p = p->LChild;
		//从根往下找最左下节点,即中序序列的开始节点
		}
		do
		{
			printf("%c ", p->data);//访问节点a
			p = InSucc(p);//找*p的中序后继
		}while(p);
	}
}


void PrintTree(BiTree Boot, int nLayer) //shuxiang
{
	int i;
	if(Boot == NULL) 
		return; 
	PrintTree(Boot->RChild, nLayer+1); 
	for(i=0; i<nLayer; i++)
		printf(" "); 
	printf("%c \n", Boot->data); 
	PrintTree(Boot->LChild, nLayer+1); 
} 



int main()
{
	BiTree bt;
	int  nLayer = 5;
	printf("输入前序二叉树:\n");
	Creat( &bt);
	//PrintTree( bt, nLayer) ;
	Inthread(bt);
	//InPre(bt,pre);
	//InSucc(bt);
	printf("输出中序序列:\n");
	TraverseInorderThrTree(bt);
	printf("\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值