建立二叉树并进行中序遍历(Pascal语言)

来源:百度知道 编辑:UC知道 时间:2024/05/17 05:37:53
建立二叉树并进行中序遍历(Pascal语言)
要源代码,要有解释并且一目了然,就给他10分。
各位高手请帮帮忙。

Type
Tree = ^Node;
Node = Record
Key : Integer;
Left, Right : Tree;
End;
Var
Root : Tree;
i, n, x : Integer;

Procedure Print(p : Tree); {中根遍历打印过程}
Begin
If p = Nil Then Exit;
Print(p^. Left);
Write(p^. Key, ' ');
Print(p^. Right);
End;

Procedure BuildTree(Var p : Tree);{建树过程};
Begin
If p = Nil Then Begin
New(p); p^. Key := x;{x为插入数}
p^. Left := Nil; p^. Right := Nil; {左右子树置空}
End Else
If x < p^. Key Then Build(p^. Left)
Else Build(p^. Right);{小的在左子树,大的在右子树}
Print(Root);
End;
Begin
Readln(n); Root := Nil;
For i := 1 To n Do Begin
Read(x);
Build(Root);
End;
End.

是排序的吗?