This preview shows page 1. Sign up to view the full content.
Unformatted text preview: e), k);
}; 5 BST
BST search, cont.
14 How
How many nodes do
we
we inspect?
! Examples:
Examples:
– BT_Search(4): 3
– BT_Search(17): 2
– BT_Search(16): 4
! ! 9
4 17
14 8 18
15 Better
Better question: What is the maximum
number of nodes we need to explore to
answer
answer our search for any number?
tree Depth + 1 6 BST
BST search complexity
!
!
!
!
!
!
! Given a complete BST
n (number of nodes) = 2Depth+1 – 1
(number
But
But with BST search, only explore at most
Depth+1
Depth+1 nodes
Let E = Depth+1
So n = 2E – 1, solving for E results with
E = log2(n+1) ≅ log2n
(n+1)
Therefore, # of explored nodes <= E ≅ log2n
<=
Thus,
Thus, BST search is O(log2n) ! 7 Follow
Followup 4 Assumption: complete
Assumption: complete BST
! What about other trees?
! – worst case: narrow tree
– Example: BT_Search(19): 8
Example: BT_Search(19):
(the
(the number of nodes)
– Complexity: O(n)
Complexity:
! 8
9
14
14 In
In general (if proper care is taken in
balancing the tree, or selecting good
pivot
pivot nodes), BST are searchable in
O(log2n) 15...
View
Full
Document
This document was uploaded on 03/06/2014 for the course ECE 368 at Purdue.
 Spring '08
 Staff
 Binary Search, Data Structures

Click to edit the document details