cse 326: data structures lecture #18 fistful of data structures
DESCRIPTION
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?). - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/1.jpg)
CSE 326: Data StructuresLecture #18
Fistful of Data Structures
Steve WolfmanWinter Quarter 2000
![Page 2: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/2.jpg)
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?)
![Page 3: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/3.jpg)
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
![Page 4: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/4.jpg)
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?
![Page 5: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/5.jpg)
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!
![Page 6: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/6.jpg)
DecreaseKey runtime
How many nodes could possibly have the wrong Null Path Length up the line?
runtime:
![Page 7: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/7.jpg)
Delete in Leftist Heaps
decreaseKey(15, -)deleteMin()
runtime:
![Page 8: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/8.jpg)
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
![Page 9: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/9.jpg)
First Five Binomial Treesrank 0 rank 1 rank 2 rank 3
rank 4
How many nodes does a binomial tree of rank k have?
![Page 10: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/10.jpg)
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
![Page 11: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/11.jpg)
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
![Page 12: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/12.jpg)
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:
![Page 13: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/13.jpg)
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:
![Page 14: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/14.jpg)
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:
![Page 15: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/15.jpg)
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?
![Page 16: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/16.jpg)
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:
![Page 17: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/17.jpg)
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:
![Page 18: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/18.jpg)
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
![Page 19: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/19.jpg)
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
![Page 20: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/20.jpg)
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?
![Page 21: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/21.jpg)
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
![Page 22: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/22.jpg)
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:
![Page 23: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/23.jpg)
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?
![Page 24: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/24.jpg)
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
![Page 25: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/25.jpg)
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:
![Page 26: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/26.jpg)
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
![Page 27: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/27.jpg)
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:
![Page 28: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/28.jpg)
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?
![Page 29: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/29.jpg)
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?
![Page 30: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/30.jpg)
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
![Page 31: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/31.jpg)
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
![Page 32: CSE 326: Data Structures Lecture #18 Fistful of Data Structures](https://reader036.vdocuments.net/reader036/viewer/2022062521/568167de550346895ddd3c96/html5/thumbnails/32.jpg)
Coming Up
• Quad Trees• k-D Trees• Quiz (February 17th)• Project III due (February 17th by 5PM!)• Project IV distributed (February 18th)