二叉树算法:查找节点路径

Bitree Find(Bitree BT,char ch)//查找节点
{
	Bitree p;
	***if(BT==NULL)
		return NULL;
	else if(BT->data==ch)
		return BT;
	else
	{
		p=Find(BT->lchild,ch);
		if(p!=NULL)
			return p;
		else
			return Find(BT->rchild,ch);
	}***
}
//查找节点路径(要先查找到想要的节点,才能进行这一步)
void Path(Bitree BT,Bitree s)
{
	Bitree Stack[100],p;
	int tag[100];
	int top = 0, i;
	p = BT;
	do
	{
		while (p != NULL)
		{
			top++;
			Stack[top] = p;
			tag[top] = 0;
			p = p->lchild;
		}
		if (top > 0)
		{
			if (tag[top] == 1)
			{
				if (Stack[top] == s)
				{
					printf("路径:");
					for (i = 1; i <= top; i++)
					{
						printf("%c ", Stack[i]->data);
					}
					printf("\n");
					break;
				}
				top--;
			}
			else
			{
				p = Stack[top];
				if(top>0)
				{
				    p = p->rchild;
				    tag[top] = 1;
				}
			}
		}

	} while (p != NULL || top > 0);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值