Preface :
We have observed the power of iterators to scan the elements in a LinkedList or ArrayList collection. Traversing the nodes in a binary tree is more difficult because a tree is a nonlinear structure and there is no one traversal order. Section 16.3 discuss recursive algorithms for performing a preorder, inorder and postorder scan in a tree. The problem with each of these traversal algorithms is that there is no escape from the recursive process util it completes. We cannot easily stop the scan, examine the contents of a node, and then continue the scan at another node in the tree. We need an iterative process to implement a binary tree iterator. Once choice would be a level-order scan using a queue. As we will discover with binary search trees, an iterator version of a recursive scan is a better choice.
Iterative Tree Traversal :
In this section, we will implement an iterator using an iterative inorder scan. Creating iterators with a preorder and a postorder iterative scan can be your practices. You are familiar with iterators and Iterator interface from our discussion of the LinkedList class. Our binary tree iterator implements the interface.To provide an iterator scan of the elements, we use a stack to hold the nodes that have been passed but not visited. In this way, we can simulate the running time system, which uses a stack to hold the recursive calls that are made during an inorder interator scan of the tree. Because it is our stack, we can halt at any time, access a node, and then continue by popping the stack to pick up the scan. Below is the implementation of class InorderIterator :
Inorder Iterative Traversal :
The inorder iterative traversal emulates a recursive scan. Use of a stack is a key feature. Nodes enter the stack when we move down the tree from the current iterator (node) position to the node that references the "next" iterator position. In this way, the iterative algorithm can "remember" each intermediate node on the path so it can come back up the tree and visit the node at a later point. To do this, push on a stack the references to each of the nodes that are discovered on the path to the "next" element.
An iterative scan begins at the leftmost node in the tree (inorder scan). The starting point is found by starting at the root and following the chain of left children until we locate a node with an empty left subtree. An iterator initially references this node. The root and all intermediate nodes on the path of left children are pushed on the stack. The iterative traversal of the tree is based on the following set of rules :
Let us trace the iterative inorder traversal of nodes in the following character tree. The order of visits to the nodes is BFDAEC. We organize the trace around the order in which nodes are scanned. In this way, you can understand how the algorithm uses the stack and the traversal rules to proceed from a "current" scan position to the "next" scan position :
- Scan 'B' and then 'F'
- Scan 'D' and then 'A'
- Scan 'E' then 'C'
We have observed the power of iterators to scan the elements in a LinkedList or ArrayList collection. Traversing the nodes in a binary tree is more difficult because a tree is a nonlinear structure and there is no one traversal order. Section 16.3 discuss recursive algorithms for performing a preorder, inorder and postorder scan in a tree. The problem with each of these traversal algorithms is that there is no escape from the recursive process util it completes. We cannot easily stop the scan, examine the contents of a node, and then continue the scan at another node in the tree. We need an iterative process to implement a binary tree iterator. Once choice would be a level-order scan using a queue. As we will discover with binary search trees, an iterator version of a recursive scan is a better choice.
Iterative Tree Traversal :
In this section, we will implement an iterator using an iterative inorder scan. Creating iterators with a preorder and a postorder iterative scan can be your practices. You are familiar with iterators and Iterator interface from our discussion of the LinkedList class. Our binary tree iterator implements the interface.To provide an iterator scan of the elements, we use a stack to hold the nodes that have been passed but not visited. In this way, we can simulate the running time system, which uses a stack to hold the recursive calls that are made during an inorder interator scan of the tree. Because it is our stack, we can halt at any time, access a node, and then continue by popping the stack to pick up the scan. Below is the implementation of class InorderIterator :
Inorder Iterative Traversal :
The inorder iterative traversal emulates a recursive scan. Use of a stack is a key feature. Nodes enter the stack when we move down the tree from the current iterator (node) position to the node that references the "next" iterator position. In this way, the iterative algorithm can "remember" each intermediate node on the path so it can come back up the tree and visit the node at a later point. To do this, push on a stack the references to each of the nodes that are discovered on the path to the "next" element.
An iterative scan begins at the leftmost node in the tree (inorder scan). The starting point is found by starting at the root and following the chain of left children until we locate a node with an empty left subtree. An iterator initially references this node. The root and all intermediate nodes on the path of left children are pushed on the stack. The iterative traversal of the tree is based on the following set of rules :
Let us trace the iterative inorder traversal of nodes in the following character tree. The order of visits to the nodes is BFDAEC. We organize the trace around the order in which nodes are scanned. In this way, you can understand how the algorithm uses the stack and the traversal rules to proceed from a "current" scan position to the "next" scan position :
- Scan 'B' and then 'F'
- Scan 'D' and then 'A'
- Scan 'E' then 'C'
沒有留言:
張貼留言