recitation03

recitation03 - MIT OpenCourseWare http:/ocw.mit.edu 6.006...

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

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .
Background image of page 1

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

View Full DocumentRight Arrow Icon
6.006 Recitation Build 2008.6
Background image of page 2
Outline Binary Search Trees Principles Algorithms & Python Code Augmenting Binary Search Trees Rank computation
Background image of page 3

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

View Full DocumentRight Arrow Icon
Orientation Binary Search Trees (BSTs) Time/op: O(lg(N)) avg, O(N) max Balanced BSTs Time/op: O(lg(N)) guaranteed Hash Tables Time/op: O(1) avg, O(N) worst
Background image of page 4
Motivation:Web Sites Many millions of DAILY visitors, billions of queries (searches) Run on SQL databases SQL indexes are mainly Tree indexes Hash indexes
Background image of page 5

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

View Full DocumentRight Arrow Icon
BST Invariants Binary rooted tree All left descendants have keys < node’s key All right descendants have keys > node’s key 8 3 10 6 1 14 4 7 13
Background image of page 6
BST Conclusions No key shows up twice Each subtree contains all and only the keys with values in an interval left subtree: upper bound right subtree: lower bound 8 3 10 6 1 14 4 7 13
Background image of page 7

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

View Full DocumentRight Arrow Icon
BST Subtree Intervals > 3 < 8 8 3 10 6 1 14 4 7 13
Background image of page 8
Invariants Rock!
Background image of page 9

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

View Full DocumentRight Arrow Icon
Invariants Rock! You can mess up a BST infinitely; as long as you maintain the invariants, it works
Background image of page 10
‘cause you need to know how to build this
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 Design BSTnode attributes: key, children (left & right) methods: insert, find (subtree rooted at) BST attributes: root of the tree methods: same as above
Background image of page 12
BST Search 1 class BSTnode(object): 2 def __init__(self, parent, t): 3 self.key = t 3 self.parent = parent 4 self.left = None 5 self.right = None 6 7 def find(self, t): 8 if t == self.key: 9 return self 10 elif t < self.key: 11 if self.left is None : 12 return None 13 else : 14 return self.left.find(t) 15 else : 16 if self.right is None : 17 return None 18 else : 19 return self.right.find(t)
Background image of page 13

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

View Full DocumentRight Arrow Icon
Insert 16 8 3 10 6 1 14 4 7 13
Background image of page 14
Insert 16 8 3 10 6 1 14 4 7 13 16
Background image of page 15

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

View Full DocumentRight Arrow Icon
Insert 9 8 3 10 6 1 14 4 7 13
Background image of page 16
Insert 9 8 3 10 6 1 14 4 7 13 9
Background image of page 17

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

View Full DocumentRight Arrow Icon
Insert 6.1 8 3 10 6 1 14 4 7 13
Background image of page 18
Insert 6.1 8 3 10 6 1 14 4 7 13 6.1
Background image of page 19

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

View Full DocumentRight Arrow Icon
BST Insertion 1 class BSTnode(object): 2 def __init__(self, parent, t): 3 self.key = t 4 self.parent = parent 4 self.left = None 5 self.right = None 6 7 def insert(self, t): 8 if t < self.key: 9 if self.left is None : 10 self.left = BSTnode(self, t) 11 else : 12 self.left.insert(t) 13 else : 14 if self.right is None : 15 self.right = BSTnode(self, t) 16 else : 17 self.right.insert(t)
Background image of page 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The BST Wrapper class BST(object): def __init__(self): self.root = None def insert(self, t): if self.root is None : self.root = BSTnode(None, t) else : self.root.insert(t) def find(self, t): if self.root is None : return None else : return self.root.find(t)
Background image of page 21

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

View Full DocumentRight Arrow Icon
Successor of 8 8 3 10 6 1 14 4 7 13
Background image of page 22
Successor of 8 8 3 10 6 1 14 4 7 13
Background image of page 23

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

View Full DocumentRight Arrow Icon
Successor of 10 8 3 10 6 1 14 4 7 13 12
Background image of page 24
Successor of 10 8 3 10 6 1 14 4 7 13 12
Background image of page 25

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

View Full DocumentRight Arrow Icon
Successor of 10 8 3 10 6 1 14 4 7 13 12
Background image of page 26
Successor of 10 8 3 10 6 1 14 4 7 13 12
Background image of page 27

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

View Full DocumentRight Arrow Icon
Successor of 7 8 3 10 6 1 14 4 7 13
Background image of page 28
Successor of 7 8 3 10 6 1 14 4 7 13
Background image of page 29

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

View Full DocumentRight Arrow Icon
Successor of 7 8 3 10 6 1 14 4 7 13
Background image of page 30
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/24/2010 for the course CS 6.006 taught by Professor Erikdemaine during the Spring '08 term at MIT.

Page1 / 78

recitation03 - MIT OpenCourseWare http:/ocw.mit.edu 6.006...

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

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