15 SearchTrees
15 SearchTrees
Structures
Yves Lucet
"Hash table 3 1 1 0 1 0 0 SP" by Jorge Stolfi - Own work. Licensed under CC BY-SA 3.0 via Commons -
1
https://commons.wikimedia.org/wiki/File:Hash_table_3_1_1_0_1_0_0_SP.svg#/media/File:Hash_table_3_1_1_0_1_0_0_SP.svg
CC BY-SA 3.0
https://en.wikipedia.org/wiki/Skip_list#/media/File:Skip_list_add_element-en.gif
Wk Class Date Activity Reading/Prep given Peer Lab
1 1 Sep 06 Syllabus, TBL, Java review NO LAB
1a Sep 08 Git, testing Java, Generics, Testing
2 2 Sep 13 RAT1: generics; unit testing Complexity 1 unit testing
3 Sep 15 Lect: Complexity, streams Lists
3 4 Sep 20 Build & Critic (training) iMAT1 2 coverage testing
5 Sep 22 tMAT1 Recursion
4 6 Sep 27 RAT2 Stack, Queue P eer 1 3 streams
7 Sep 29 Build & Critic Iterators
5 8 Oct 04 mini-lecture+exercises iMAT2 4 simulation
9 Oct 06 tMAT2 BST, PQ, heap Holiday: Mon Oct 2
6 10 Oct 11 RAT3 Hash, skip list P eer 2
11 Oct 13 Hash table, Skiplist, bottom-up
14.6heap
Shortest
construction
path Holiday: Mon Oct 9
4
tMAT: Solution 1
array + list + skip list
• Array indexed on date storing pointer to transaction and to
transaction position in list
• max array size in Java is 2^31/4 = 17 years
• singled linked list<Transaction>; essentially each Transaction has
a next pointer to the next transaction
• skip list with key = cost
• Insert transaction in array with pointer to list node, and in skip list
with pointer to list node; update skip counter and update previous
transaction skip counter
• a) O(1); b) O(1); c) find start Array, iterate in Array using skip counter:
O(h); d) skip list O(log n + h)
6
• Cannot use skip counter to replace skip list. Why?
2022-10-27
tMAT: Solution 2 [Most logical]
hash table + pointer + skip list
Hash function
Feeback:
• Write whatever is needed to justify your complexity: data structure,
explanation/pseudo-code even Java code if it is faster to write
• Other
• Transaction object pre-made or need to be created?
• Map is not precise enough: hashmap (hashtable), treemap (red-black tree)
• Do NOT use a data structure you do not understand, e.g., TreeMap
• Binary search tree is NOT a heap
• h and n are different: number of transactions between dates vs. total number of
transactions. Ask questions 10
Readings
• Search trees
• For AVL/2-3-4/Red-Black trees: do not memorize rebalancing
operations, just be convinced that it works in
• You should be able to
• Given an AVL/2-3-4/Binary Search tree, use
https://cmps-people.ok.ubc.ca/ylucet/DS/Algorithms.html to draw the resulting
tree after insert/delete
• Recognize Binary Search Tree, AVL, Red-Black, 2-3-4 (at
https://cmps-people.ok.ubc.ca/ylucet/DS/BTree.html use Max. Degree =4) Trees
• Know the complexity of each operation: insert, delete, find:
• Apply a balanced tree (Red-Black) in your problem solving questions
11
Balanced search trees
Leveraging binary search further:
• skip list insert/delete/find is but only in expected case
• sorted table is worst-case to find, but does not allow easy
insert/delete
2 9
>
1 4 = 8
AVL Trees
6
v
3 8
z
4
4 n(1)
c=z
17 78 17 78
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
before insertion
after insertion
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 24
Trinode Restructuring
Let (a,b,c) be the inorder listing of x, y, z
Perform the rotations needed to make b the topmost node of the three
Single rotation Double rotation around
around b a=z c and a
a=z
c=y
b=y
T0
T0
c=x b=x
T3
T1 b=y b=x
T2 T3 T1 T2
a=z c=x a=z c=y
T0 T1 T2 T3 T0 T1 T2 T3
T2
T0 T1 T3
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 26
Restructuring (as Single Rotations)
Single Rotations:
T0 T3
T1 T3 T0 T1 T2
T2
T3 T3
T0 T2 T2 T1 T0
T1
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 27
Restructuring (as Double Rotations)
double rotations:
T0 T2
T2 T3 T0 T1 T3
T1
T0 T2
T3 T2 T3 T1 T0
T1
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 28
Removal
Removal begins as in a binary search tree, which means the node removed
will become an empty external node. Its parent, w, may cause an
imbalance.
Example:
44 44
17 62 17 62
32 50 78 50 78
48 54 88 48 54 88
44 78
w 17 62 b=y
17 50 88
50 78 c=x
48 54
48 54 88
(2,4) Trees
2 5 7 10 14
2 6 8 15 27 32
30
11 24
8 12
2 6 8 15 27 32
2 4 6 10 14 18
30
1 3 5 7 9 11 13 16 19
15 17
© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 35
Multi-Way Searching
Similar to search in a binary search tree
A each internal node with children v1 v2 … vd and keys k1 k2 … kd-1
k = ki (i = 1, …, d - 1): the search terminates successfully
k < k1: we continue the search in child v1
ki-1 < k < ki (i = 2, …, d - 1): we continue the search in child vi
k > kd-1: we continue the search in child vd
Reaching an external node terminates the search unsuccessfully
Example: search for 30
11 24
2 6 8 15 27 32
30
10 15 24
2 8 12 18 27 32
1 2
h-1 2h-1
h 0
10 15 24
v
2 8 12 18 27 30 32 35
v v' v"
12 18 27 30 32 35 12 18 27 30 35
v1 v2 v3 v4 v5 v1 v2 v3 v4 v5
2 8 12 18 27 32 35
10 15 27
2 8 12 18 32 35
u u
9 14 9
w v v'
2 5 7 10 2 5 7 10 14
u u
4 9 4 8
w v w v
2 6 8 2 6 9
AVL and
(2,4) log n log n log n o complex to implement
worst-case worst-case worst-case
Tree
Red-Black Trees
6
v
3 8
z
4
4 3 5 2 6 7
4 5 3 6
3 OR 5 2 7
4 15
2 6 12 21
6 6
v v
3 8 3 8
z z
4
4 4
w v w v
2 7 2 7
z z
6 6
4 6 7 2 4 6 7
.. 2 ..
© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 53
Restructuring
A restructuring remedies a child-parent double red when the
parent red node has a black sibling
It is equivalent to restoring the correct replacement of a 4-node
The internal property is restored and the other properties are
preserved
z
4 6
w v v
2 7 4 7
z w
6 2
4 6 7 4 6 7
.. 2 .. .. 2 ..
© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 54
Restructuring (cont.)
There are four restructuring configurations depending on
whether the double red nodes are left or right children
2 6 6 2
6 2 4 4
4 4 2 6
2 6
4 4
w v w v
2 7 2 7
z z
6 6
… 4 …
2 4 6 7
2 6 7
6 6
v r
3 8 3
r w
4 4
AVL Red-Black
• height • height
• more rigidly balanced • There are some Red-Black trees
• any AVL tree can be colored Red- that are not AVL trees
Black • implemented in
• Java: TreeMap, TreeSet;
• C++: map, multimap, multiset;
• Linux kernel: completely fair
scheduler, linux/rbtree.h
By Nomen4Omen - Own workThis W3C-unspecified vector image was
created with Inkscape., CC BY-SA 4.0,
https://commons.wikimedia.org/w/index.php?curid=49182185
https://en.wikipedia.org/wiki/AVL_tree#Comparison_to_other_structures 63
Other trees
• B-tree (critical in databases)
• Splay tree (reorganize after each search)
• Hash tree or Merkle tree (used in block chain)
• k-d tree: range queries in k-dimensional space
• BK-tree (used in spell-checker)
•…
Application