Iterator in a Tree

What is a Iterator ( Iterator Class ) doing with a Tree? How does the Iterator know the order of the Childs?
[129 byte] By [jstillha] at [2007-9-30 6:03:21]
# 1

Think of a tree as a collection of nodes.

There are many kinds of collections, hashtables linked lists dynamic arrays, heaps, stacks...

In a collection you might sometimes want to iterate through all the nodes, and thats what the iterator is for. The order does not matter.

Ragnvald_id2 at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...
# 2

But how does the Iterator implement this in a Tree ADT?

Example for a height calculating method ( in a Tree ):

public static int height2(Tree T, Position v){

if(T.isExternal(v))

return 0;

else{

int h = 0;

Iterator children = T.children(v); //What does the Iterator do?

while (children.hasNext())

h = Math.max(h, height2(T, (Position) children.next()));

return 1 + h;

}

}

jstillha at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...
# 3
Are you talking about an iterator for the entire tree, or an iterator for the collection of direct children of a treenode?
Ragnvald_id2 at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...
# 4
Is this not the same?
jstillha at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...
# 5

There are many kinds of trees.

A tree node might have more descendants than direct children, so an iterator for the direct children of a node is not the same as an iterator for all nodes in a tree. Direct children excludes the parent, grandchildren, and grandchildrens descendants.

Ragnvald_id2 at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...
# 6
Thank's for your posts.I have read about iterators and now I know how they work.grezjstillha
jstillha at 2007-7-1 19:34:59 > top of Java-index,Other Topics,Algorithms...