64 lines
1.1 KiB
Text
64 lines
1.1 KiB
Text
tree[] = [ 1 2 3 4 5 6 -1 7 -1 -1 -1 8 9 ]
|
|
#
|
|
proc preorder ind .
|
|
if ind > len tree[] or tree[ind] = -1 : return
|
|
write " " & tree[ind]
|
|
preorder ind * 2
|
|
preorder ind * 2 + 1
|
|
.
|
|
write "preorder:"
|
|
preorder 1
|
|
print ""
|
|
#
|
|
proc inorder ind .
|
|
if ind > len tree[] or tree[ind] = -1 : return
|
|
inorder ind * 2
|
|
write " " & tree[ind]
|
|
inorder ind * 2 + 1
|
|
.
|
|
write "inorder:"
|
|
inorder 1
|
|
print ""
|
|
#
|
|
proc postorder ind .
|
|
if ind > len tree[] or tree[ind] = -1 : return
|
|
postorder ind * 2
|
|
postorder ind * 2 + 1
|
|
write " " & tree[ind]
|
|
.
|
|
write "postorder:"
|
|
postorder 1
|
|
print ""
|
|
#
|
|
global tail head queue[] .
|
|
proc initqu n .
|
|
len queue[] n
|
|
tail = 1
|
|
head = 1
|
|
.
|
|
proc enqu v .
|
|
queue[tail] = v
|
|
tail = (tail + 1) mod1 len queue[]
|
|
.
|
|
func dequ .
|
|
if head = tail : return -1
|
|
h = head
|
|
head = (head + 1) mod1 len queue[]
|
|
return queue[h]
|
|
.
|
|
initqu len tree[]
|
|
proc levelorder n .
|
|
enqu n
|
|
repeat
|
|
ind = dequ
|
|
until ind = -1
|
|
if ind < len tree[] and tree[ind] <> -1
|
|
write " " & tree[ind]
|
|
enqu ind * 2
|
|
enqu ind * 2 + 1
|
|
.
|
|
.
|
|
.
|
|
write "level-order:"
|
|
levelorder 1
|
|
print ""
|