Compsci 201 Stacks Queues Priority Queues Owen Astrachan

  • Slides: 8
Download presentation
Compsci 201, Stacks, Queues, Priority Queues Owen Astrachan ola@cs. duke. edu http: //bit. ly/201

Compsci 201, Stacks, Queues, Priority Queues Owen Astrachan ola@cs. duke. edu http: //bit. ly/201 spring 19 March 22, 2019 3/8/2019 Compsci 201, Spring 2019, Search Trees and Recursion 1

R is for … • Recursion • Base case of wonderful stuff • Refactoring

R is for … • Recursion • Base case of wonderful stuff • Refactoring • No new functionality 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators 2

Plan for FCo. Sin 201 • Recursion and Invariants with List Reverse • Thinking

Plan for FCo. Sin 201 • Recursion and Invariants with List Reverse • Thinking about recursion and iteration • Linear Data Structures • Stacks and Queues • Classes, Interfaces, Applications • Priority Queue API 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 3

Recursion Idiom for Trees • Use root = caller(root, val); • Modifies Tree at

Recursion Idiom for Trees • Use root = caller(root, val); • Modifies Tree at root and returns the result • Recursive calls works similarly • root. left = caller(root. left, val); • Process the subtree, use the result • Where do we see this? Insertion into search tree • We also see it in tree tighten APT 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 4

Iterative or Recursive Insert • Where is new node added? At leaf, reason •

Iterative or Recursive Insert • Where is new node added? At leaf, reason • https: //coursework. cs. duke. edu/201 spring 19/classwork-spring 19 • Repeat? root = add(root, scan. next()) 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 5

APT Tree Practice Redux • https: //www 2. cs. duke. edu/csed/newapt/trimtree. html • It's

APT Tree Practice Redux • https: //www 2. cs. duke. edu/csed/newapt/trimtree. html • It's a search tree, so we can use range search • Left subtree <= root. Right subtree > root • With no duplicates, < and not <= • Range [2, 15] whole tree • What about … 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 6

Trimming the Tree • https: //www 2. cs. duke. edu/csed/newapt/trimtree. html • Range [2,

Trimming the Tree • https: //www 2. cs. duke. edu/csed/newapt/trimtree. html • Range [2, 15] both subtrees • What about [3, 7] or [5, 13] or [11, 17] • If root in range? • Process subtrees, return root • t. left = trim(t. left, …) • t. right = trim(t. right, …) • If root not in range? Return result of one call! • return trim(t. left, . . ) or … 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 7

Toward All Green • Identify base-case: null every time • Sometimes leaf? In this

Toward All Green • Identify base-case: null every time • Sometimes leaf? In this case not needed • Make sure you use result of recursive call • Trim returns a tree, what do we do? • t. left = trim(t. left, low, high); • return t • Compare to simply return trim(t. left); • Don't include node when outside range? 3/20/2019 Compsci 201, Spring 2019, Trees and Recurrences 8