1 / 17

CS 261 – Data Structures

CS 261 – Data Structures. CS261 – Data Structures. Binary Tree Iterator. Recursive Traversal. Robert. Gabby. Sam. Abigail. Dave. Kate. void inorder ( struct Node *node) { if (node != 0){ inorder (node->left) ; process (node-> val ) ; inorder (node-> rght ); } }.

amos-martin
Download Presentation

CS 261 – Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 261 – Data Structures CS261 – Data Structures Binary Tree Iterator

  2. Recursive Traversal Robert Gabby Sam Abigail Dave Kate void inorder(struct Node *node) { if (node != 0){ inorder(node->left); process (node->val); inorder(node->rght); } }

  3. Goals • In-Order traversal that supports the Iterator Interface (HasNext, Next) • Concepts • Implementation

  4. Simple Iterator • Simple iterator  recursively traverse tree, placing all node values into a linked list, then use a linked list iterator • Problem: duplicates data, uses twice as much space • Can we do better?

  5. Exercise Robert Gabby Sam Abigail Dave Kate What is being stored in the process stack? void inorder(struct Node *node) { if (node != 0){ inorder(node->left); process (node->val); inorder(node->rght); } }

  6. Exercise Robert Gabby Sam Abigail Dave Kate void inorder(struct Node *node) { if (node != 0){ inorder(node->left); process (node->val); inorder(node->rght); } } When Process Abigail: Robert Gabby Abigail are all unfinished! InOrder Robert InOrder Gabby InOrder Abigail InOrder NULL Process Abigail InOrder Dave InOrder NULL Process Dave InOrder NULL Process Gabby InOrder Kate Process Stack represents a path to the leftmost unprocessed node!! When Process Dave Robert Gabby Abigail Dave are all unfinished! When Process Gabby Robert Gabby are all unfinished!

  7. Solution  Replace Process Stack • Simulate recursion using a stack • Stack path as we traverse down to the leftmost element (smallest in BST) • Useful routine: void _slideLeft(struct Stack *stk, struct Node *n) while (n != 0) { pushStack(stk, n); n = n->left; } }

  8. Binary Tree In-Order Iterator • Main Idea • Next returns the top of the stack (e.g. the next element you’ll go UNDER in Euler Tour) • HasNext • Returns true if there are elements left (on stack) to iterate • Sets up the subsequent call to‘Next()’by making sure the leftmost node (smallest in BST) element is on top of the stack. It does this by calling _slideLeft on the node’s right child

  9. BST In-Order Iterator: Algorithm Initialize: create an empty stack hasNext: if stack is empty perform slide left on root otherwise let n be top of stack pop n slide left on right child of n return true if stack is not empty (false otherwise) next: return value of node on top of stack (but don’t pop node)

  10. In-Order Enumeration: Sliding Left Stack holds the path to the leftmost node => next node you can go UNDER => path to the next smallest element in a BST

  11. In-Order Iterator: Simulation 10 10 On stack (lowest node at top). 5 9 5 9 Not yet visited. 4 8 1 4 8 1 Enumerated (order indicated). 3 6 7 3 6 7 Initialized in hasNext() slideLeft next, hasNext 2 2 10 10 10 2 9 2 9 2 9 4 8 4 8 4 8 1 1 1 3 6 7 3 6 7 4 6 7 next, hasNext pop, slideLeft next, hasNext pop next, hasNext pop 2 3 3

  12. In-Order Iterator: Simulation 6 10 On stack (lowest node at top). 2 9 2 9 Not yet visited. 5 8 1 5 8 1 Enumerated (order indicated). 4 6 7 4 6 7 next,hasNext pop next,hasNext pop, slideLeft 3 3 6 6 6 2 7 2 7 2 7 5 8 5 9 5 9 1 1 1 4 6 7 4 8 10 4 8 10 next,hasNext() pop, slideLeft next,hasNext pop next,hasNext next,hasNext 3 3 3

  13. BST In-Order Iterator: Algorithm Initialize: create an empty stack hasNext: if stack is empty perform slide left on root otherwise let n be top of stack pop n slide left on right child of n return true if stack is not empty (false otherwise) next: return value of node on top of stack (but don’t pop node)

  14. Complexity? • Each nodes goes on the stack exactly one time • Each node is popped off the stack exactly one time • O(N)

  15. Other Traversals • Pre-order and post-order traversals also use a stack • See Chapter 10 discussion

  16. Level-Order Iteration Haven’t seen this traversal yet: • Traverse nodes a level at a time from left to right • Start with root level and then traverse its children and then their children and so on • Implementation? Example result:p s e a m l r a t e e p s e a m r l a e t e

  17. Your Turn Complete Worksheet #30: BST Iterator

More Related