L05 BST

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

9/22/2016

Overview
Binary Search Trees
http://izismile.com/tags/Gibraltar/

• A problem: Runway reservations


10
• A new datastructure:
Binary Search Trees 5 12

1 6

6.006 Lecture 5 7
Image from Wikipedia.org

Runway reservation system Runway reservation system


 Maintain reservations for set of landings on single  Example
runway
 Legality: in future + at least 3 unscheduled minutes 53
before/after each landing
x
 Operations:
 Add new request to land at time t (if legal)  R = (41, 46, 49, 56)
 Find next landing and remove from set  requests for time:
 44 => reject (46 in R)
 53 => ok, R is now (41, 46, 49, 53, 56)
 20 => not allowed (already past)
 Ideas for efficient implementation ?

1
9/22/2016

A possibility? Data structures that we know:


• Represent R as (sorted) linked-list
• Add O(n)
• Find next O(1)
minutes • Represent R as unsorted array
1 i maxtime • Add O(n) (test legality? Insert O(1))
• Find next O(n)
x xx x x • Represent R as sorted array
• Add O(n) (insert? Test legality O(log n))
• Find next O(1)
Insert x,i
• Represent R as min heap Is this the
Numbers • Add O(n) (test legality?)
Add O(1) arbitrarily • Find next O(1) best we
large
Find next O(maxtime) can do?

Let’s apply our 6.006 data Binary Search Trees (BSTs)


structure superpowers again! A tree …
…where each node x has:
10
a key[x]

DS
three pointers:
5 12
left[x] : points to left child
right[x] : points to right child 1 6
p[x] : points to parent
E.g. key[x1]=10 7
left[x1]=x2
p[x2]=x1
p[x1]=NIL

2
9/22/2016

Binary Search Trees (BSTs) Binary Search Trees (BSTs)


BST property : BST property :
for any node x: x for any node x: 10
– for all nodes y in left – for all nodes y in the
subtree of x: left subtree of x: 5 12
key[y] ≤ key[x] key[y] ≤ key[x]
– for all nodes y in right – for all nodes y in the 1 6
subtree of x: <key[x] right subtree of x:
key[y] ≥ key[x] key[y] ≥ key[x] 7

>key[x]

Sorting given BST Question:


Inorder-tree-walk(x):  Given a set of keys, is there a unique BST?
10
 If ≠ Null
 Inorder-tree-walk(left[x])
5 12  No!
 Output key[x]
 Inorder-tree-walk(right[x])
1 6 1 2

7 2 1

O(n) time!

3
9/22/2016

Some BST operations: BST-Sort


– insert(k): insert a node with key k at the appropriate  Create BST from input
location of the tree 10,12,5,6,1,7
– find(k): finds the node containing key k (if it exists)  Inorder-tree-walk(root)
– delete(k): delete the node containing key k, if such a
node exists 10
– findmin(x) (findmax(x)): finds the minimum
(maximum) of the tree rooted at x
5 12
– deletemin(): finds the minimum of the tree and
deletes it
– successor(x): finds the node containing the key that 1 6
is the immediate next of key[x]
7

Growing BSTs BST Insert


root insert(k): insert a node with key k at the
appropriate location of the tree
• Insert 10 10
• Insert 12 3 root
e.g. insert(37)
• Insert 5 2 5 12
height
• Insert 1 49
?
• Insert 6 1 1 6
41? 56
• Insert 7 0 7
46

4
9/22/2016

BST insert BST find


insert(k): insert a node with key k at the find(k): finds the node containing key k (if it exists)
appropriate location of the tree

root e.g. insert(37)


root e.g. find(46)

49 Running time?
?
49
Running time?
5
41 6 ??41
41 5
6
4 3 4
37 6 7 ?6
46

Aside: Can do the “within 3” check for reservation system


during insertion.

BST findmin BST delete


findmin: finds the node containing min valued key k delete(k): delete the node containing key k, if such a
node exists

root e.g. delete(46)


10 “Go left young man”
-- Horace Greeley 4
49
?9
5 12
4 5
1 6 Running time? ?
?141 6
3 44
7 7 6?6

5
9/22/2016

BST delete Successor Go up tree


until find a
delete(k): delete the node containing key k, if such a “left child”
node exists successor(x):
• If right[x] ≠ NIL then
10
return findmin(right[x])
root e.g. delete(46) • Otherwise
y ← p[x] 5 12
4
49 While y≠NIL and x=right[y] do
?9
49
Running time? • x←y 1 6 y
41?4 5 • y ← p[y] x
1 6 Return y 7
3
7
Question: What if we have to delete a node that is internal? successor( 5 ) = 6

How do we fill in the hole? A: next lecture. successor( 7 )

Successor Go up tree Go up tree


until find a Successor until find a
“left child” “left child”
successor(x): successor(x):
• If right[x] ≠ NIL then • If right[x] ≠ NIL then y
10 10
return findmin(right[x]) return findmin(right[x])
• Otherwise y • Otherwise
y ← p[x] 5 12 y ← p[x] 5 x 12
While y≠NIL and x=right[y] do x While y≠NIL and x=right[y] do
• x←y 1 6 • x←y 1 6
• y ← p[y] • y ← p[y]
Return y 7 Return y 7

successor( 5 ) = 6 successor( 5 ) = 6

successor( 7 ) successor( 7 ) = 10

6
9/22/2016

Analysis Analysis
• Height can be n-1 1
• How much time do operations 5
take ? 10
• Still O(n) for the runway 6
• Worst case: O(height)
5 12 reservation system operations? 7
=> height really important
10
1 6 • Next lecture:
• After we insert n elements,
12
what is the worst possible BST
height?
7
Balanced BSTs

You might also like