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

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

CSE 4101/5101 Search Trees Prof. Andy Mirzaian

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

View Full Document
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
TOPICS Ø Binary Trees Ø Binary Search Trees Ø Multi-way Search Trees 3

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

View Full Document
References: [CLRS] chapter 12 4
Binary Trees 5

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

View Full Document
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
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

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

View Full Document
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
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 nil if 1 ) ( ) ( ) ( T T R Time L Time T Time

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

View Full Document
Binary Search Trees 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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern