Trees 2: Dynamic Binary Tree Navigation and Iteration

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Advertisements

Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Trees ---- Soujanya.
Planning & System installation
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
slides created by Alyssa Harding
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
CMSC 341 Binary Search Trees 11/19/2018.
Abstract Data Structures
CMSC 341 Binary Search Trees.
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Trees Definitions Implementation Traversals K-ary Trees
CMSC 341 Binary Search Trees 1/3/2019.
Binary Tree Traversals
CS-240 Dick Steflik D.J. Foreman
CMSC 341 Binary Search Trees 2/23/2019.
Stacks with Dynamic Memory
CMSC 341 Binary Search Trees.
Chapter 20: Binary Trees.
CMSC 341 Binary Search Trees 5/8/2019.
CMSC 341 Binary Search Trees 5/28/2019.
Binary Tree Traversal.
CMSC 341 Binary Search Trees 2/21/2006.
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Trees 2: Dynamic Binary Tree Navigation and Iteration Andy Wang Data Structures, Algorithms, and Generic Programming

Trees 2: Overview Dynamic memory implementation of binary trees Binary tree navigators Binary tree iterators

Dynamic Memory Implementation of Binary Trees Similar to dynamic memory implementation of List Use a structural class TNode TNode has three pointers parent lchild rchild

Dynamic Memory Implementation of Binary Trees TNode has one data field value The three pointer fields allow navigation through the tree up down-left down-right

Non-Object-Based Approach: Operate Directly on Public TNode<T> template <class T> class TNode { public: T value; TNode *parent; *lchild; *rchild TNode(const T& t) : value(t), parent(0), lchild(0), rchild(0) { } };

Non-Object-Based Recursive Traversal Algorithms for TNode<T> void inorder(TNode<T>* root, void (TNode<T>*) visit) { if (root == 0) { return; } inorder(root->lchild, visit); visit(root); inorder(root->rchild, visit); void preorder(TNode<T>* root, void (TNode<T>*) visit) {

Non-Object-Based Recursive Traversal Algorithms for TNode<T> void postorder(TNode<T>* root, void (TNode<T>*) visit) { if (root == 0) { return; } inorder(root->lchild, visit); inorder(root->rchild, visit); visit(root);

An Object-Based Approach Generic pContainer TBinaryTree<T> Restrict access to data Make external algorithms object-based Model: TList<T> Define iterators Allow stopping in mid-traversal Allow multiple iterators for a single container

An Object-Based Approach Define navigators Allow navigation within a tree structure Use to implement tree iterator methods and other algorithms When a recursive method implementation is appropriate, make the method protected

Object-Based Implementation template <typename T> class TBinaryTree { class TNode { T value; TNode *parent, *lchild; *rchild; TNode(const T& t); friend class TBinaryTree<T>; friend class TBinaryTreeNavigator<T>; }; protected: TNode *root;

Object-Based Implementation public: typedef T value_type; typedef TBinaryTreeNavigator<T> Navigator; typedef TBinaryTreeInorderIterator<T> Iterator; .. other iterator types as needed };

Binary Tree Navigators Analogous to list iterators Use tree structure to navigate N.initialize(B) sets N at the root of B ++N moves N down to its left childe N++ moves N down to its right child --N or N-- moves N up to its parent Navigator used to implement tree iterator types

Binary Tree Navigators Not the same as the tree Iterators! Navigators: physical traversals

Binary Tree Navigators Not the same as the tree Iterators! Iterators: logical traversals Navigator used to implement tree iterator types

Defining class TBinaryTreeNavigator template <typename T> class TBinaryTreeNavigator { private: TBinaryTree<T>::TNode *currNode; public: T& Retrieve() const; int Valid() const; int HasParent() const; int HasLeftChild() const; int HasRightChild() const; int IsLeftChild() const; int IsRightChild() const; // various operators: ==, !=, *, =, ++, --, };

Implementing class TBinaryTreeNavigator<T> template <typename T> int TBinaryTreeNavigator<T>::HasParent() const { return (currNode != 0 && currNode->parent != 0); } int TBinaryTreeNavigator<T>::HasLeftChild() const { return (currNode != 0 && currNode->lchild != 0); int TBinaryTreeNavigator<T>::IsLeftChild() const { return (currNode != 0 && currNode->parent != 0 && currNode == currNode->parent->lchild);

Implementing class TBinaryTreeNavigator<T> template <typename T> int TBinaryTreeNavigator<T>::Valid() const { return (currNode != 0); } T& TBinaryTreeNavigator<T>::Retrieve() const { if (currNode == 0) { // error } return (currNode->value); TBinaryTreeNavigator<T>& TBinaryTreeNavigator<T>::operator ++() { if (currNode != 0) { currNode = currNode->lchild; } return *this;

Implementing class TBinaryTreeNavigator<T> template <typename T> TBinaryTreeNavigator<T>& TBinaryTreeNavigator<T>::operator ++(int) { if (currNode != 0) { currNode = currNode->rchild; } return *this; } TBinaryTreeNavigator<T>& TBinaryTreeNavigator<T>::operator --() { if (currNode != 0) { currNode = currNode->parent; }

Inorder Iterator template <typename T> class TBinaryTreeInorderIterator { private: TBinaryTreeNavigator<T> N; public: typedef T value_type; TBinaryTreeInorderIterator(); ~TBinaryTreeInorderIterator(); TBinaryTreeInorderIterator(const TBinaryTreeInorderIterator& I); TBinaryTreeInorderIterator(const TBinaryTree<T>& B); TBinaryTreeInorderIterator(const TBinaryTreeNavigator<T> &N); void Initialize(const TBinaryTree<T>& T); void rInitialize(const TBinaryTree<T>& T);

Inorder Iterator T& Retrieve() const; int Valid() const; int operator==(const TBinaryTreeInorderIterator& I2) const; int operator!=(const TBinaryTreeInorderIterator& I2) const; T& operator*() const; TBinaryTreeInorderIterator<T>& operator=(const TBinaryTreeInorderIterator& I); TBinaryTreeInorderIterator<T>& operator++(); TBinaryTreeInorderIterator<T> operator++(int); TBinaryTreeInorderIterator<T>& operator--(); TBinaryTreeInorderIterator<T> operator--(int); };

Inorder Iterator Initialization template <typename T> void TBinaryTreeInorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); // slide to leftmost node while (N.HasLeftChild()) { ++N; } N 1 2 3 4 5 7 6 root

Inorder Iterator Initialization template <typename T> void TBinaryTreeInorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); // slide to leftmost node while (N.HasLeftChild()) { ++N; } 1 2 3 4 5 7 6 root N

Inorder Iterator Initialization template <typename T> void TBinaryTreeInorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); // slide to leftmost node while (N.HasLeftChild()) { ++N; } 1 2 3 4 5 7 6 root N

Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root N Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root N Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root N Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; } N

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root Inorder Iterator Incrementation N template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Inorder Iterator Incrementation 1 2 3 4 5 7 6 root N Inorder Iterator Incrementation template <typename T> TBinaryTreeInorderIterator<T>& TBinaryTreeInorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } if (N.HashRightChild()) { // slide down the left subtree of the right // child N++; while (N.HasLeftChild()) { ++N; } } else { // back up to the first ancestor not already visited int NwasRightChild; do { // back up once if in a left branch; all the way, if in a // right branch NwasRightChild = N.IsRightChild(); --N; } while (NwasRightChild); return *this; }

Postorder Iterator template <typename T> class TBinaryTreePostorderIterator { private: TBinaryTreeNavigator<T> N; public: typedef T value_type; TBinaryTreePostorderIterator(); ~TBinaryTreePostorderIterator(); TBinaryTreePostorderIterator(const TBinaryTreePostorderIterator& I); TBinaryTreePostorderIterator(const TBinaryTree<T>& B); TBinaryTreePostorderIterator(const TBinaryTreeNavigator<T> &N); void Initialize(const TBinaryTree<T>& T); void rInitialize(const TBinaryTree<T>& T);

Postorder Iterator T& Retrieve() const; int Valid() const; int operator==(const TBinaryTreePostorderIterator& I2) const; int operator!=(const TBinaryTreePostorderIterator& I2) const; T& operator*() const; TBinaryTreePostorderIterator<T>& operator=(const TBinaryTreePostorderIterator& I); TBinaryTreePostorderIterator<T>& operator++(); TBinaryTreePostorderIterator<T> operator++(int); TBinaryTreePostorderIterator<T>& operator--(); TBinaryTreePostorderIterator<T> operator--(int); };

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; }

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } N root 1 3 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 N 3 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 N 3 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 3 N 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 3 N 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } 1 2 3 4 5 7 6 root N

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 N 2 3 4 5 6 7

Postorder Iterator Initialization template <typename T> void TBinaryTreePostorderIterator<T>::Initialize(const TBinaryTree<T>& B) { // enter tree at root N = B.Root(); int finished = !N.Valid(); while (!finished) { if (N.HasLeftChild()) { ++N; } else if (N.HasRightChild()) { N++; } else { finished = 1; } root 1 2 3 N 4 5 6 7

Postorder Iterator Incrementation template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation 1 2 3 4 5 7 6 root Postorder Iterator Incrementation N template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation 1 2 3 4 5 7 6 root Postorder Iterator Incrementation N template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation 1 2 3 4 5 7 6 root Postorder Iterator Incrementation N template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation 1 2 3 4 5 7 6 root Postorder Iterator Incrementation template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 N Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 N Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this; N

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 Postorder Iterator Incrementation 2 3 N 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 N Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Postorder Iterator Incrementation root 1 N Postorder Iterator Incrementation 2 3 4 5 6 7 template <typename T> TBinaryTreePostorderIterator<T>& TBinaryTreePostorderIterator<T>::operator ++() { if (!N.Valid()) { return *this; } int NwasLeftChild = N.IsLeftChild(); --N; if (NwasLeftChild && N.HasRightChild()) { N++; // go to the left most node of the left branch, or the left most // node of the right branch. } return *this;

Preorder Iterator template <typename T> class TBinaryTreePreorderIterator { private: CStack<fsu::TBinaryTree<T>::Navigator, TDeque<TBinaryTree<T>::Navigator> Stk; TBinaryTreePreorderIterator(const TBinaryTreePreorderIterator& I); void rInitialize(const TBinaryTree<T>& T); TBinaryTreePreorderIterator<T>& operator=(const TBinaryTreePreorderIterator& I); TBinaryTreePreorderIterator<T>& operator++(int); TBinaryTreePreorderIterator<T> operator--(); TBinaryTreePreorderIterator<T>& operator--(int);

Preorder Iterator public: typedef T value_type; TBinaryTreePreorderIterator(); TBinaryTreePreorderIterator(const TBinaryTree<T>& L); virtual ~TBinaryTreePreorderIterator(); void Initialize(const TBinaryTree<T>& T); T& Retrieve() const; int Valid() const; int operator==(const TBinaryTreePreorderIterator& I2); int operator!=(const TBinaryTreePreorderIterator& I2); T& operator*() const; TBinaryTreePreorderIterator<T>& operator++(); };

Preorder Iteartor Initialization 1 2 3 4 5 7 6 root Preorder Iteartor Initialization template <typename T> void TBinaryTreePreorderIterator<T>::Initialize(const TBinaryTree<T>& B) { Stk.Clear(); if (!B.Empty()) { Stk.Push(B.Root()); } T& TBinaryTreePreorderIterator<T>::operator*() const { if (!Stk.Empty()) { return *(Stk.Top()); 1

Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; }

Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; }

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 4 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 4 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 4 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 2 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 5 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 5 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 5 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 1

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; }

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 6 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation template <typename T> TBinaryTreePreorderIterator<T>& TBinaryTreePreorderIterator<T>::operator++() { TBinaryTree<T>::Navigator child; if (!Stk.Empty()) { // push left child of top if possible if (Stk.Top().HasLeftChild()) { Stk.Push(++Stk.Top()); return *this; } 6 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 6 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 3

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; }

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 7

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 7

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; } 7

Preorder Iteartor Incrementation 1 2 3 4 5 7 6 root Preorder Iteartor Incrementation // otherwise pop until a right child is found // and push that child onto stack TBinaryTree<T>::Navigator current; while (!Stk.Empty()) { current = Stk.Top(); Stk.Pop(); if (current.HasRightChild()) { Stk.Push(current++); return *this; }

Levelorder Iterator template <typename T> class TBinaryTreeLevelorderIterator { private: CQueue<fsu::TBinaryTree<T>::Navigator, TDeque<TBinaryTree<T>::Navigator> Que; TBinaryTreeLevelorderIterator(const TBinaryTreePreorderIterator& I); void rInitialize(const TBinaryTree<T>& T); TBinaryTreeLevelorderIterator<T>& operator=(const TBinaryTreeLevelorderIterator& I); TBinaryTreeLevelorderIterator<T>& operator++(int); TBinaryTreeLevelorderIterator<T> operator--(); TBinaryTreeLevelorderIterator<T>& operator--(int);

Levelorder Iterator public: typedef T value_type; TBinaryTreeLevelorderIterator(); TBinaryTreeLevelorderIterator(const TBinaryTree<T>& L); virtual ~TBinaryTreeLevelorderIterator(); void Initialize(const TBinaryTree<T>& T); T& Retrieve() const; int Valid() const; int operator==(const TBinaryTreeLevelorderIterator& I2); int operator!=(const TBinaryTreeLevelorderIterator& I2); T& operator*() const; TBinaryTreeLevelorderIterator<T>& operator++(); };

Levelorder Iteartor Initialization 1 2 3 4 5 7 6 root Levelorder Iteartor Initialization template <typename T> void TBinaryTreeLevelorderIterator<T>::Initialize(const TBinaryTree<T>& B) { Que.Clear(); if (!B.Empty()) { Que.Push(B.Root()); } T& TBinaryTreeLevelorderIterator<T>::operator*() const { if (!Que.Empty()) { return *(Que.Front()); 1

Levelorder Iteartor Incrementation template <typename T> TBinaryTreeLevelorderIterator<T>& TBinaryTreeLevelorderIterator<T>::operator++() { if (!Que.Empty()) { if (Que.Front().HasLeftChild()) { Que.Push(++Que.Front()); } if (Que.Front().HasRightChild()) { Que.Push(Que.Front()++); Que.Pop(); return *this;