recitation03

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

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

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 .

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

View Full Document
6.006 Recitation Build 2008.6
Outline Binary Search Trees Principles Algorithms & Python Code Augmenting Binary Search Trees Rank computation

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

View Full Document
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
Motivation:Web Sites Many millions of DAILY visitors, billions of queries (searches) Run on SQL databases SQL indexes are mainly Tree indexes Hash indexes

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

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

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

View Full Document
BST Subtree Intervals > 3 < 8 8 3 10 6 1 14 4 7 13
Invariants Rock!

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

View Full Document
Invariants Rock! You can mess up a BST inﬁnitely; as long as you maintain the invariants, it works
‘cause you need to know how to build this

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

View Full Document
BST Design BSTnode attributes: key, children (left & right) methods: insert, ﬁnd (subtree rooted at) BST attributes: root of the tree methods: same as above
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)

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

View Full Document
Insert 16 8 3 10 6 1 14 4 7 13
Insert 16 8 3 10 6 1 14 4 7 13 16

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

View Full Document
Insert 9 8 3 10 6 1 14 4 7 13
Insert 9 8 3 10 6 1 14 4 7 13 9

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

View Full Document
Insert 6.1 8 3 10 6 1 14 4 7 13
Insert 6.1 8 3 10 6 1 14 4 7 13 6.1

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

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

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

View Full Document
Successor of 8 8 3 10 6 1 14 4 7 13
Successor of 8 8 3 10 6 1 14 4 7 13

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

View Full Document
Successor of 10 8 3 10 6 1 14 4 7 13 12
Successor of 10 8 3 10 6 1 14 4 7 13 12

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

View Full Document
Successor of 10 8 3 10 6 1 14 4 7 13 12
Successor of 10 8 3 10 6 1 14 4 7 13 12

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

View Full Document
Successor of 7 8 3 10 6 1 14 4 7 13
Successor of 7 8 3 10 6 1 14 4 7 13

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

View Full Document
Successor of 7 8 3 10 6 1 14 4 7 13
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
Ask a homework question - tutors are online