给出一个前序遍历,给出一个中序遍历,要求把树输出
给出算法答案如下:
main()
{
Datatype preorder[n], inorder[n];
Struct link
*
BT;
if
(n
>
0
)
BT
=
creatBT(
0
, n
-
1
,
0
, n
-
1
);
return
(BT);
}
struct
link
*
createBT(
int
prestart,
int
preend,
int
instart,
int
inend)
{
p
=
(
struct
link
*
)malloc(
sizeof
(
struct
link);
p
->
lchild
=
null
;
p
->
rchild
=
null
;
p
->
data
=
preorder[prestart];
if
(prestart
>
preend)
{
for
(
int
i
=
instart; inorder[i]
!=
preorder[start]; i
++
);
if
(i
>
instart)
p
->
lchild
=
createBT(prestart
+
1
, prestart
-
instart
+
1
, instart, i
-
1
);
if
(i
<
inend)
p
->
rchild
=
createBT(prestart
-
instart
+
i
+
1
, preend, i
+
1
, inend);
}
return
(p):
}