cse 100 ternary tries and skip...
TRANSCRIPT
![Page 1: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/1.jpg)
CSE 100 Ternary Tries and Skip Lists
![Page 2: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/2.jpg)
Multi-way tries: Efficient finding of keys by their sequence Build the trie to store the following numbers:
Is there a way to find whether all keys contained in a sequence of digits are present in the trie? A. Yes B. No
![Page 3: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/3.jpg)
Properties of tries Build the trie to store the following numbers:
If you stored the same N D-digit keys in a Binary Search Tree, what would be the worst case height of the tree? A. N B. lg(10^D) C. lg(N) D. lg(D) E. Other
![Page 4: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/4.jpg)
Properties of tries Build the trie to store the following numbers:
Consider storing the full 10^D keys. We know that on average the height of a BST will be lg(10^D). Which is smaller: D or lg(10^D)? A. D B. lg(10^D) C. They are the same
![Page 5: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/5.jpg)
Properties of tries Build the trie to store the following numbers:
So what is the main drawback of tries? A. They are difficult to implement B. They (usually) waste a lot of space C. They are slow D. There is no drawback of tries
![Page 6: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/6.jpg)
Ternary search trees to the rescue! • Tries combine binary search trees with tries. • Each node contains the following:
• A key digit for search comparison • Three pointers:
• left and right: for when the digit being considered is less than and greater than (respectively) the digit stored in the node (the BST part)
• middle: for when the digit being considered is equal to the digit stored in the node (the trie part)
• An end bit to indicate we’ve completed a key stored in the tree.
![Page 7: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/7.jpg)
List all the words (strings) you can find in this TST
g
i a
f
t
e
c
a
r s
e
t
m
Are the following in the tree? (A=yes, B=no) • get • if • gif • its • gacar • tsem
![Page 8: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/8.jpg)
Draw the ternary tree for the following (in this order)
i just met this is crazy call me maybe
Does the structure of the tree depend on the order in which keys were inserted? A. Yes B. No
![Page 9: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/9.jpg)
Draw the ternary tree for the following (in this order)
i just met this is crazy call me maybe
Does the tree have a strong ordering property (i.e. keys in the left subtree are always less than trees in the right subtree? A. Yes B. No
![Page 10: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/10.jpg)
Algorithms for insert and find (in TSTs and MWTs) • In your reading and/or in Paul Kube’s slides
![Page 11: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/11.jpg)
Skip Lists: Motivation • Which data structure is faster in the worst case for finding
elements, assuming the elements are sorted? A. An array B. A linked list C. They can both be made equally fast
2 5 9 29 35 42 47 55 58 60 65
2 5 29 42 55 60 65
![Page 12: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/12.jpg)
Skip Lists: Motivation • Which data structure is faster in the worst case for
inserting elements, assuming the elements are sorted? A. An array B. A linked list C. They can both be made equally fast
2 5 9 29 35 42 47 55 58 60 65
2 5 29 42 55 60 65
![Page 13: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/13.jpg)
Toward Skip lists
2 5 29 42 55 60 65
• Adding forward pointers in a list can vastly reduce search time, from O(N) to O(logN) in the worst case.
![Page 14: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/14.jpg)
Toward Skip lists
2 5 29 42 55 60 65
• Costly to maintain a deterministic structure
Skip lists fix this problem by using randomness to randomly determine where pointers go
![Page 15: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/15.jpg)
Node levels
Which of the following depicts a node with max-level 2 in a skip list?
3 5 42
A B C
The max level of a node is equal to the number of forward pointers it has.
![Page 16: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/16.jpg)
Node levels • The following depicts a level 2 node
5
Shorthand
5
Picture of the book’s implementation
![Page 17: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/17.jpg)
Find in a Skip List Highlight the pointers that are followed in a find for the element 12. Annotate the order in which they are followed
42 3 12 20 9 5
.
.
. .
root
![Page 18: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/18.jpg)
Find in a Skip List Which of the following pointers are followed in a find for the element 35?
42 3 12 20 9 5
.
.
. .
A. Red only B. Red and blue only C. Red, blue and purple only D. Red, blue, purple and black E. Some other combination of pointers
root
![Page 19: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/19.jpg)
SkipList find, pseudocode • To find key k:
1. Let x bet list header (root). Let i be the highest non-null index pointer in x
2. While pointer x[i] is not null and points to a key smaller than k, let x = x[i] (follow the pointer)
3. If the pointer x[i] points to a key equal to k return true 4. If the pointer x[i] points to a key larger than k, decrement i
(drop down a level in x) 5. If i < 0 return false. Else go to 2.
42 3 12 20 9 5
.
.
. .
Assumes index pointers are 1 less then level
root
![Page 20: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/20.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
lvl_ind:
22
root
curr .
![Page 21: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/21.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
3
root
curr
lvl_ind:
.
![Page 22: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/22.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
2
root
curr
lvl_ind:
. Because prev[3] is null start at root again
.
![Page 23: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/23.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
2
root
curr
lvl_ind:
.
![Page 24: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/24.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
1 lvl:
root
![Page 25: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/25.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
0 lvl:
root
curr
![Page 26: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/26.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
0 lvl:
root
curr
![Page 27: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/27.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
-1 lvl: 22
root
curr
newNode
![Page 28: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/28.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
-1 lvl: 22
root
curr
newNode
![Page 29: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/29.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
prev
-1 lvl:
22
root
newNode
![Page 30: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/30.jpg)
SkipList insert: slow motion
26 3 21 25 6 5
.
.
.
.
.
22
root
![Page 31: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/31.jpg)
Why Skip Lists? • Why use a skip list? Discuss with your group.
![Page 32: CSE 100 Ternary Tries and Skip Listscseweb.ucsd.edu/classes/fa14/cse100/lectures/Lec22_before_pdf.pdf · Find in a Skip List Highlight the pointers that are followed in a find for](https://reader036.vdocuments.us/reader036/viewer/2022071401/60ebd451dff1dd4ee844ca51/html5/thumbnails/32.jpg)
Why Skip Lists? • Why use a skip list? Discuss with your group.
• Simpler to implement • Simple in-order traversal • Fast find (comparable to Balanced Binary Tree in the average
case) • Min O(1), delete-min O(1) • Amenable to concurrent modification (changes are quite local)