cs 261 data structures
play

CS 261 Data Structures Binary Tree Iterator Recursive - PowerPoint PPT Presentation

CS261 Data Structures CS 261 Data Structures Binary Tree Iterator Recursive Traversal void inorder(struct Node *node) { if (node !=


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

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

  3. Goals ¡ • In-­‑Order ¡traversal ¡that ¡supports ¡the ¡Iterator ¡ Interface ¡ ¡(HasNext, ¡Next) ¡ – Concepts ¡ – ImplementaLon ¡ ¡

  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 ¡beQer? ¡

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

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

  7. SoluLon ¡ à ¡Replace ¡Process ¡Stack ¡ • Simulate ¡recursion ¡using ¡a ¡stack ¡ • Stack ¡path ¡as ¡we ¡traverse ¡down ¡to ¡the ¡leYmost ¡ element ¡ (smallest ¡in ¡BST) ¡ • Useful ¡rouLne: ¡ ¡void ¡_slideLe9(struct ¡Stack ¡*stk, ¡struct ¡Node ¡*n) ¡ ¡ ¡ ¡ ¡while ¡(n ¡!= ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pushStack(stk, ¡n); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡n ¡= ¡n-­‑>le9; ¡ ¡ ¡ ¡ ¡} ¡ ¡} ¡

  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 ¡leY ¡(on ¡stack) ¡to ¡ iterate ¡ • Sets ¡up ¡the ¡subsequent ¡call ¡to ‘ Next() ’ by ¡making ¡sure ¡ the ¡leYmost ¡node ¡(smallest ¡in ¡BST) ¡element ¡is ¡on ¡top ¡of ¡ the ¡stack. ¡ ¡It ¡does ¡this ¡by ¡calling ¡_slideLeY ¡on ¡the ¡ node’s ¡right ¡child ¡

  9. BST ¡In-­‑Order ¡Iterator: ¡ Algorithm ¡ ¡IniLalize: ¡create ¡an ¡empty ¡stack ¡ ¡hasNext: ¡ ¡ ¡ ¡if ¡stack ¡is ¡empty ¡perform ¡slide ¡leY ¡on ¡root ¡ ¡ ¡ ¡ ¡otherwise ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡let ¡n ¡be ¡top ¡of ¡stack ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pop ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡slide ¡leY ¡on ¡right ¡child ¡of ¡n ¡ ¡ ¡ ¡return ¡true ¡if ¡stack ¡is ¡not ¡empty ¡ (false ¡otherwise) ¡ ¡next: ¡ ¡ ¡return ¡value ¡of ¡node ¡on ¡top ¡of ¡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¡ 7 ¡ next,hasNext ¡ next,hasNext ¡ ¡ ¡ ¡pop ¡ ¡ ¡ ¡pop, ¡ slideLe9 ¡ 3 ¡ 3 ¡ 6 ¡ 6 ¡ 6 ¡ 2 ¡ 7 ¡ 2 ¡ 7 ¡ 2 ¡ 7 ¡ 1 ¡ 5 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 1 ¡ 5 ¡ 9 ¡ 4 ¡ 6 ¡ 7 ¡ 4 ¡ 8 ¡ 10 ¡ 4 ¡ 8 ¡ 10 ¡ next,hasNext() ¡ next,hasNext ¡ next,hasNext ¡ ¡ ¡ ¡pop, ¡ slideLe9 ¡ ¡ ¡ ¡pop ¡ next,hasNext ¡ 3 ¡ 3 ¡ 3 ¡

  13. BST ¡In-­‑Order ¡Iterator: ¡ Algorithm ¡ ¡IniLalize: ¡create ¡an ¡empty ¡stack ¡ ¡hasNext: ¡ ¡ ¡ ¡if ¡stack ¡is ¡empty ¡perform ¡slide ¡leY ¡on ¡root ¡ ¡ ¡ ¡ ¡otherwise ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡let ¡n ¡be ¡top ¡of ¡stack ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pop ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡slide ¡leY ¡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 ¡Lme ¡ • Each ¡node ¡is ¡popped ¡off ¡the ¡stack ¡exactly ¡one ¡ Lme ¡ • ¡O(N) ¡

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

  16. Level-­‑Order ¡IteraLon ¡ ¡Haven’t ¡seen ¡this ¡traversal ¡ yet: ¡ p ¡ – Traverse ¡nodes ¡a ¡level ¡at ¡a ¡ Lme ¡from ¡leY ¡to ¡right ¡ s ¡ e ¡ – Start ¡with ¡root ¡level ¡and ¡then ¡ traverse ¡its ¡children ¡and ¡then ¡ their ¡children ¡and ¡so ¡on ¡ a ¡ m ¡ r ¡ l ¡ – ImplementaLon? ¡ a ¡ e ¡ t ¡ ¡ e ¡ ¡Example ¡result: ¡p ¡s ¡e ¡a ¡m ¡l ¡r ¡a ¡t ¡e ¡e ¡

  17. Your ¡Turn ¡ ¡Complete ¡Worksheet ¡#30: ¡BST ¡Iterator ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend