难题

icd 2004-04-02 08:11:19
如何根据一棵二叉树的前序、中序序列,勾造一棵二叉树。
...全文
35 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
kitigesh 2004-04-04
  • 打赏
  • 举报
回复
虽然结帖了,还是发一个吧
/*Function:
* rebirth the tree by it's preorder squence and inorder squence.
*Prototype:
* struct btnode * Create_Tree(int * PreorderSquen, int * InorderSquen, int SquenLength)
*Input parameter:
* PreorderSquen: an integer pointer, denote the start address of the preorder squence
* InorderSquen: an integer pointer, denote the start address of the inorder squence
* SquenceLength: an integer, denote the length of the squence, namely the number of the tree's node
*Output:
* a btnode pointer, point the root of the created tree
*Notice:
* the pointers, Preorder and Inorder, shouldn't be null
* also, the two squence should have the same set of nodes
* the value of SquenLength should be bigger than zero
*/

struct btnode
{
int value;
struct btnode * LeftChild;
struct btnode * RightChild;
};

struct btnode * Create_Tree(int * PreorderSquen, int * InorderSquen, int SquenLength)
{
int position_inorder; //save where the head element of preorder squence is in the inoder squence
struct btnode * head; //point the root of the tree

if (SquenLength > 0)
{
//initialize the new node
head = (struct btnode *)malloc(sizeof(struct btnode));
head->value = *PreorderSquen;
head->LeftChild = NULL;
head->RightChild = NULL;

position_inorder = Find(*PreorderSquen, InorderSquen, SquenLength);
/*here ignore the condition that the function Find return -1*/
head->LeftChild = Create_Tree(PreorderSquen++, InorderSquen, position_inorder);
head->RightChild = Create_Tree((PreorderSquen + position_inorder), (InorderSquen + position_inorder), (SquenceLength - position_inorder));
return head;
}
else
return NULL;
}

//return the element x position in object_squence, from 1 to length
int Find(int x, int * object_squence, int length)
{
int i;

i = 0;
while ((i < length) && (*(object_squence + i) != x))
{
i++;
}
if (i < length)
{
return (i+1);
}
else
{
return -1;
}
}
kitigesh 2004-04-03
  • 打赏
  • 举报
回复
to:bshaozi(C#在人在!)
当你觉得别人很傻,甚至是白痴的时候,常常是别人已经到了你远远未到的深度和广度!

mark一个

代码写好后马上贴!
Leeu 2004-04-03
  • 打赏
  • 举报
回复
楼主,是说要用程序编出来吗?

要是编出来还真有点难,
要不是写出结果就好办,有时人脑比电脑强,

电脑就涨着容量大,速度快, ————
runki 2004-04-03
  • 打赏
  • 举报
回复
同意楼上的观点
yzx1983 2004-04-03
  • 打赏
  • 举报
回复
不过bshaozi(C#在人在!) 的代码的确不符合要求。
还是等kitigesh(kitigesh)的代码吧。
yzx1983 2004-04-03
  • 打赏
  • 举报
回复
kitigesh(kitigesh)在说什么?偶怎么看不懂呢?
icd 2004-04-03
  • 打赏
  • 举报
回复
谢谢各位,谢谢wythust(小涛) 和newegg2002(天生我菜有X用) 说的很好
另外,我在http://search.csdn.net/expert/topic/14/1404/2002/3/31/612209.xml处找到了详细说明,而且我已经按照他们说的写出了程序。

已知前序是ABCDEFG 中序是CBEDAFG 求二叉树

首先从取前序第1个字母(A) 按此字母把中序分成两段 (CBED) (AFG)
A做根 (CBED)做左子树 (FG)右子树
再按长度把前序后面的部分分成(BCDE) (FG)

问题就转换成
已知前序是BCDE 中序是CBED 求二叉树

已经前序是FG 中序是FG 求二叉树

看出来了没有,递归求解的.
下面的步骤省略分析.
前序 中序 : 根 左子树分解 右子树分解
BCDE CBED : B (C) (C) (ED) (DE)
C C : C
DE  ED : D (E) (E)
E E : E

FG FG : F (G) (G)
G G : G
得出的二叉树就是
A
/ B F
/ \ C D G
/
E

bshaozi 2004-04-02
  • 打赏
  • 举报
回复
struct btnode
{
int a;
btnode *lchild;
btnode *rchild;


};
void ct_preorde(btnode* p)+
{

int i;
cout<<"in to:"<<endl;
cin>>i;
if(i==0)
p=NULL;
else

{
btnode *p=new btnode;
p->a=i;
ct_preorde(p->lchild);
ct_preorde(p->rchild);
}
这就是前序;
至于中序和后续;
把输入值的位置变化一下就行了啊~


}
newegg2002 2004-04-02
  • 打赏
  • 举报
回复
我是这样理解的小涛的意思的。。
因为在取出结点的时候。用的是前序的取法。根据前序的算法,那么就有两种情况。
A根结点 B左子树
A左子树 B右子树
而在中序中。却是先遍历左子树的。次之才为根结点,所以若在中序序列中(B)先于(A),那么必是第一种情况,则(B)为(A)的左儿子也就显得显然的了。。,
不知道我有没有误会。或者曲解小涛的意思,若有。在此请罪。。
icd 2004-04-02
  • 打赏
  • 举报
回复
谢谢wythust(小涛)
可否再说的仔细一点,
为什么在中序序列中(B)先于(A),则(B)为(A)的左儿子。右儿子怎么确定呢?
bm1408 2004-04-02
  • 打赏
  • 举报
回复
晕!
data structure!
wythust 2004-04-02
  • 打赏
  • 举报
回复
简单啊,先根据前序序列,每次取一个结点(B),取出的结点与前一个取出的结点(A)比较,若在中序序列中(B)先于(A),则(B)为(A)的左儿子,依此类推
langjifengyu 2004-04-02
  • 打赏
  • 举报
回复
我不动

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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