Download presentation
Presentation is loading. Please wait.
Published by水 翟 Modified over 7 years ago
1
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW Marcus Frean School of Engineering and Computer Science, Victoria University of Wellington 2014-T2 Lecture 26
2
RECAP-TODAY RECAP Tree traversals TODAY Binary Search Trees
Using hierarchical access to unstructured data Reading: Chapter 17.1
3
Binary Search Again Searching “50” 20 5 52 1 13 50 99 1 5 13 20 50 52
mid low hi
4
“≤” if duplicates are allowed
Binary Search Trees “≤” if duplicates are allowed Properties For every node: all items in left subtree < item item < all items in right subtree Ascending order obtained by ___________ traversal 20 5 52 1 13 50 99 51 8
5
Efficiency How many steps do we need to find an element?
Inverse question is easier to answer: How many nodes fit into a binary tree of height n? Answer: n = 2(height+1) -1 Steps needed = height+1 = log2(n+1) Height Nodes 1 1 3 2 7 3 15
6
Efficiency Does it help? Number of Elements Steps
7
This is why ensureCapacity() (cf. slide 17 in L6)
Efficiency This is why ensureCapacity() (cf. slide 17 in L6) doubles the array size! Does it help? Steps required Elements
8
Efficiency Complexity of finding an element =
Complexity of adding an element = Complexity of removing an element = Fred Celia Joe Anton Des Hans Kim Berta Zoe
9
Efficiency Height depends on order of adding elements
optimal case: O(log(n)) H G L B E J N A C D F I K M O H G L B E J N A C D F I K M O
10
Efficiency Height depends on order of adding elements
worst case: O(n) A B C D E F G H I J K L M O P A B C D E F …
11
Balanced and Unbalanced BSTs
H G B E J N L C F D K I O M A A H I L D F B K J N M C E G O H G B F M K N C A E D O I L J see the discussion of rotations, in the text book
12
Efficiency Unbalanced tree: Balanced tree: worst case cost =
average case cost = Balanced tree: cf. “Random Binary Trees” cf. “AVL”, “2-3”, “Red-Black” trees, etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.