Recent

Author Topic: [Solved] Pre order traversal of a tree  (Read 1765 times)

lainz

  • Hero Member
  • *****
  • Posts: 4742
  • Web, Desktop & Android developer
    • https://lainz.github.io/
[Solved] Pre order traversal of a tree
« on: August 06, 2019, 03:10:45 am »
Say I have two classes

Myobject
-name
-id
-quantity
-items: myclass

Myclass
- optionals: array of myobject
- additional: something else

A: myclass

A.optionals[0].items.optionals[0].name...

It starts on A that is myclass type. Containing an array of myobject type.

Then if items (myclass) is assigned or != Nil I must look for subitems, else stop and continue if remaining in another item or subitem

The order must be pre order traversal.

Any good examples you can point me?

PD: this is not homework. Thanks.
« Last Edit: August 06, 2019, 11:35:04 pm by lainz »

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: Pre order traversal of a tree
« Reply #1 on: August 06, 2019, 03:52:18 am »
You can simply use a recursive function.
(sorry, no working examples, I'm on mobile)

Something like (pseudo code)
Code: [Select]
function foo(my: myclass)
begin
  for x being 0 to my.optionals.count - 1
  begin
     If my.optionals[x].items <> nil do
        foo(my.optionals[x].items)
     Handle my.optionals[x].rest
  end
  Handle my.additional
end

This should give you pre order traversal.
« Last Edit: August 06, 2019, 03:54:03 am by rvk »

julkas

  • Guest
Re: Pre order traversal of a tree
« Reply #2 on: August 06, 2019, 09:34:13 am »
Alternative - use stack.
Code: Text  [Select][+][-]
  1. push(root node)
  2. while (stack not empty) {
  3.   pop node
  4.   proc node
  5.   push node childs from the last to the first
  6. }
  7.  
« Last Edit: August 06, 2019, 10:20:01 am by julkas »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12771
  • FPC developer.
Re: Pre order traversal of a tree
« Reply #3 on: August 06, 2019, 12:03:55 pm »
Alternative - use stack.
Code: Text  [Select][+][-]
  1. push(root node)
  2. while (stack not empty) {
  3.   pop node
  4.   proc node
  5.   push node childs from the last to the first
  6. }
  7.  

That code processes the root node before the children of the root node, and isn't systematically preorder. Fixed:

Code: Text  [Select][+][-]
  1. push root node childs from the last to the first
  2. push(root node)
  3. while stack not empty do
  4.    begin
  5.       pop node
  6.       proc node
  7.       push node childs from the last to the first
  8.    end
  9.  

I assume the last to first bit is a matter of taste) or spec.

julkas

  • Guest

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12771
  • FPC developer.
Re: Pre order traversal of a tree
« Reply #5 on: August 06, 2019, 12:32:10 pm »
Never mind, I was wrong.

lainz

  • Hero Member
  • *****
  • Posts: 4742
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Pre order traversal of a tree
« Reply #6 on: August 06, 2019, 11:34:52 pm »
Thanks rvk and others, it works pretty well with the recursive way.

 

TinyPortal © 2005-2018