Title: Trees 2: Dynamic Binary Tree Navigation and Iteration
1Trees 2 Dynamic Binary Tree Navigation and
Iteration
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Trees 2 Overview
- Dynamic memory implementation of binary trees
- Binary tree navigators
- Binary tree iterators
3Dynamic Memory Implementation of Binary Trees
- Similar to dynamic memory implementation of List
- Use a structural class TNode
- TNode has three pointers
- parent
- lchild
- rchild
4Dynamic 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
5Non-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)
6Non-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)
-
7Non-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)
-
8An 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
9An 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
10Object-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
-
11Object-Based Implementation
- public
- typedef T value_type
- typedef TBinaryTreeNavigatorltTgt Navigator
- typedef TBinaryTreeInorderIteratorltTgt Iterator
- .. other iterator types as needed
12Binary 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
13Binary Tree Navigators
- Not the same as the tree Iterators!
- Navigators physical traversals
14Binary Tree Navigators
- Not the same as the tree Iterators!
- Iterators logical traversals
- Navigator used to implement tree iterator types
15Defining 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 , !, , , , --,
16Implementing 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)
-
17Implementing 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
18Implementing 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
-
19Inorder 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)
20Inorder 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)
21Inorder 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
-
22Inorder 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
-
23Inorder 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
-
24Inorder 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
-
25Inorder 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
-
26Inorder 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
-
27Inorder 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
-
28Inorder 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
-
29Inorder 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
30Inorder 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
-
31Inorder 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
32Inorder 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
-
33Inorder 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
-
34Inorder 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
-
35Inorder 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
-
36Inorder 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
-
37Inorder 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
-
38Inorder 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
-
39Inorder 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
-
40Inorder 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
-
41Inorder 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
-
42Inorder 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
-
43Inorder 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
-
44Inorder 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
-
45Inorder 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
-
46Inorder 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
-
47Inorder 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
-
48Inorder 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
-
49Inorder 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
-
50Postorder 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)
51Postorder 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)
52Postorder 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
-
-
53Postorder 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
54Postorder 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
55Postorder 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
56Postorder 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
57Postorder 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
58Postorder 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
-
-
59Postorder 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
60Postorder 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
61Postorder 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
62Postorder 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
63Postorder 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
64Postorder 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
65Postorder 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
66Postorder 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
67Postorder 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
68Postorder 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
69Postorder 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
70Postorder 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
71Postorder 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
72Postorder 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
73Postorder 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
74Postorder 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
75Postorder 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
76Postorder 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
77Postorder 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
78Postorder 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
79Postorder 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
80Postorder 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
81Postorder 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
82Postorder 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
83Postorder 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
84Postorder 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
85Postorder 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
86Postorder 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
87Preorder 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) -
88Preorder 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()
89Preorder 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
90Preorder 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
-
91Preorder 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
92Preorder 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
93Preorder 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
94Preorder 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
95Preorder 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
96Preorder 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
97Preorder 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
98Preorder 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
99Preorder 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
100Preorder 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
101Preorder 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
102Preorder 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
103Preorder 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
104Preorder 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
105Preorder 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
106Preorder 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
107Preorder 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
108Preorder 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
109Preorder 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
110Preorder 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
111Preorder 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
112Preorder Iteartor Incrementation
- template lttypename Tgt
- TBinaryTreePreorderIteratorltTgt
- TBinaryTreePreorderIteratorltTgtoperator()