List and Binary Tree Iterator...

78
List and Binary Tree Iterator Implementation Lecture 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

Transcript of List and Binary Tree Iterator...

Page 1: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 2: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 3: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 4: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 5: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 6: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 7: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 8: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 9: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 10: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 11: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 12: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 13: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 14: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 15: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 16: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 17: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 18: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 19: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 20: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 21: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 22: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 23: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 24: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 25: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 26: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 27: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 28: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 29: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 30: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 31: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 32: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 33: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 34: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 35: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 36: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 37: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 38: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 39: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 40: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 41: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 42: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 43: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 44: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 45: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 46: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 47: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 48: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 49: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 50: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 51: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 52: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 53: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 54: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 55: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 56: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 57: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 58: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 59: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 60: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 61: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 62: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 63: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 64: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 65: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 66: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 67: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 68: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 69: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 70: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 71: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 72: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 73: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 74: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 75: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 76: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 77: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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

Page 78: List and Binary Tree Iterator Implementationpeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures...List and Binary Tree Iterator Implementation Lecture 35 Section 14.6 Robb T. Koether

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