数据结构中左右子树拷贝问题,在线等待~~~`
在数据结构中有关左右子树拷贝问题,请给一算法实例 问题点数:100、回复次数:8Top
1 楼beyond_goal(飞火流星)回复于 2002-12-14 21:29:06 得分 0
怎么没人回啊,在线等待立刻给分Top
2 楼strafer(努力中...)回复于 2002-12-14 22:28:24 得分 0
和遍历一样呀。Top
3 楼strafer(努力中...)回复于 2002-12-14 22:32:30 得分 25
从老外的书上拽了个伪代码,你可以参考一下。
procedure COPY(T)
//for a binary tree T, COPY returns a pointer to an exact copy of
T; new nodes are gotten using the usual mechanism//
Q 0
if T 0 then [R COPY(LCHILD(T)) //copy left subtree//
S COPY(RCHILD(T)) //copy right subtree//
call GETNODE(Q)
LCHILD(Q) R; RCHILD(Q) S
//store in fields of Q//
DATA(Q) DATA(T)]
return(Q) //copy is a function//
end COPY
Top
4 楼strafer(努力中...)回复于 2002-12-14 22:34:13 得分 0
ft,所有等号,指针符号都没了,你看个大概意思吧。Top
5 楼lins(Anders*小明)回复于 2002-12-14 23:24:06 得分 0
不会吧!基本功啊!Top
6 楼beyond_goal(飞火流星)回复于 2002-12-15 01:07:22 得分 0
再详细点please,我是菜鸟Top
7 楼boytan()回复于 2002-12-15 17:03:08 得分 0
NODE *copy_binarytree(NODE *t)
{
NODE * newtree;
if(t!=NULL)
{
newtree=(NODE *)malloc(sizeof(NODE));
newtree->data=t->data;
newtree->lchild=copy_binarytree(t->rchild);
newtree->rchild=copy_binarytree(t->lchild);
}
return newtree;
}
Top
8 楼boytan()回复于 2002-12-15 17:06:01 得分 75
sorry 少了一句
NODE *copy_binarytree(NODE *t)
{
NODE * newtree;
if(t!=NULL)
{
newtree=(NODE *)malloc(sizeof(NODE));
newtree->data=t->data;
newtree->lchild=copy_binarytree(t->rchild);
newtree->rchild=copy_binarytree(t->lchild);
}
else return NULL;
return newtree;
}Top




