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);
}
二叉树算法:查找节点路径
最新推荐文章于 2023-01-14 20:41:10 发布