Trees 2: Dynamic Binary Tree Navigation and Iteration - PowerPoint PPT Presentation

About This Presentation
Title:

Trees 2: Dynamic Binary Tree Navigation and Iteration

Description:

Dynamic Memory Implementation of Binary Trees. Similar to ... down-right. Non-Object-Based Approach: Operate Directly on Public TNode T template class T ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 125
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Trees 2: Dynamic Binary Tree Navigation and Iteration


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 TNodeltTgt
  • template ltclass Tgt
  • 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 TNodeltTgt
  • void inorder(TNodeltTgt root, void (TNodeltTgt)
    visit)
  • if (root 0)
  • return
  • inorder(root-gtlchild, visit)
  • visit(root)
  • inorder(root-gtrchild, visit)
  • void preorder(TNodeltTgt root, void (TNodeltTgt)
    visit)
  • if (root 0)
  • return
  • visit(root)
  • inorder(root-gtlchild, visit)
  • inorder(root-gtrchild, visit)

7
Non-Object-Based Recursive Traversal Algorithms
for TNodeltTgt
  • void postorder(TNodeltTgt root, void (TNodeltTgt)
    visit)
  • if (root 0)
  • return
  • inorder(root-gtlchild, visit)
  • inorder(root-gtrchild, visit)
  • visit(root)

8
An Object-Based Approach
  • Generic pContainer TBinaryTreeltTgt
  • Restrict access to data
  • Make external algorithms object-based
  • Model TListltTgt
  • 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 lttypename Tgt
  • class TBinaryTree
  • class TNode
  • T value
  • TNode parent,
  • lchild
  • rchild
  • TNode(const T t)
  • friend class TBinaryTreeltTgt
  • friend class TBinaryTreeNavigatorltTgt
  • protected
  • TNode root

11
Object-Based Implementation
  • public
  • typedef T value_type
  • typedef TBinaryTreeNavigatorltTgt Navigator
  • typedef TBinaryTreeInorderIteratorltTgt 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 lttypename Tgt
  • class TBinaryTreeNavigator
  • private
  • TBinaryTreeltTgtTNode 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 TBinaryTreeNavigatorltTgt
  • template lttypename Tgt
  • int TBinaryTreeNavigatorltTgtHasParent() const
    return (currNode ! 0 currNode-gtparent !
    0)
  • template lttypename Tgt
  • int TBinaryTreeNavigatorltTgtHasLeftChild() const
    return (currNode ! 0 currNode-gtlchild !
    0)
  • template lttypename Tgt
  • int TBinaryTreeNavigatorltTgtIsLeftChild() const
  • return (currNode ! 0 currNode-gtparent ! 0
  • currNode currNode-gtparent-gtlchild)

17
Implementing class TBinaryTreeNavigatorltTgt
  • template lttypename Tgt
  • int TBinaryTreeNavigatorltTgtValid() const
  • return (currNode ! 0)
  • template lttypename Tgt
  • T TBinaryTreeNavigatorltTgtRetrieve() const
  • if (currNode 0) // error
  • return (currNode-gtvalue)
  • template lttypename Tgt
  • TBinaryTreeNavigatorltTgt TBinaryTreeNavigatorltTgt
    operator ()
  • if (currNode ! 0) currNode
    currNode-gtlchild
  • return this

18
Implementing class TBinaryTreeNavigatorltTgt
  • template lttypename Tgt
  • TBinaryTreeNavigatorltTgt TBinaryTreeNavigatorltTgt
    operator (int)
  • if (currNode ! 0) currNode
    currNode-gtrchild
  • return this
  • template lttypename Tgt
  • TBinaryTreeNavigatorltTgt TBinaryTreeNavigatorltTgt
    operator --()
  • if (currNode ! 0) currNode
    currNode-gtparent
  • return this

19
Inorder Iterator
  • template lttypename Tgt
  • class TBinaryTreeInorderIterator
  • private
  • TBinaryTreeNavigatorltTgt N
  • public
  • typedef T value_type
  • TBinaryTreeInorderIterator()
  • TBinaryTreeInorderIterator()
  • TBinaryTreeInorderIterator(const
    TBinaryTreeInorderIterator I)
  • TBinaryTreeInorderIterator(const
    TBinaryTreeltTgt B)
  • TBinaryTreeInorderIterator(const
    TBinaryTreeNavigatorltTgt N)
  • void Initialize(const TBinaryTreeltTgt T)
  • void rInitialize(const TBinaryTreeltTgt T)

20
Inorder Iterator
  • T Retrieve() const
  • int Valid() const
  • T Retrieve() const
  • int Valid() const
  • int operator(const TBinaryTreeInorderIterator
    I2) const
  • int operator!(const TBinaryTreeInorderIterator
    I2) const
  • T operator() const
  • TBinaryTreeInorderIteratorltTgt operator(const
    TBinaryTreeInorderIterator I)
  • TBinaryTreeInorderIteratorltTgt operator()
  • TBinaryTreeInorderIteratorltTgt operator(int)
  • TBinaryTreeInorderIteratorltTgt operator--()
  • TBinaryTreeInorderIteratorltTgt operator--(int)

21
Inorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreeInorderIteratorltTgtInitialize(con
    st TBinaryTreeltTgt B)
  • // enter tree at root
  • N B.Root()
  • // slide to leftmost node
  • while (N.HasLeftChild())
  • N

22
Inorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreeInorderIteratorltTgtInitialize(con
    st TBinaryTreeltTgt B)
  • // enter tree at root
  • N B.Root()
  • // slide to leftmost node
  • while (N.HasLeftChild())
  • N

23
Inorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreeInorderIteratorltTgtInitialize(con
    st TBinaryTreeltTgt B)
  • // enter tree at root
  • N B.Root()
  • // slide to leftmost node
  • while (N.HasLeftChild())
  • N

24
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

31
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

39
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

40
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

41
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

45
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

46
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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

47
Inorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreeInorderIteratorltTgt
  • TBinaryTreeInorderIteratorltTgtoperator ()
  • 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 lttypename Tgt
  • class TBinaryTreePostorderIterator
  • private
  • TBinaryTreeNavigatorltTgt N
  • public
  • typedef T value_type
  • TBinaryTreePostorderIterator()
  • TBinaryTreePostorderIterator()
  • TBinaryTreePostorderIterator(const
    TBinaryTreePostorderIterator I)
  • TBinaryTreePostorderIterator(const
    TBinaryTreeltTgt B)
  • TBinaryTreePostorderIterator(const
    TBinaryTreeNavigatorltTgt N)
  • void Initialize(const TBinaryTreeltTgt T)
  • void rInitialize(const TBinaryTreeltTgt T)

51
Postorder Iterator
  • T Retrieve() const
  • int Valid() const
  • T Retrieve() const
  • int Valid() const
  • int operator(const TBinaryTreePostorderIterato
    r I2) const
  • int operator!(const TBinaryTreePostorderIterato
    r I2) const
  • T operator() const
  • TBinaryTreePostorderIteratorltTgt
    operator(const TBinaryTreePostorderIterator
    I)
  • TBinaryTreePostorderIteratorltTgt operator()
  • TBinaryTreePostorderIteratorltTgt
    operator(int)
  • TBinaryTreePostorderIteratorltTgt operator--()
  • TBinaryTreePostorderIteratorltTgt
    operator--(int)

52
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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 lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
3
7
6
54
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
3
7
6
55
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
3
7
6
56
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
3
7
6
57
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
3
7
6
58
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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

59
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
2
3
4
5
7
6
60
Postorder Iterator Initialization
  • template lttypename Tgt
  • void TBinaryTreePostorderIteratorltTgtInitialize(c
    onst TBinaryTreeltTgt 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
root
2
3
4
5
7
6
61
Postorder Iterator Incrementation
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

66
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

67
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

68
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

75
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

76
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

77
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

80
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

81
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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

82
Postorder Iterator Incrementation
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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
1
root
2
3
4
5
7
6
  • template lttypename Tgt
  • TBinaryTreePostorderIteratorltTgt
  • TBinaryTreePostorderIteratorltTgtoperator ()
  • 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 lttypename Tgt
  • class TBinaryTreePreorderIterator
  • private
  • CStackltfsuTBinaryTreeltTgtNavigator,
  • TDequeltTBinaryTreeltTgtNavigatorgt Stk
  • TBinaryTreePreorderIterator(const
    TBinaryTreePreorderIterator I)
  • void rInitialize(const TBinaryTreeltTgt T)
  • TBinaryTreePreorderIteratorltTgt operator(const
  • TBinaryTreePreorderIterator I)
  • TBinaryTreePreorderIteratorltTgt
    operator(int)
  • TBinaryTreePreorderIteratorltTgt operator--()
  • TBinaryTreePreorderIteratorltTgt
    operator--(int)

88
Preorder Iterator
  • public
  • typedef T value_type
  • TBinaryTreePreorderIterator()
  • TBinaryTreePreorderIterator(const
    TBinaryTreeltTgt L)
  • virtual TBinaryTreePreorderIterator()
  • void Initialize(const TBinaryTreeltTgt T)
  • T Retrieve() const
  • int Valid() const
  • int operator(const TBinaryTreePreorderIterator
    I2)
  • int operator!(const TBinaryTreePreorderIterator
    I2)
  • T operator() const
  • TBinaryTreePreorderIteratorltTgt operator()

89
Preorder Iteartor Initialization
  • template lttypename Tgt
  • void TBinaryTreePreorderIteratorltTgtInitialize(co
    nst TBinaryTreeltTgt B)
  • Stk.Clear()
  • if (!B.Empty())
  • Stk.Push(B.Root())
  • template lttypename Tgt
  • T TBinaryTreePreorderIteratorltTgtoperator()
    const
  • if (!Stk.Empty())
  • return (Stk.Top())







1
90
Preorder Iteartor Incrementation
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this

92
Preorder Iteartor Incrementation
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this





4
2
1
99
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this






2
1
100
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this






2
1
101
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this







1
102
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this






5
1
103
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this






5
1
104
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this






5
1
105
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this







1
106
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this







1
107
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this








108
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this







3
109
Preorder Iteartor Incrementation
  • // otherwise pop until a right child is found
  • // and push that child onto stack
  • TBinaryTreeltTgtNavigator current
  • while (!Stk.Empty())
  • current Stk.Top()
  • Stk.Pop()
  • if (current.HasRightChild())
  • Stk.Push(current)
  • return this
  • return this







3
110
Preorder Iteartor Incrementation
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
  • TBinaryTreeltTgtNavigator 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
  • template lttypename Tgt
  • TBinaryTreePreorderIteratorltTgt
  • TBinaryTreePreorderIteratorltTgtoperator()
Write a Comment
User Comments (0)
About PowerShow.com