lecture_08

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

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

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J Lecture 8 Prof. Piotr Indyk
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
Sorting, ctd. Does it mean that we can
Background image of page 8
Image of page 9
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 Right Arrow Icon
Ask a homework question - tutors are online