'TRAVERSING A TREE

uses console

class branches
  '
  int level
  int i
  branches*br1
  branches*br2
  '
  method constructor(int n)
  =========================
    level=n
    if level>0
      new branches b1(n-1)
      new branches b2(n-1)
      @br1=@b1
      @br2=@b2
    endif
    ...
  end method
  '
  method destructor()
  ===================
    i=0
    if level>0
      del br1
      del br2
    endif
    ...
  end method
  '
  method Preorder()
  =================
  if i then output i "  "
  if @br1 then br1.Preorder
  if @br2 then br2.Preorder
  end method
  '
  method Inorder()
  ================
  if @br1 then br1.Inorder
  if i then output i "  "
  if @br2 then br2.Inorder
  end method
  '
  method Postorder()
  ================
  if @br1 then br1.postorder
  if @br2 then br2.postorder
  if i then output i "  "
  end method
  '
  method Lorder(int h)
  ========================
  if level=h
    if i then output i "  "
  endif
  if level>h
  if @br1 then br1.lorder(h)
  if @br2 then br2.lorder(h)
 endif
 end method

  method Levelorder()
  ========================
  int i
  for i=level to 1 step -1
    Lorder(i)
  next
  end method
  '
end class

'#recordof branches

new branches br(4) '4 levels
br.i=1
br.br1.i=2
br.br2.i=3
br.br1.br1.i=4
br.br1.br2.i=5
br.br2.br1.i=6
br.br1.br1.br1.i=7
br.br2.br1.br1.i=8
br.br2.br1.br2.i=9
'
br.Preorder   : output cr
br.Inorder    : output cr
br.Postorder  : output cr
br.Levelorder : output cr
output cr

pause
del br
   