Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees 2: Dynamic Binary Tree Navigation and Iteration

Similar presentations


Presentation on theme: "Trees 2: Dynamic Binary Tree Navigation and Iteration"— Presentation transcript:

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

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

3 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

4 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

5 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) { } };

6 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) {

7 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);

8 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

9 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

10 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;

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

12 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

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

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

15 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: ==, !=, *, =, ++, --, };

16 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);

17 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;

18 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; }

19 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);

20 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); };

21 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

22 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

23 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

24 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; }

25 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; }

26 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; }

27 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; }

28 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; }

29 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

30 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

31 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

32 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; }

33 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; }

34 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; }

35 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; }

36 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; }

37 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; }

38 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

39 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

40 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

41 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; }

42 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; }

43 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; }

44 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

45 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

46 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

47 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; }

48 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; }

49 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; }

50 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);

51 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); };

52 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; }

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 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

61 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;

62 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;

63 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;

64 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;

65 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

66 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

67 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

68 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;

69 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;

70 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;

71 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;

72 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;

73 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;

74 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

75 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

76 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

77 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;

78 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;

79 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

80 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

81 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

82 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;

83 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;

84 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;

85 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;

86 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;

87 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);

88 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++(); };

89 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

90 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; }

91 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; }

92 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

93 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

94 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

95 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

96 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

97 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

98 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

99 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

100 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

101 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

102 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

103 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

104 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

105 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

106 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

107 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; }

108 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

109 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

110 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

111 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

112 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

113 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

114 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

115 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

116 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; }

117 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

118 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

119 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

120 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; }

121 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);

122 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++(); };

123 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

124 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;


Download ppt "Trees 2: Dynamic Binary Tree Navigation and Iteration"

Similar presentations


Ads by Google