- Inherits from:
- Object
- Declared in:
- DTree.h
Object
|
+---DTreeIterator
Class Description
The tree iterator class implements an iterator on a n-tree collection.
Because there is no key or index available to store and remove the objects,
the objects can only be stored and removed by this iterator.
- Example:
#include <stdio.h>
#include "ofc/DTree.h"
#include "ofc/DText.h"
int main(int argc, char *argv[])
{
DTree *tree = [DTree new ];
DTreeIterator *iter = [DTreeIterator alloc];
DText *str1;
DText *str2;
int level;
[iter init :tree]; // Init the iterator with the tree
str1 = [DText new]; [str1 set :"root"];
[iter append :str1]; // Set the root
str1 = [DText new]; [str1 set :"child1"];
[iter append :str1]; // Append a child to the root and ..
[iter parent]; // .. move the iterator back to the root
str1 = [DText new]; [str1 set :"child2"]; // Append again a child to the root and ..
[iter append :str1]; // .. keep the iterator on the child
str1 = [DText new]; [str1 set :"child21"]; // Append a child for the chilld
[iter append :str1];
str1 = [DText new]; [str1 set :"child22"]; // Append another child
[iter after :str1];
[iter parent]; // Move iter back to level 2
str1 = [DText new]; [str1 set :"child4"];
[iter after :str1]; // Append after the child a new child
str2 = [DText new]; [str2 set :"child3"];
[iter before :str2]; // Prepend before the child a new child
printf("Elements in the tree:%ld.\n", [tree length]);
printf("Tree %s child3.\n", ([tree has :str2] ? "has" : "has not"));
level = 1;
str1 = [iter root]; // Move the iter to the root
while (str1 != nil)
{
printf("Level:%d Element:%s.\n", level, [str1 cstring]);
if ([iter hasChildren]) // If the node has children, than move to the first child
{
level++;
str1 = [iter child];
}
else if ([iter isLast]) // If all children done, than move to the next of the parent
{
level--;
str1 = [iter parent];
if (str1 != nil)
str1 = [iter next];
}
else
{
str1 = [iter next]; // Else next child
}
}
[iter free]; // Cleanup
[tree free];
return 0;
}
- Last modified:
- 05-Aug-2008 (DTree.h)
Instance Variables
- private DTree *_tree
- the tree
- private DTreeNode *_node
- the current node
- Constructors
- - (DTreeIterator *) init
- Initialise an empty iterator
- Returns:
- the object
- - (DTreeIterator *) init :(DTree *) tree
- Initialise the iterator with a tree (iterator is moved to the root)
- Parameters:
- tree - the tree for the iterator (<> nil)
- Returns:
- the object
- Movement methods
- - (id) child
- Move to the first child (for the current parent)
- Returns:
- the object in the first child (or nil)
- - (id) first
- Move to the first child (in the list of childs)
- Returns:
- the object of the first child (or nil)
- - (id) last
- Move to the last child (in the list of childs)
- Returns:
- the object in the last node (or nil)
- - (BOOL) move :(id) obj
- Move the iterator to a certain object. The object is searched
from the root. If the object is not found, the iterator
remains on the same location as before calling this method.
If the object is found, the iterator is moved to this object.
- Parameters:
- obj - the object to be found
- Returns:
- is it found ?
- - (id) next
- Move to the next child (in the list of childs)
- Returns:
- the object of the next child (or nil)
- - (id) parent
- Move to the parent (for the current child)
- Returns:
- the object in the parent (or nil)
- - (id) prev
- Move to the previous child (in the list of childs)
- Returns:
- the object of the previous child (or nil)
- - (id) root
- Move to the root
- Returns:
- the object in the root (or nil)
- Test methods
- - (BOOL) hasChildren
- Test if the iterator is on a parent with children
- Returns:
- has it ?
- - (BOOL) isFirst
- Test if the iterator is on the first child (in the list of childs)
- Returns:
- is it ?
- - (BOOL) isLast
- Test if the iterator is on the last child (in the list of childs)
- Returns:
- is it ?
- - (BOOL) isRoot
- Test if the iterator is on the root
- Returns:
- is it ?
- Stored object methods
- - (id) object
- Get the object in the current node
- Returns:
- the object in the node (or nil)
- - (id) object :(id) obj
- Set the object in the current node
- Parameters:
- obj - the object to be set
- Returns:
- the previous object in the node
- AddInsertRemove methods
- - (DTreeIterator *) after :(id) obj
- Insert a new child after current (child) node (iterator is moved to new child)
- Parameters:
- obj - the object to be added as child
- Returns:
- the object
- - (DTreeIterator *) append :(id) obj
- Append a child to current (parent) node (iterator is moved to new child)
- Parameters:
- obj - the object to be added as child
- Returns:
- the object
- - (DTreeIterator *) before :(id) obj
- Insert a new child before current (child) node (iterator is moved to new child)
- Parameters:
- obj - the object to be added as child
- Returns:
- the object
- - (DTreeIterator *) prepend :(id) obj
- Prepend a child to the current (parent) node (iterator is moved to this new child)
- Parameters:
- obj - the object to be added as child
- Returns:
- the object
- - (id) remove
- Remove the current node
Note: only if current node has no children
Note: iterator moves to 1. the next child or 2. the previous child or 3. the parent
- Returns:
- the object from the current node (or nil)
generated 06-Sep-2008 by ObjcDoc 3.0.0