Search - CSE 4101/5101 Prof. Andy Mirzaian Search Trees...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 4101/5101 Search Trees Prof. Andy Mirzaian
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lists Move-to-Front Search Trees Binary Search Trees Multi-Way Search Trees B-trees Splay Trees 2-3-4 Trees Red-Black Trees SELF ADJUSTING WORST-CASE EFFICIENT competitive competitive? Linear Lists Multi-Lists Hash Tables DICTIONARIES 2
Background image of page 2
TOPICS Ø Binary Trees Ø Binary Search Trees Ø Multi-way Search Trees 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
References: [CLRS] chapter 12 4
Background image of page 4
Binary Trees 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Trees: A Quick Review d b e a c g f root[T] n (internal) nodes n-1 (internal) edges n+1 external nodes (nil) n+1 external edges (nil) Node x structure: key[x] p[x] parent left[x] left child right[x] right child external nodes (nil) 6
Background image of page 6
Binary Tree Traversals § Inorder(T): Inorder(L); r; Inorder(R). abcdefg § Preorder(T): r ; Preorder(L); Preorder(R). dbacegf § Postorder(T): Postorder(L); Postorder(R); r. acbfged § Levelorder(T): non-decreasing depth order dbeacgf (same depth left-to-right) r L R T: graph BFS d b e a c g f root[T] graph DFS 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Traversals in O(n) time procedure Inorder(x) 1. if x = nil then return 2. Inorder(left[x]) 3. visit(x) 4. Inorder(right[x]) end r L R Running Time Analysis by Accounting: Line 1: n+1 external nodes (return), n (internal) nodes (continue). Line 3: Assume visit takes O(1) time. Lines 2 & 4: After recursive expansion: Each node x (internal or external) visited exactly once. O(1) time execution of lines 1 & 3 charged to node x. Total n + (n+1) nodes, each charged O(1) time. Total time = O(2n+1) = O(n). § Preorder and Postorder are similar and take O(n) time. § Exercise: Write a simple O(n) time algorithm for Levelorder. [Hint: use a queue.] 8
Background image of page 8
Running Time Analysis by Recurrence CLAIM: Time(T) = 2 |T| + 1. Proof: By induction on |T|. Basis (|T|=0): Time(T) = 1 = 2 |T| + 1. Induction Step (|T| > 0): Time(T) = Time(L) + Time(R) + 1 [by the recurrence] = (2|L|+1) + (2|R|+1) + 1 [by the Induction Hypothesis] = 2(|L| + |R| + 1) + 1 = 2 |T| + 1. r L R T: 9 = + + = nil if 1 1 ) ( ) ( ) ( T T R Time L Time T Time
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Search Trees 10
Background image of page 10
BST from Binary Search on Sorted Array E0 < K1 < E1 < K2 < E2 < K3 < E3 < K4 < E4 < K5 < E5 < K6 < E6 <  < Kn < En K1 E0 K2 E1 K3 E2 K4 E3 K5 E4 K6 E5 E6 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BST from Binary Search on Sorted Array E0 < K1 < E1 < K2 < E2 < K3 < E3 < K4 < E4 < K5 < E5 < K6 < E6 <  < Kn < En K1 E0 K2 E1 K3 E2 K4 E3 K5 E4 K6 E5 E6 K1 E0 K2 E1 E2 K4 E3 K5 E4 K6 E5 E6 K3 12
Background image of page 12
BST from Binary Search on Sorted Array E0 < K1 < E1 < K2 < E2 < K3 < E3 < K4 < E4 < K5 < E5 < K6 < E6 <  < Kn < En K1 E0 K2 E1 K3 E2 K4 E3 K5 E4 K6 E5 E6 K3 E0 K2 E1 E2 K4 E3 K5 E4 E5 E6 K1 K6 13
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BST from Binary Search on Sorted Array E0 < K1 < E1 < K2 < E2 < K3 < E3 < K4 < E4 < K5 < E5 < K6 < E6 <  < Kn < En K1 E0 K2 E1 K3 E2 K4 E3 K5 E4 K6 E5 E6 K3 E0 E6 K1 K6 E1 E2 E3 K5 E4 E5 K2 K4 14
Background image of page 14
BST from Binary Search on Sorted Array E0 < K1 < E1 < K2 < E2 < K3 < E3 < K4 < E4 < K5 < E5 < K6 < E6 <  < Kn < En K1 E0 K2 E1 K3 E2 K4 E3 K5 E4 K6 E5 E6 K3 E0 E6 K1 K6 E1 E2 E3 K2 K4 E4 E5 K5 SORTED ORDER BST INORDER 15
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BST Definition BST is a binary tree T with one distinct key per node such that: Ø Inorder node sequence of T encounters keys in sorted order.
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/13/2012 for the course CSE 4101 taught by Professor Mirzaian during the Winter '12 term at York University.

Page1 / 34

Search - CSE 4101/5101 Prof. Andy Mirzaian Search Trees...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online