CSE 326: Data Structures Lecture #7 Branching Out

Download Report

Transcript CSE 326: Data Structures Lecture #7 Branching Out

CSE 326: Data Structures
Lecture #18
Fistful of Data Structures
Steve Wolfman
Winter Quarter 2000
Today’s Outline
•
•
•
•
•
•
What Steve Didn’t Get To On Monday
Warm-up: augmenting leftist heaps
Binomial Queues
Treaps
Randomized Skip Lists
What Steve Won’t Get To (Ever?)
Thinking about DecreaseKey
in Leftist Heaps
Why not just percolate up?
decreaseKey(node , 3)
7
3
12
8
7
8
3
15
20 22
17
9
18
30
12
20 22
17
9
18
30
DecreaseKey in Leftist Heaps
decreaseKey(15, 3)
7
7
12
8
12
8
3
15
20 22
17
9
18
30
3
20 22
17
9
30
18
Now just merge the two?
Fixing DecreaseKey
in Leftist Heaps
decreaseKey(15, 3)
This may
not be leftist
7
7
So, fix it!
12
3
20 22
This is
still leftist
8
17
9
18
8
30
9
12
30
17
18
Now, merge!
DecreaseKey runtime
runtime:
How many nodes could possibly have the wrong
Null Path Length up the line?
Delete in Leftist Heaps
decreaseKey(15, -)
deleteMin()
runtime:
Binomial Trees
A binomial tree of rank 0 is a one node tree.
rank 0
A binomial tree of rank k is a binomial tree of rank k1 with another binomial tree of rank k-1 hanging
from its root.
rank 1
First Five Binomial Trees
rank 0
rank 1
rank 2
rank 3
rank 4
How many
nodes does a
binomial tree of
rank k have?
Binomial Queue Heap Data Structure
rank 3
rank 2
rank 1
rank 0
• Composed of a forest
of binomial trees, no
size = 10 = 10102
two of the same rank
rank 3
• Heap-order enforced rank 1
within each tree
5
3
• Ranks present can be
9
7 13 4
computed using the
binary representation
15 6 10
of the tree’s size
21
Insertion in Binomial Queues
insert(10)
10
rank 1 rank 2
5
rank 0 rank 1 rank 2
10
3
9
7
13
15
If there’s no rank 0 tree, just put
the new node in as a rank 0 tree.
5
3
9
7
13
15
Insertion in Binomial Queues
insert(10)
10
rank 0 rank 1
5
3
rank 0 rank 1
5
7
rank 2
3
10
3
7
7
5
10
It’s like addition of binary numbers!
1+1 = 0 plus a carry tree
1+1+1 = 1 plus a carry tree
runtime:
Merge in Binomial Queues
rank 1 rank 2
5
rank 0 rank 1
3
9
11
7
4
13
15
16
rank 0
rank 3
11
3
0110 + 0011 = 1001
7
13 4
15 16 5
runtime:
9
DeleteMin in Binomial Queues
These are one Binomial Queue
11
10
14
1
3
8
25
7
27
13 4
15 16 5
9
These are another
Just merge the two:
8
10
11
3
14 25
27
runtime:
7 13 4
15 16
5
9
Binomial Queue Summary
• Implements priority queue ADT
–
–
–
–
Insert in amortized O(1) time
FindMin (with some tricks) in O(1) time
DeleteMin in O(log n) time
Merge in O(log n) time
• Memory use
– O(1) per node
– about the cost of skew heaps
• Complexity?
Treap Dictionary Data Structure
• Treaps have the binary
search tree
– binary tree property
– search tree property
• Treaps also have the
heap-order property!
– randomly assigned
priorities
heap in yellow; search tree in blue
2
9
6
7
4
18
7
8
9
15
Legend:
priority
key
15
12
10
30
Tree + Heap… Why Bother?
Insert data in sorted order into a treap; what shape
tree comes out?
insert(7)
insert(8)
insert(9)
insert(12)
6
7
6
7
2
9
2
9
7
8
6
7
6
7
15
12
Legend:
priority
key
7
8
7
8
Treap Insert
• Choose a random priority
• Insert as in normal BST
• Rotate up until heap order is restored
insert(15)
2
9
6
7
15
12
7
8
2
9
6
7
2
9
15
12
7
8
6
7
9
15
9
15
7
8
15
12
Treap Delete
•
•
•
•
delete(9)
Find the key
Increase its value to 
Rotate it to the fringe
Snip it off
6
7
6
7
2
9
6
7
7
8
9
15
15
12
6
7
7
8
9
15
7
8
9
15

9
15
12
15
12

9

9
7
8
9
15
15
12
6
7
7
8
9
15
15
12
Treap Summary
• Implements Dictionary ADT
– insert in expected O(log n) time
– delete in expected O(log n) time
– find in expected O(log n) time
• Memory use
– O(1) per node
– about the cost of AVL trees
• Complexity?
Perfect Binary Skip List
• Sorted linked list
• # of links of a node is its height
• The height i link of each node (that has one) links
to the next node of height i or greater
22
29
23
20
13
10
19
11
8
2
Find in a Perfect Binary Skip List
• Start i at the maximum height
• Until the node is found or i is one and the next
node is too large:
– If the next node along the i link is less than the target,
traverse to the next node
– Otherwise, decrease i by one
runtime:
Randomized Skip List Intuition
It’s far too hard to insert into a perfect skip list, but is
perfection necessary?
What matters in a skip list?
Randomized Skip List
• Sorted linked list
• # of links of a node is its height
• The height i link of each node (that has one) links
to the next node of height i or greater
• There should be about 1/2 as many nodes of
height i+1 as there are of height i
29
23
20
19
11
10
22
13
8
2
Find in a RSL
• Start i at the maximum height
• Until the node is found or i is one and the next
node is too large:
– If the next node along the i link is less than the target,
traverse to the next node
– Otherwise, decrease i by one
Same as for a perfect skip list!
runtime:
Insertion in a RSL
• Flip a coin until it comes up heads; that takes i
flips. Make the new node’s height i.
• Do a find, remembering nodes where we go down
• Put the node at the spot where the find ends
• Point all the nodes where we went down (up to the
new node’s height) at the new node
• Point the new node’s links where those redirected
pointers were pointing
Insertion Example in RSL
insert(22)
with 3 flips
13
8
29
23
20
19
11
10
2
29
23
20
19
11
10
22
13
8
2
runtime:
Range Queries and Iteration
• Range query: search for everything that falls
between two values
• Iteration: successively return (in order) each
element in the structure
How do we do them?
How fast are they?
Randomized Skip List Summary
• Implements Dictionary ADT
– insert in expected O(log n)
– find in expected O(log n)
– delete?
• Memory use
– expected constant memory per node
– about double a linked list
• Complexity?
What We Won’t Discuss
Pairing heaps - practically, the fastest and best implementation of
heaps for decreaseKey and merge; they use the leftist cut and
merge technique
Red-Black Trees - a balanced tree that uses just a one-bit color flag
and some invariants to maintain balance: see www/homes/sds/rb.html
AA-Trees - a cross between Red-Black trees and B-Trees that is
relatively simple to code and gives worst case O(log n) running
time
Deterministic skip lists - a version of skip lists that gives worst case
O(log n) running time
To Do
• Finish Project III
• Browse chapters 10 & 12 in the book
• Form Project IV teams!
– groups of 4-6
– 2 1/2 week project
– demos at the end
Coming Up
•
•
•
•
•
Quad Trees
k-D Trees
Quiz (February 17th)
Project III due (February 17th by 5PM!)
Project IV distributed (February 18th)