二叉树中求位于先序序列中第k个位置的结点的值

ekisstherain 2009-04-24 01:44:21
编写递归算法,在二叉树中求位于先序序列中
第k个位置的结点的值。

要求实现下列函数:
TElemType PreOrder(BiTree bt, int k);
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose */
/* position number is k, and return its value. */
/* if can't found it, then return '#'. */

二叉链表类型定义:
typedef struct BiTNode {
TElemType data;
BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
...全文
1906 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
EachFor 2012-06-04
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

上面算法有问题,如上定义:BiTree a[20],当树的左结点数多了就不适用了...
贴上我的代码吧,虽然没有分了…

int countNodes(BiTree bt){ //本函数返回树bt的结点总数
return bt ? (1 + countNodes(bt->lchild) + countNodes(bt->rchild)) : 0; //树和结点总数……
[/Quote]
俺也是广工的,“count>=--k ? PreOrder(bt->lchild,k) : PreOrder(bt->rchild,k-count)”这都能想出来
codinghom 2011-04-26
  • 打赏
  • 举报
回复
你1楼得错法是因为 在递归函数里面返回值 在上层函数无法接住,最终的返回值就为零!
codinghom 2011-04-26
  • 打赏
  • 举报
回复
我也在做这个题! anyview的! 想想不难但越想问题越多!
郁闷! 学习!
Even_GuangZhou 2009-06-08
  • 打赏
  • 举报
回复
上面算法有问题,如上定义:BiTree a[20],当树的左结点数多了就不适用了...
贴上我的代码吧,虽然没有分了…

int countNodes(BiTree bt){ //本函数返回树bt的结点总数
return bt ? (1 + countNodes(bt->lchild) + countNodes(bt->rchild)) : 0; //树和结点总数等于根结点数(就是1)加上左子树和右子树的结点总数。
}//countNodes

TElemType PreOrder(BiTree bt, int k)
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose */
/* position number is k, and return its value. */
/* if can't found it, then return '#'. */
{
int count; //用于记录树的左子树的结点总数。
if(k <=0||!bt) return '#'; //当k为0或者bt空时,没有找到结点,返回'#'。
if(k==1) return bt->data; //当k从给定的数减到1时,表示找到,返回bt->data。
count = countNodes(bt->lchild); //取得树bt的左树的结点总数赋给count。
return count>=--k ? PreOrder(bt->lchild,k) : PreOrder(bt->rchild,k-count); //先判断所求结点在左子树中还是在右子树中,再递归求得。
}//PreOrder

写了个函数countNodes,计算结点数的,(我也是广工的,那个系统可以自己写函数,但函数要写在要调用函数的前面。)
代码不多,能解释的基本都写上面了,有问题再交流吧(^∀^)
xxh29 2009-06-04
  • 打赏
  • 举报
回复
你简直是我的偶像阿
ekisstherain 2009-04-25
  • 打赏
  • 举报
回复
还是自己来吧~~~~~想知道的看看啦^_^

以下是题目:
编写递归算法,在二叉树中求位于先序序列中
第k个位置的结点的值。

要求实现下列函数:
TElemType PreOrder(BiTree bt, int k);
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose */
/* position number is k, and return its value. */
/* if can't found it, then return '#'. */

二叉链表类型定义:
typedef struct BiTNode {
TElemType data;
BiTNode *lchild, *rchild;
} BiTNode, *BiTree;


以下是其中的一个方法:

TElemType PreOrder(BiTree bt, int k)
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose */
/* position number is k, and return its value. */
/* if can't found it, then return '#'. */
{
int i=0,count=0; //i为循环变量、count为记录根结点的左子树的结点个数
TElemType T; //定义一个返回值变量T
BiTree a[20],b; //a[20]记录左子树的根结点,b为一个中间变量
if(k<=0||!bt) return '#'; //当k为0或者bt空时,没有找到结点,返回'#'
if(k==1) return bt->data; //当k从给定的数减到1时,表示找到,返回bt->data
T=PreOrder(bt->lchild,k-1); //左子树递归
if(T!='#') //当找到T时,返回T
return(T);
b=bt->lchild; //没有找到,继续找
while(b||i) //下面是计算左子树有多少个结点的算法,并记录在count中
{
if(b) //当左子树非空时,
{
a[i]=b; //记录第i个结点在a[i]中,用来计算本结点的右子树用的
i++; //计算下一个结点的序号
b=b->lchild; //对下一个结点操作
count++; //记录结点数
}
else
{
i--; //如果b空,表示执行到最左的叶子,现在要找到上一个结点的右子树
b=a[i]; //把上一个结点赋给b
b=b->rchild; //使b指向右子树
}
}
T=PreOrder(bt->rchild,k-count-1); //递归右子树,并返回k-count-1,表示要找的结点可能在右子树的第k-count-1的位置
if(T!='#') //对T判断,当T不为'#'时,表示找到,返回T
return (T);
else //否则返回 '#'表示没有找到
return '#';
} //至此,结束

解释不是很好,希望可以看明白啦^_^
ekisstherain 2009-04-25
  • 打赏
  • 举报
回复
还是没有知道啊???
ekisstherain 2009-04-24
  • 打赏
  • 举报
回复
首先这个函数是在一个系统中测试的,
所以定义不了全局变量,
要是可以定义那么一个变量,也不会那么难了

没有人对递归比较了解的吗,
以上的所有意见都没有满意的结果~~~~
还在等待中,希望高人指教 >_<
flythere 2009-04-24
  • 打赏
  • 举报
回复
上面的好像还有点问题,再参照5楼的
k=k-1;
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,k);
}

改为
m=k-1; //m为全局变量
if(bt->lchild)
{
PreOrder(bt->lchild,m);
}
if(bt->rchild)
{
PreOrder(bt->rchild,m);
}
cyldf 2009-04-24
  • 打赏
  • 举报
回复

改成:
else
{
if(bt->lchild)
{
PreOrder(bt->lchild,k-1);
}
if(bt->rchild) //此处不要else
{
PreOrder(bt->rchild,k-1);
}
}

cyldf 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hairetz 的回复:]
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
if(bt->rchild)
{
PreOrder(bt->rchild,k);
}
[/Quote]
正解,刚才一直以为k的问题,没注意if/else中出问题了。
ekisstherain 2009-04-24
  • 打赏
  • 举报
回复
typedef struct BiTNode {
TElemType data;
BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

这就是二叉树的结构定义,函数实现的功能就是按照先序序列的顺序把输入第k个结点的数据~~~~~
就这样,例如:


这棵树结构为:T3 = V(L(A(#,K),Q(N,U)),Z(Y(W,#),#))
运行结果为A$: k=7 --> U 即是当k=7时,找到的结点是U,函数的功能就是为了找到U并输出~~~~

有没有人可以帮忙的啊~~~~好烦中
flythere 2009-04-24
  • 打赏
  • 举报
回复
k=k-1;
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,k);
}
换成

m=k-1; //m为全局变量
if(bt->lchild)
{
PreOrder(bt->lchild,m);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,m);
}

试试看
  • 打赏
  • 举报
回复
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
if(bt->rchild)
{
PreOrder(bt->rchild,k);
}
cyldf 2009-04-24
  • 打赏
  • 举报
回复
把你的建树代码也发上来吧
ekisstherain 2009-04-24
  • 打赏
  • 举报
回复
谢谢你,不过现是不能得到正确结果
cyldf 2009-04-24
  • 打赏
  • 举报
回复

else
{
k=k-1;
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,k);
}
}
改成
else
{
if(bt->lchild)
{
PreOrder(bt->lchild,k-1);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,k-1);
}
}
试试看
ekisstherain 2009-04-24
  • 打赏
  • 举报
回复
TElemType PreOrder(BiTree bt, int k)
/* bt is the root node of a binary linked list, */
/* Preorder travel it and find the node whose */
/* position number is k, and return its value. */
/* if can't found it, then return '#'. */
{

if(bt&&k!=0)
{
if(k==1)
{
return bt->data;
}
else
{
k=k-1;
if(bt->lchild)
{
PreOrder(bt->lchild,k);
}
else if(bt->rchild)
{
PreOrder(bt->rchild,k);
}
}
}
else
return '#';
}
自己写的,就是结果不正确

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧