List and Binary Tree Iterator...

Preview:

Citation preview

List and Binary Tree Iterator ImplementationLecture 35

Section 14.6

Robb T. Koether

Hampden-Sydney College

Fri, Apr 26, 2013

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 1 / 35

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 2 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 3 / 35

A Sort Function

Sorting with Iteratorsvoid LinkedListwIter<T>::sortIter(){

Iterator base = begin();while (base != end()){

Iterator min = base;Iterator curr = min;++curr;while (curr != end()){

if (*curr < *min)min = curr;

++curr;}T temp = *base;

*base = *min;

*min = temp;++base;

}return;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 4 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 5 / 35

List Traversals

Definition (Traverse)To traverse a list is to move systematically through its nodes, “visiting”each node along the way. Forward traversals go from head to tail.Reverse traversals go from tail to head.

The meaning of “visiting” a node is left unspecified at this point.The meaning will be specified at the time of the traversal.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 6 / 35

The Traversal Function

The traverse() Functionvoid traverse(void (*visit)(Iterator&));

Introduce a new List member function traverse().The parameter visit is a pointer to a function.The visit() function has prototype

void visit(Iterator& it);

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 7 / 35

Traversals and Iterators

Traversal Implementationvoid traverse(void (*visit)(Iterator&)){

for (Iterator it = begin(); it != end(); ++it)visit(it);

return;}

The traverse() function is implemented as a for loop.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 8 / 35

Example - Print the List

The print() Functionvoid print(Iterator& it){

cout << *it << endl;return;

}...

list.traverse(print);

For example, we could write a print() function and then usetraverse() to print all the values in the list.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 9 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 10 / 35

Reverse Iterators

A reverse iterator is an iterator that advances in the oppositedirection, from tail to head.It is initialized to the last element in the list.It “advances” until it has gone beyond the head of the list.Because a reverse iterator is an iterator, we will derive theReverseIterator class from the Iterator class.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 11 / 35

ReverseIterator Member Functions

Additional ReverseIterator Member FunctionsReverseIterator(const LinkedListwIter<T>* lst,

LinkedListNode<T>* p);ReverseIterator& operator++();

ReverseIterator(LinkedListwIter<T>*,LinkedListNode<T>*) – Construct a ReverseIterator.operator++() – Advance the ReverseIterator to the nextnode.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 12 / 35

LinkedListwIter Member Functions

Additional LinkedListwIter Member FunctionsReverseIterator rbegin() const;ReverseIterator rend() const;

rbegin() – Create a ReverseIterator set to the beginning(tail) of the list.rend() – Create a ReverseIterator set to the end (beyondthe head) of the list.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 13 / 35

LinkedListwIter Member Functions

The other LinkedListwIter member functions that useiterators, such as the iterator version of getElement(), canaccept reverse iterators as well because. . .That is because

A ReverseIterator IS-A Iterator.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 14 / 35

Implemention of Reverse Iterators

To construct a reverse iterator for a linked list,Introduce a stack data member.Push NULL onto the stack.Then push the addresses of the nodes onto the stack as the list istraversed from head to tail.Stop with all but the final NULL pointer on the stack.Now the reverse iterator is initialize.

To increment the reverse list iteratorPop an address off the stack.Assign it to the node pointer.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 15 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 16 / 35

Binary Tree Iterators

In a list, we could traverse in only two ways:Head to tail (forward)Tail to head (reverse)

In a binary tree, there is a variety of ways in which we can traversethe structure.

Pre-orderIn-orderPost-orderLevel-order, etc.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 17 / 35

Binary Tree Iterators

Accordingly, we create the following binary tree iterator classes.PreorderIterator classInorderIterator classPostorderIterator classLevelorderIterator class

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 18 / 35

Binary Tree Iterators

Furthermore, these are all subclasses of a base class Iterator.By using inheritance, all we have to implement for each subclassis

The constructor.The ++ operator.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 19 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 20 / 35

Binary Tree Preorder Iterators

We will build on what we learned about reverse iterators for lists.To reach the “next” node from a root node, a preorder iterator mustmove to the root of the left subtree.It should also push the pointer to the right subtree, if there is one.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 21 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

The binary tree

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Visit root node, push null pointer

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p2

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Push pointer to right subtree, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p4

p2

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Push pointer to right subtree, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p2

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p6

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Push pointer to right subtree, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p8

p6

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Push pointer to right subtree, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p6

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

NULL

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Binary Tree Preorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop null pointer, quit

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 22 / 35

Binary Tree Preorder Iterators

Preorder Iterator ConstructorPreorderIterator(const BinaryTree<T>* bt,

BinaryTreeNode<T>* rt) : Iterator(bt, rt){

stack.push(NULL);return;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 23 / 35

Binary Tree Preorder Iterators

Preorder Iterator operator++()PreorderIterator& operator++(){

if (node != NULL){

if (node->rightNode() != NULL)stack.push(node->rightNode());

if (node->leftNode() != NULL)node = node->leftNode();

elsenode = stack.pop();

}return *this;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 24 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 25 / 35

Binary Tree Inorder Iterators

To reach the “next” node from a root node, an inorder iterator musttravel to the right subtree, and then as far left as possible, pushingnode pointers along the way.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 26 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

The binary tree

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Start at root node, push null pointer

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p0

NULL

p1

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p0

p1

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Move right

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p2

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p2

p5

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p2

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p2

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Binary Tree Inorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

Pop null pointer, quit

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 27 / 35

Binary Tree Inorder Iterators

Inorder Iterator ConstructorInorderIterator(const BinaryTree<T>* bt,

BinaryTreeNode<T>* rt) : Iterator(bt, rt){

stack.push(NULL);if (node != NULL)

while (node->leftNode() != NULL){

stack.push(node);node = node->leftNode();

}return;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 28 / 35

Binary Tree Inorder Iterators

Inorder Iterator operator++()InorderIterator& operator++(){

if (node != NULL){

if (node->rightNode() != NULL){

node = node->rightNode();while (node->leftNode() != NULL){

stack.push(node);node = node->leftNode();

}}else

node = stack.pop();}return *this;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 29 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 30 / 35

Binary Tree Postorder Iterators

To the “next” node from a root node, a postorder iterator must dothe following.If the current node is a left child, then the iterator must followingthe leftmost branch of the sibling right subtree all the way to a leafnode, pushing nodes along the way.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 31 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9

The binary tree

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Start at root node, push null pointer

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p1

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p3

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

p0

p1

Push pointer to node, move right

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p3

p0

p1

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p3

p0

p1

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p1

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Move to right child of parent, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

p0

Move to right child of parent

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

NULL

p0

p2

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9

Push pointer to node, move left

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

NULL

p0

p2

p5

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9

Push pointer to node, move left, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p2

p5

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Move to right child of parent, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p2

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Pop pointer, visit that node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p2

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Move to right child of parent, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p0

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9 NULL

Pop pointer, visit node

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Binary Tree Postorder Iterators

p1

p5 p6

p8p7

p4p3

p2

Stack

p0

p9

Pop null pointer, quit

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 32 / 35

Binary Tree Postorder Iterators

Postorder Iterator ConstructorPostorderIterator(const BinaryTree<T>* bt,

BinaryTreeNode<T>* rt) : Iterator(bt, rt){

stack.push(NULL);if (node != NULL)

while (node->leftNode() != NULL|| node->rightNode() != NULL){

stack.push(node);if (node->leftNode() != NULL)

node = node->leftNode();else

node = node->rightNode();}

return;}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 33 / 35

Binary Tree Postorder Iterators

Postorder Iterator operator++()PostorderIterator& operator++(){

if (node != NULL){

BinaryTreeNode<T>* parent = stack.top();if (parent != NULL && node == parent->leftNode()&& parent->rightNode() != NULL){

node = parent->rightNode();while (node->leftNode() != NULL|| node->rightNode() != NULL){

stack.push(node);if (node->leftNode() != NULL)

node = node->leftNode();else

node = node->rightNode();}

}else{

stack.pop();node = parent;

}}return *this;

}

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 34 / 35

Outline

1 LinkedList Iterators

2 List Traversals

3 Reverse Iterators

4 Binary Tree IteratorsPreorder IteratorsInorder IteratorsPostorder Iterators

5 Assignment

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 35 / 35

Assignment

AssignmentRead Section 14.6.Create a LevelorderIterator class for binary trees.

Robb T. Koether (Hampden-Sydney College) List and Binary Tree Iterator Implementation Fri, Apr 26, 2013 36 / 35

Recommended