lecture_08

# lecture_08 - Introduction to Algorithms 6.046J/18.401J...

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

Introduction to Algorithms 6.046J/18.401J Lecture 8 Prof. Piotr Indyk

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

View Full Document
Data structures Previous lecture: hash tables – Insert, Delete, Search in (expected) constant time – Works for integers from {0…m r -1} This lecture: Binary Search Trees – Insert, Delete, Search (Successor) – Works in comparison model © Piotr Indyk Introduction to Algorithms October 6, 2004 L7.2
Binary Search Tree Each node x has: –key[x] –Pointers: •left[x] • right[x] • p[x] 9 12 5 1 6 7 8 © Piotr Indyk Introduction to Algorithms October 6, 2004 L7.3

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

View Full Document
Binary Search Tree (BST) Property: for any node x : – For all nodes y in the left subtree of x : key[y] key[x] – For all nodes y in the right subtree of x : key[y] key[x] Given a set of keys, is BST for those keys unique ? 9 12 5 1 6 7 8 © Piotr Indyk Introduction to Algorithms October 6, 2004 L7.4
No uniqueness 9 12 5 1 6 7 8 7 5 9 1 6 8 12 © Piotr Indyk Introduction to Algorithms October 6, 2004 L7.5

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

View Full Document
What can we do given BST ? Sort ! Inorder-Walk( x ): If x NIL then – Inorder-Walk( left[x] ) –pr in t key[x] – Inorder-Walk( right[x] ) Output: 1 7 6 5 8 9 12 5 1 6 7 8 9 12 © Piotr Indyk Introduction to Algorithms
Sorting, ctd. What is the running time of Inorder-Walk? It is O(n) Because: – Each link is traversed twice –There are O(n) links 9 12 5 1 6 7 8 © Piotr Indyk Introduction to Algorithms October 6, 2004 L7.7

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

View Full Document
Sorting, ctd. Does it mean that we can
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

lecture_08 - Introduction to Algorithms 6.046J/18.401J...

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

View Full Document
Ask a homework question - tutors are online