Next: , Previous: , Up: Classes   [Index]


TreeNode

TreeNode Class

TreeNode objects and methods maintain links to other TreeNode objects (siblings), and to sets of objects (children). The methods can add sibling and child TreeNodes, traverse the tree, and set the content of each TreeNode.

The class provides basic methods for adding sibling and child nodes, adding content to each node (which is a String object), and to traverse the tree. The methods that visit each node in a tree– print, format, and search–are fairly generic. They perform a depth-first traversal which should work equally well with balanced and non-balanced trees.

Instance Variables

children

A List of TreeNode objects, which are accessible only through the parent node. See List.

content

A String that contains the node’s displayable text.

levelMargin

A String, normally consisting of all spaces, that is the additional left margin for each level of the tree. See String.

levelMarginLength

An Integer that is the length of the levelMargin string.

siblings

Another List of TreeNode objects that occur at the same level. See List.

Instance Methods

addChild (TreeNode child)

Add child to the end of the receiver’s children list.

format (void)

Print the receiver tree to a String object. This method uses two other methods, __formatChildren and __formatSiblings to traverse each TreeNode object in the receiver tree. Returns a String object.

makeSibling (TreeNode sib)

Add sib to the end of the receiver’s sibling list.

map (OBJECT *(method)())

Execute method over each member of the receiver tree. As with map methods in other classes, method must also belong to TreeNode class and takes no arguments.

print (void)

Print the receiver tree to the terminal. This method uses two other methods, __printChildren and __printSiblings to traverse each TreeNode object in the receiver tree.

search (String searchString)

Return the first node in the receiver tree whose content matches searchString. Returns only the first node that contains searchString. Does not look for multiple matches.

setContent (String str)

Sets the receiver TreeNode's content instance variable to str, a String object. See String.


Next: , Previous: , Up: Classes   [Index]