Download presentation
Presentation is loading. Please wait.
Published byNaomi Norris Modified over 9 years ago
1
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
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
3 in-order traversal? method A... D B H FCAI EG
4
4 in-order traversal? method B... D B H FCAI EG
5
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
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
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
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
9 Divide & Conquer Strategy use existing elements for decision nodes 1 5 1320505299
10
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
11 Binary Search Again Searching “50” 1 5 13 20 50 52 99 151320505299 low mid hi
12
12 Binary Search Trees Properties For every node: all items in left subtree <item item < all items in right subtree Ascending order obtained by ___________ traversal 20 5 5 1 1 13 50 99 52 51 8 8 “≤” if duplicates are allowed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.