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

cs 261 data structures
SMART_READER_LITE
LIVE PREVIEW

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 !=


slide-1
SLIDE 1

CS ¡261 ¡– ¡Data ¡Structures ¡

Binary ¡Tree ¡Iterator ¡

CS261 ¡– ¡Data ¡Structures ¡

slide-2
SLIDE 2

Recursive ¡Traversal ¡

¡ ¡

void ¡inorder(struct ¡Node ¡*node) ¡{ ¡ ¡ ¡ ¡if ¡(node ¡!= ¡0){ ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>le9); ¡ ¡ ¡ ¡ ¡ ¡process ¡(node-­‑>val); ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>rght); ¡ ¡ ¡ ¡} ¡ ¡} ¡

¡ ¡ ¡ ¡ ¡

Robert ¡ Gabby ¡ Sam ¡ Kate ¡ Abigail ¡ Dave ¡

slide-3
SLIDE 3

Goals ¡

  • In-­‑Order ¡traversal ¡that ¡supports ¡the ¡Iterator ¡

Interface ¡ ¡(HasNext, ¡Next) ¡

– Concepts ¡ – ImplementaLon ¡ ¡

slide-4
SLIDE 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? ¡
slide-5
SLIDE 5

Exercise ¡

¡ ¡What ¡is ¡being ¡stored ¡in ¡the ¡process ¡stack? ¡ ¡

void ¡inorder(struct ¡Node ¡*node) ¡{ ¡ ¡ ¡ ¡if ¡(node ¡!= ¡0){ ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>le9); ¡ ¡ ¡ ¡ ¡ ¡process ¡(node-­‑>val); ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>rght); ¡ ¡ ¡ ¡} ¡ ¡} ¡

¡ ¡ ¡ ¡ ¡

Robert ¡ Gabby ¡ Sam ¡ Kate ¡ Abigail ¡ Dave ¡

slide-6
SLIDE 6

Exercise ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

void ¡inorder(struct ¡Node ¡*node) ¡{ ¡ ¡ ¡ ¡if ¡(node ¡!= ¡0){ ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>le9); ¡ ¡ ¡ ¡ ¡ ¡process ¡(node-­‑>val); ¡ ¡ ¡ ¡ ¡ ¡inorder(node-­‑>rght); ¡ ¡ ¡ ¡} ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡

Robert ¡ Gabby ¡ Sam ¡ Kate ¡ Abigail ¡ Dave ¡

InOrder ¡Robert ¡ ¡InOrder ¡Gabby ¡ ¡ ¡InOrder ¡Abigail ¡ ¡ ¡ ¡InOrder ¡NULL ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Process ¡Abigail ¡ ¡ ¡ ¡InOrder ¡Dave ¡ ¡ ¡ ¡ ¡InOrder ¡NULL ¡ ¡ ¡ ¡ ¡Process ¡Dave ¡ ¡ ¡ ¡ ¡InOrder ¡NULL ¡ ¡ ¡Process ¡Gabby ¡ ¡ ¡InOrder ¡Kate ¡ When ¡Process ¡Abigail: ¡ ¡Robert ¡ ¡Gabby ¡ ¡Abigail ¡ ¡ are ¡all ¡unfinished! ¡ When ¡Process ¡Dave ¡ ¡Robert ¡ ¡Gabby ¡ ¡Abigail ¡ ¡ ¡Dave ¡ are ¡all ¡unfinished! ¡ When ¡Process ¡Gabby ¡ ¡Robert ¡ ¡Gabby ¡ are ¡all ¡unfinished! ¡

Process ¡Stack ¡represents ¡a ¡ path ¡to ¡ the ¡leYmost ¡ ¡unprocessed ¡ node!! ¡

slide-7
SLIDE 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; ¡ ¡ ¡ ¡ ¡} ¡ ¡} ¡

slide-8
SLIDE 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 ¡

slide-9
SLIDE 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 ¡stack ¡(but ¡don’t ¡pop ¡node) ¡

slide-10
SLIDE 10

In-­‑Order ¡EnumeraLon: ¡Sliding ¡LeY ¡

Stack ¡holds ¡the ¡path ¡to ¡the ¡leYmost ¡node ¡=> ¡next ¡node ¡ ¡ you ¡can ¡go ¡UNDER ¡=> ¡path ¡to ¡the ¡next ¡smallest ¡element ¡ ¡ in ¡a ¡BST ¡

slide-11
SLIDE 11

In-­‑Order ¡Iterator: ¡SimulaLon ¡

1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡

Not ¡yet ¡visited. ¡ On ¡stack ¡(lowest ¡node ¡at ¡top). ¡ Enumerated ¡(order ¡indicated). ¡

1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡ 1 ¡ 3 ¡ 3 ¡ 4 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡ 1 ¡ 3 ¡ 4 ¡ 4 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡

IniLalized ¡in ¡hasNext() ¡

¡ ¡ ¡slideLe9 ¡

next, ¡hasNext ¡

¡ ¡

next, ¡hasNext ¡

¡ ¡ ¡pop, ¡slideLe9 ¡

next, ¡hasNext ¡

¡ ¡pop ¡

next, ¡hasNext ¡

¡ ¡ ¡pop ¡

slide-12
SLIDE 12

In-­‑Order ¡Iterator: ¡ ¡SimulaLon ¡

1 ¡ 3 ¡ 4 ¡ 5 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡

Not ¡yet ¡visited. ¡ On ¡stack ¡(lowest ¡node ¡at ¡top). ¡ Enumerated ¡(order ¡indicated). ¡

1 ¡ 3 ¡ 4 ¡ 5 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 9 ¡ 6 ¡ 1 ¡ 3 ¡ 4 ¡ 5 ¡ 2 ¡ 6 ¡ 7 ¡ 8 ¡ 7 ¡ 6 ¡ 1 ¡ 3 ¡ 4 ¡ 5 ¡ 2 ¡ 8 ¡ 10 ¡ 9 ¡ 7 ¡ 6 ¡ 1 ¡ 3 ¡ 4 ¡ 5 ¡ 2 ¡ 8 ¡ 10 ¡ 9 ¡ 7 ¡ 6 ¡

next,hasNext ¡

¡ ¡ ¡pop ¡

next,hasNext ¡

¡ ¡ ¡pop, ¡slideLe9 ¡

next,hasNext() ¡

¡ ¡ ¡pop, ¡slideLe9 ¡

next,hasNext ¡

¡ ¡ ¡pop ¡

next,hasNext ¡ next,hasNext ¡

slide-13
SLIDE 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) ¡

slide-14
SLIDE 14

Complexity? ¡

  • Each ¡nodes ¡goes ¡on ¡the ¡stack ¡exactly ¡one ¡Lme ¡
  • Each ¡node ¡is ¡popped ¡off ¡the ¡stack ¡exactly ¡one ¡

Lme ¡

  • ¡O(N) ¡
slide-15
SLIDE 15

Other ¡Traversals ¡

  • Pre-­‑order ¡and ¡post-­‑order ¡traversals ¡also ¡use ¡a ¡

stack ¡

  • See ¡Chapter ¡10 ¡discussion ¡
slide-16
SLIDE 16

Level-­‑Order ¡IteraLon ¡

¡Haven’t ¡seen ¡this ¡traversal ¡ yet: ¡

– Traverse ¡nodes ¡a ¡level ¡at ¡a ¡ Lme ¡from ¡leY ¡to ¡right ¡ – Start ¡with ¡root ¡level ¡and ¡then ¡ traverse ¡its ¡children ¡and ¡then ¡ their ¡children ¡and ¡so ¡on ¡ – ImplementaLon? ¡ ¡

¡Example ¡result: ¡p ¡s ¡e ¡a ¡m ¡l ¡r ¡a ¡t ¡e ¡e ¡

e ¡ l ¡ s ¡ a ¡ a ¡ m ¡ r ¡ t ¡ e ¡ p ¡ e ¡

slide-17
SLIDE 17

Your ¡Turn ¡

¡Complete ¡Worksheet ¡#30: ¡BST ¡Iterator ¡