46 lines
1.2 KiB
Text
46 lines
1.2 KiB
Text
constant VALUE = 1, LEFT = 2, RIGHT = 3
|
|
|
|
constant tree = {1, {2, {4, {7, 0, 0}, 0},
|
|
{5, 0, 0}},
|
|
{3, {6, {8, 0, 0},
|
|
{9, 0, 0}},
|
|
0}}
|
|
|
|
procedure preorder(object tree)
|
|
if sequence(tree) then
|
|
printf(1,"%d ",{tree[VALUE]})
|
|
preorder(tree[LEFT])
|
|
preorder(tree[RIGHT])
|
|
end if
|
|
end procedure
|
|
|
|
procedure inorder(object tree)
|
|
if sequence(tree) then
|
|
inorder(tree[LEFT])
|
|
printf(1,"%d ",{tree[VALUE]})
|
|
inorder(tree[RIGHT])
|
|
end if
|
|
end procedure
|
|
|
|
procedure postorder(object tree)
|
|
if sequence(tree) then
|
|
postorder(tree[LEFT])
|
|
postorder(tree[RIGHT])
|
|
printf(1,"%d ",{tree[VALUE]})
|
|
end if
|
|
end procedure
|
|
|
|
procedure level_order(object tree, sequence more = {})
|
|
if sequence(tree) then
|
|
more &= {tree[LEFT],tree[RIGHT]}
|
|
printf(1,"%d ",{tree[VALUE]})
|
|
end if
|
|
if length(more) > 0 then
|
|
level_order(more[1],more[2..$])
|
|
end if
|
|
end procedure
|
|
|
|
puts(1,"\n preorder: ") preorder(tree)
|
|
puts(1,"\n inorder: ") inorder(tree)
|
|
puts(1,"\n postorder: ") postorder(tree)
|
|
puts(1,"\n level-order: ") level_order(tree)
|