# 2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.

## Presentation on theme: "2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."— Presentation transcript:

2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW COMP 103 Marcus Frean finishing traversals, and introducing Binary Search Trees (I)

2 RECAP-TODAY RECAP  Tree traversals TODAY  iterative in-order traversals! (two ways to proceed...)  Introduction to Binary Search Trees  Using hierarchical access to unstructured data  Reading: Chapter 17.1

3 in-order traversal? method A... D B H FCAI EG

4 in-order traversal? method B... D B H FCAI EG

5 Depth-First vs Breadth-First  Depth-First:  Visit one child and all of its descendants before its siblings  May visit root before, after, or between its children  +Requires only one branch to manage traversal!  − May find “costly” results first, and may not find results at all (infinite search trees)  Breadth-First (level-order):  Visit nodes in order of increasing depth  May choose nodes in a level in a specific order  − Requires accumulation of levels to manage traversal  +Finds minimal solutions and guarantees success!

6 Remember: Efficiency Challenge  Sorted Arrays  access: O(log(n))  modification:O(n)  Linear Linked Structures  access:O(n)  modification:O(1) [provided we know the position]  Can we have the best of both worlds?  yes, using trees!

7 Divide & Conquer  Strategy  eliminate as many as possible in each step Toby Tiger Lea Lion Bully Bulldog Tanja Tui Kurt Kaka Tim Turtle Sally Snake Egg Laying Mammal Reptile Feline Canine Bird

8 Divide & Conquer  Strategy  eliminate as many as possible in each step  what does this remind you of?  can we improve on the below? 15132050 52 99 >19 <19 >51 <10 >10 <51

9 Divide & Conquer  Strategy  use existing elements for decision nodes 1 5 1320505299

10 Divide & Conquer  Strategy  use existing elements for decision nodes  now we need less nodes can get “lucky” on the way down 1 5 13 20 50 52 99

11 Binary Search Again  Searching “50” 1 5 13 20 50 52 99 151320505299 low mid hi

12 Binary Search Trees  Properties  For every node: all items in left subtree { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4367877/slides/slide_12.jpg", "name": "12 Binary Search Trees  Properties  For every node: all items in left subtree

Download ppt "2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas."

Similar presentations