CSE 326: Data Structures Lecture #21 Multidimensional Search Trees
CSE 326: Data Structures Lecture #18 Fistful of Data Structures
description
Transcript of CSE 326: Data Structures Lecture #18 Fistful of Data Structures
CSE 326: Data StructuresLecture #18
Fistful of Data Structures
Steve WolfmanWinter 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?
18
30917
812
7
15
2220
decreaseKey( , 3)
3
18
30917
87
3
12
2220
node
DecreaseKey in Leftist Heaps
18
30917
812
7
15
2220
decreaseKey(15, 3)
3
18
30917
812
7
3
2220
Now just merge the two?
Fixing DecreaseKey in Leftist Heaps
decreaseKey(15, 3)
18
30917
812
7
3
2220This is
still leftist
This may not be leftist
18
309 17
8 12
7So, fix it!
Now, merge!
DecreaseKey runtime
How many nodes could possibly have the wrong Null Path Length up the line?
runtime:
Delete in Leftist Heaps
decreaseKey(15, -)deleteMin()
runtime:
Binomial Trees
A binomial tree of rank 0 is a one node tree.
A binomial tree of rank k is a binomial tree of rank k-1 with another binomial tree of rank k-1 hanging from its root.
rank 0
rank 1
First Five Binomial Treesrank 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
• Composed of a forest of binomial trees, no two of the same rank
• Heap-order enforced within each tree
• Ranks present can be computed using the binary representation of the tree’s size
5
9
3
7 13
15
4
6 10
21
size = 10 = 10102
rank 1 rank 3
rank 0rank 1rank 2rank 3
Insertion in Binomial Queues
If there’s no rank 0 tree, just putthe new node in as a rank 0 tree.
5
9
3
7 13
15
rank 1 rank 2insert(10)
10 5
9
3
7 13
15
rank 1 rank 2
10
rank 0
Insertion in Binomial Queues
insert(10)
5 3
7
rank 0 rank 1
10 3
7 5
10
rank 2
3
7
rank 0 rank 1
5
10
It’s like addition of binary numbers!1+1 = 0 plus a carry tree1+1+1 = 1 plus a carry tree runtime:
Merge in Binomial Queues
5
9
3
7 13
15
rank 1 rank 2
11 4
16
rank 0 rank 1
11
rank 0
3
7 13
15
rank 3
4
16 5
9
0110 + 0011 = 1001
runtime:
DeleteMin in Binomial Queues
11 3
7 13
15
4
16 5
9
1
8 25
27
10
14
These are one Binomial Queue
These are another
Just merge the two:8
11
10
14 25
27
3
7 13
15
4
16 59runtime:
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
1512
1030
915
78
418
67
29
heap in yellow; search tree in blue
prioritykey
Legend:
Tree + Heap… Why Bother?
Insert data in sorted order into a treap; what shape tree comes out?
67
insert(7)
67
insert(8)
78
67
insert(9)
78
29
67
insert(12)
78
29
1512
prioritykey
Legend:
Treap Insert• Choose a random priority• Insert as in normal BST• Rotate up until heap order is restored
67
insert(15)
78
29
1512
67
78
29
1512
915
67
78
29
915
1512
Treap Delete• Find the key• Increase its value to • Rotate it to the fringe• Snip it off
delete(9)
67
78
29
915
1512
78
67
9
915
1512
78
67
915
9
1512
78
67
915
1512
9
78
67
915
1512
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
8
2
11
10
1913 20
22
2923
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
2 19 23
8
13
292010
22
11
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
2 19 23
8
13
292010 11
insert(22)with 3 flips
2 19 23
8
13
292010
22
11
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 DiscussPairing 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)