Last modified 9 July 2003 by jdavis@math.wisc.edu

SMKBinaryTree

Inherits from NSObject
Conforms to NSCoding, NSCopying

SMKBinaryTree implements a simple, lightweight binary tree. The tree is a recursive data structure; each node in the tree is itself a tree with two subtrees, both of which are retained by the tree. The tree does not maintain a pointer to its parent. The tree doesn't store any data, either; you add data fields in subclasses. To access the left and right subtrees, use the following methods.

- (id)setLeftChild:(SMKBinaryTree *)tree;

Sets the left subtree to that indicated, releasing the previous occupant, if any.

- (id)leftChild;

Returns the left subtree.

- (id)setRightChild:(SMKBinaryTree *)tree;

Sets the right subtree to that indicated, releasing the previous occupant, if any.

- (id)rightChild;

Returns the right subtree.

The next method helps you remove nodes from a tree.

- (id)removeRoot;

Removes the root (that is, the receiving node) from the tree and returns a reconfigured tree. This method does not release the root or retain the result. This method is guaranteed to preserve any binary search tree structure that happens to be imposed on the tree.

For example, this excerpt shows how to remove the left child of the root of a tree:

    [root setLeftChild:[[root leftChild] removeRoot]];

The remaining methods make depth-first traversals of the tree, invoking variants of NSObject's performSelector: at each node. All of these methods return the receiver, rather than the results of the selectors.

- (id)iterateSelector:(SEL)selector;

Invokes performSelector: at each node.

- (id)iterateSelector:(SEL)aSelector withObject:(id)anObject;

Invokes performSelector:withObject: at each node.

- (id)iterateSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject;

Invokes performSelector:withObject:withObject: at each node.

- (id)iterateSelector:(SEL)aSelector thenSelector:(SEL)anotherSelector;

At each node, invokes performSelector: and then invokes performSelector: on that result.

- (id)iterateSelector:(SEL)aSelector thenSelector:(SEL)anotherSelector withObject:(id)anObject;

At each node, invokes performSelector: and then invokes performSelector:withObject: on that result.

- (id)iterateSelector:(SEL)aSelector thenSelector:(SEL)anotherSelector withObject:(id)anObject withObject:(id)anotherObject;

At each node, invokes performSelector: and then invokes performSelector:withObject:withObject: on that result.