0% found this document useful (0 votes)
27 views67 pages

15 SearchTrees

Binary search trees allow for efficient search, insert, and delete operations in O(h) time on average where h is the height of the tree. AVL trees, (2,4) trees, and red-black trees are self-balancing binary search trees that perform rebalancing operations during insertions and deletions to guarantee O(log n) time for all operations by keeping the height of the tree low.

Uploaded by

Arhaan Khaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views67 pages

15 SearchTrees

Binary search trees allow for efficient search, insert, and delete operations in O(h) time on average where h is the height of the tree. AVL trees, (2,4) trees, and red-black trees are self-balancing binary search trees that perform rebalancing operations during insertions and deletions to guarantee O(log n) time for all operations by keeping the height of the tree low.

Uploaded by

Arhaan Khaku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

COSC 222 Data

Structures
Yves Lucet

Shiyu Ji, CC BY-SA 4.0, via Wikimedia Commons

"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

7 12 Oct 18 Dijsktra+adaptable PQ Union-find 5 hashing


13 Oct 20 Union-find/Disjoint sets iMAT3
8 14 Oct 25 tMAT3 Search Trees AVL/RB 6 connected
15 Oct 27 Lecture BST, AVL, (2,4), RB B-Trees
9 16 Nov 01 B-trees iMAT4
17 Nov 03 tMAT4
10 18 Nov 08 Midterm review P eer 3 NO LAB
19 Nov 10 Midterm
11 Nov 15 Reading week
Nov 17 Reading week Text processing
12 20 Nov 22 Pattern matching KMP, BM, Trie 7 matching
21 Nov 24 RAT4 Huffman coding
13 22 Nov 29 Huffman coding iMAT5 8 Trie vs. BST vs. Hash table
23 Dec 01 tMAT5 2
14 24 Dec 06 Review/Course Evaluation P eer 4 NO LAB
Plan
• tMAT feedback
• Summary
• Binary search tree
• AVL tree
• (2,4) tree
• Red-Black tree
tMAT: build & critic
A credit card company is keeping track of all the transactions. Each transaction is
described as a date given as an integer storing the number of seconds since Jan 1,
1970 (Unix epoch), an amount (in dollars), and a description. At each second, there are
0 or 1 transaction recorded with at least one transaction a minute. The company
wants best time performance but is not concerned about space. Consequently, the
following performance must be achieve:
a) for inserting a new transaction and have it immediately available for operations
b) and c)
b) for accessing a transaction based on date; you can assume there is only one
transaction every second
c) for returning all the transactions between 2 given dates
d) for returning all the transactions between 2 given costs

A. [8 marks] Describe your data structure.


B. [2 marks] Justify the complexity of your solution for a)-d) e.g. using a linear
search in a list is . Indicate if it is worst-case, average, or expected.

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 list in order they arrive i.e. sorted by time, in


array with pointer to list node, and in skip list with pointer to list
node
• a) O(1); b) O(1);
c) find start & end in Array then iterate in the list: O(h)
since there is at least one transaction per minute 5
tMAT: Solution 1’
array + nextIndex + skip list

• Array indexed on date storing pointer to transaction + skip counter


• skip counter i.e. the index of the next transaction, which plays the
same role as a 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

• hash table with key on date [saves space]


• Each transaction stores a pointer to the next
transaction (explicit list data structure)
• SkipList with key on cost (in cents)
• a) O(1); b) O(1); expected time
• c) O(h) worst case after O(1) expected to find first
transaction i.e. O(h) expected, O(n) worst-case
• d) O(log n + h) expected
7
tMAT: Solution 3 [Most time efficient]
array + nextTransInt + array
• Array with index on (translated) key on date as epoch
and transaction storing the next transaction date [waste
space]
• Transaction object stores date, cost, description,
nextTransactionTime
• array indexed by cost (in cents) with value storing a list
of pointers to Transactions having that specific cost
[waste space]

• a) O(1); b) O(1); worst-case time


• c) O(h) worst-case 8
2022-10-27
tMAT: Solution 4 [Best overall]
hash table + nextTransInt + array 1666912734

• Hash table with key on seconds


• Transaction object stores date, cost, description, Hash function
nextTransactionTime
• array indexed by cost (in cents) with value storing a
list of pointers to Transactions having that specific
cost [waste space]

• a) O(1); b) O(1); expected time


• c) O(h) worst-case
• d) O(h + range size) worst-case
9
tMAT

• Average 72%; 2016: 76%; 2014: 74%

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

Balanced search trees


• search/insert/delete in in the worst case
• space in the worst-case
Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014

Binary Search Trees


< 6

2 9
>
1 4 = 8

© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 13


Binary Search Trees
A binary search tree is a
binary tree storing keys An inorder traversal of a
(or key-value entries) at binary search trees
its internal nodes and visits the keys in
satisfying the following increasing order
property:
 Let u, v, and w be three 6
nodes such that u is in
2 9
the left subtree of v and
w is in the right subtree 1 4 8
of v. We have
key(u)  key(v)  key(w)
External nodes do not
store items
© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 14
Search
Algorithm TreeSearch(k, v)
To search for a key k, we if T.isExternal (v)
trace a downward path return v
starting at the root if k < key(v)
The next node visited return TreeSearch(k, left(v))
depends on the comparison else if k = key(v)
of k with the key of the return v
current node else { k > key(v) }
If we reach a leaf, the key return TreeSearch(k,
right(v))
is not found
< 6
Example: get(4):
 Call TreeSearch(4,root) 2
> 9
The algorithms for nearest 1 4 = 8
neighbor queries are
similar
© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 15
Insertion
6
To perform operation put(k, <
o), we search for key k 2 9
>
(using TreeSearch)
1 4 8
Assume k is not already in >
the tree, and let w be the
leaf reached by the search w
We insert k at node w and
expand w into an internal 6
node 2 9
Example: insert 5
1 4 8
w
5

© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 16


Deletion
6
To perform operation <
remove(k), we search for 2 9
key k >
Assume key k is in the tree, 1 4 v 8
w
and let let v be the node 5
storing k
If node v has a leaf child w,
we remove v and w from the
tree with operation 6
removeExternal(w), which
2 9
removes w and its parent
Example: remove 4 1 5 8

© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 17


Deletion (cont.)
1
We consider the case where v
3
the key k to be removed is
stored at a node v whose 2 8
children are both internal 6 9
 we find the internal node w w
that follows v in an inorder 5
traversal z
 we copy key(w) into node v
 we remove node w and its 1
left child z (which must be a v
leaf) by means of operation 5
removeExternal(z) 2 8
Example: remove 3 6 9

© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 18


Performance
Consider an ordered
map with n items
implemented by means
of a binary search tree
of height h
 the space used is O(n)
 methods get, put and
remove take O(h) time
The height h is O(n) in
the worst case and
O(log n) in the best
case

© 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees 19


https://cmps-people.ok.ubc.ca/ylucet/DS/BST.html

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 20


Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014

AVL Trees
6
v
3 8
z
4

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 21


AVL Tree Definition Height of node v is number of
edges from leaf node to v in the
longest path

AVL trees are 4


44
balanced
2 3
An AVL Tree is a 17 78
binary search tree 1 2 1
32 50 88
such that for every
1 1
internal node v of 48 62
T, the heights of
the children of v
can differ by at An example of an AVL tree where the
most 1 heights are shown next to the nodes

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 22


n(2) 3

4 n(1)

Height of an AVL Tree


Fact: The height of an AVL tree storing n keys is O(log n).
Proof (by induction): Let us bound n(h): the minimum number
of internal nodes of an AVL tree of height h.
We easily see that n(1) = 1 and n(2) = 2
For n > 2, an AVL tree of height h contains the root node,
one AVL subtree of height n-1 and another of height n-2.
That is, n(h) = 1 + n(h-1) + n(h-2)
Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),
n(h) > 2in(h-2i)
Solving the base case we get: n(h) > 2 h/2-1
Taking logarithms: h < 2log n(h) +2
Thus the height of an AVL tree is O(log n)
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 23
Insertion
Insertion is as in a binary search tree
Always done by expanding an external node.
Example:
44 44

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

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 25


Insertion Example, continued
5
44
2
z 64
17 78 7
3
2y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced... T2
T0
4
T1 44 4
2 3 x
17
2 y 62
z6
1 2 2
32
1
1 50 3 5
78 7
1 1
...balanced 48 54 88

T2

T0 T1 T3
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 26
Restructuring (as Single Rotations)
Single Rotations:

a=z single rotation b=y


b=y a=z c=x
c=x

T0 T3
T1 T3 T0 T1 T2
T2

c=z single rotation b=y


b=y a=x c=z
a=x

T3 T3
T0 T2 T2 T1 T0
T1
© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 27
Restructuring (as Double Rotations)
double rotations:

a=z double rotation b=x


c=y a=z c=y
b=x

T0 T2
T2 T3 T0 T1 T3
T1

c=z double rotation b=x


a=y a=y c=z
b=x

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

before deletion of 32 after deletion

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 29


Rebalancing after a Removal
Let z be the first unbalanced node encountered while travelling up the tree
from w. Also, let y be the child of z with the larger height, and let x be the
child of y with the larger height
We perform a trinode restructuring to restore balance at z
As this restructuring may upset the balance of another node higher in the tree,
we must continue checking for balance until the root of T is reached
62
a=z 44

44 78
w 17 62 b=y

17 50 88
50 78 c=x

48 54
48 54 88

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 30


AVL Tree Performance
AVL tree storing n items
 The data structure uses O(n) space
 A single restructuring takes O(1) time
 using a linked-structure binary tree
 Searching takes O(log n) time
 height of tree is O(log n), no restructures needed
 Insertion takes O(log n) time
 initial find is O(log n)
 restructuring up the tree, maintaining heights is O(log n)
 Removal takes O(log n) time
 initial find is O(log n)
 restructuring up the tree, maintaining heights is O(log n)

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 31


https://cmps-people.ok.ubc.ca/ylucet/DS/AVLtree.html

© 2014 Goodrich, Tamassia, Goldwasser AVL Trees 32


Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014

(2,4) Trees

2 5 7 10 14

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 33


Multi-Way Search Tree
A multi-way search tree is an ordered tree such that
 Each internal node has at least two children and stores d -1
key-element items (ki, oi), where d is the number of children
 For a node with children v1 v2 … vd storing keys k1 k2 … kd-1
 keys in the subtree of v1 are less than k1
 keys in the subtree of vi are between ki-1 and ki (i = 2, …, d - 1)
 keys in the subtree of vd are greater than kd-1
 The leaves store no items and serve as placeholders
11 24

2 6 8 15 27 32

30

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 34


Multi-Way Inorder Traversal
We can extend the notion of inorder traversal from binary trees
to multi-way search trees
Namely, we visit item (ki, oi) of node v between the recursive
traversals of the subtrees of v rooted at children vi and vi + 1
An inorder traversal of a multi-way search tree visits the keys in
increasing order

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

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 36


(2,4) Trees
A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way
search with the following properties
 Node-Size Property: every internal node has at most four children
 Depth Property: all the external nodes have the same depth
Depending on the number of children, an internal node of a
(2,4) tree is called a 2-node, 3-node or 4-node

10 15 24

2 8 12 18 27 32

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 37


Height of a (2,4) Tree
Theorem: A (2,4) tree storing n items has height O(log n)
Proof:
 Let h be the height of a (2,4) tree with n items
 Since there are at least 2i items at depth i = 0, … , h - 1 and no
items at depth h, we have
n  1 + 2 + 4 + … + 2h-1 = 2h - 1
 Thus, h  log (n + 1)
Searching in a (2,4) tree with n items takes O(log n) time
depth items
0 1

1 2
h-1 2h-1

h 0

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 38


Insertion
We insert a new item (k, o) at the parent v of the leaf reached by
searching for k
 We preserve the depth property but
 We may cause an overflow (i.e., node v may become a 5-node)
Example: inserting key 30 causes an overflow
10 15 24
v
2 8 12 18 27 32 35

10 15 24
v
2 8 12 18 27 30 32 35

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 39


Overflow and Split
We handle an overflow at a 5-node v with a split operation:
 let v1 … v5 be the children of v and k1 … k4 be the keys of v
 node v is replaced nodes v' and v"
 v' is a 3-node with keys k1 k2 and children v1 v2 v3
 v" is a 2-node with key k4 and children v4 v5
 key k3 is inserted into the parent u of v (a new root may be created)
The overflow may propagate to the parent node u
u u
15 24 15 24 32

v v' v"
12 18 27 30 32 35 12 18 27 30 35

v1 v2 v3 v4 v5 v1 v2 v3 v4 v5

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 40


Analysis of Insertion
Algorithm put(k, o) Let T be a (2,4) tree
with n items
1. We search for key k to locate  Tree T has O(log n)
the insertion node v height
2. We add the new entry (k, o) at  Step 1 takes O(log n)
time because we visit
node v O(log n) nodes
3. while overflow(v)  Step 2 takes O(1) time
Step 3 takes O(log n)
if isRoot(v)

time because each split


create a new empty root takes O(1) time and we
perform O(log n) splits
above v Thus, an insertion in a
v  split(v) (2,4) tree takes O(log n)
time
© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 41
Deletion
We reduce deletion of an entry to the case where the item is at the
node with leaf children
Otherwise, we replace the entry with its inorder successor (or,
equivalently, with its inorder predecessor) and delete the latter entry
Example: to delete key 24, we replace it with 27 (inorder successor)
10 15 24

2 8 12 18 27 32 35

10 15 27

2 8 12 18 32 35

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 42


Underflow and Fusion
Deleting an entry from a node v may cause an underflow, where
node v becomes a 1-node with one child and no keys
To handle an underflow at node v with parent u, we consider two
cases
Case 1: the adjacent siblings of v are 2-nodes
 Fusion operation: we merge v with an adjacent sibling w and move an
entry from u to the merged node v'
 After a fusion, the underflow may propagate to the parent u

u u
9 14 9
w v v'
2 5 7 10 2 5 7 10 14

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 43


Underflow and Transfer
To handle an underflow at node v with parent u, we consider
two cases
Case 2: an adjacent sibling w of v is a 3-node or a 4-node
 Transfer operation:
1. we move a child of w to v
2. we move an item from u to v
3. we move an item from w to u
 After a transfer, no underflow occurs

u u
4 9 4 8
w v w v
2 6 8 2 6 9

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 44


Analysis of Deletion
Let T be a (2,4) tree with n items
 Tree T has O(log n) height
In a deletion operation
 We visit O(log n) nodes to locate the node from
which to delete the entry
 We handle an underflow with a series of O(log n)
fusions, followed by at most one transfer
 Each fusion and transfer takes O(1) time
Thus, deleting an item from a (2,4) tree takes
O(log n) time

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 45


Comparison of Map Implementations

Search Insert Delete Notes


o no ordered map
Hash 1 1 1 methods
Table expected expected expected
o simple to implement

log n log n log n o randomized insertion


Skip List high prob. high prob. high prob. o simple to implement

AVL and
(2,4) log n log n log n o complex to implement
worst-case worst-case worst-case
Tree

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 46


https://cmps-people.ok.ubc.ca/ylucet/DS/BTree.html

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 47


Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014

Red-Black Trees
6
v
3 8
z
4

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 48


From (2,4) to Red-Black Trees
A red-black tree is a representation of a (2,4) tree by means of a
binary tree whose nodes are colored red or black
In comparison with its associated (2,4) tree, a red-black tree has
 same logarithmic time performance
 simpler implementation with a single node type

4 3 5 2 6 7

4 5 3 6
3 OR 5 2 7

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 49


Red-Black Trees
A red-black tree can also be defined as a binary
search tree that satisfies the following properties:
 Root Property: the root is black
 External Property: every leaf is black
 Internal Property: the children of a red node are black
 Depth Property: all the leaves have the same black depth

4 15

2 6 12 21

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 50


Height of a Red-Black Tree
Theorem: A red-black tree storing n items has height
O(log n)
Proof:
 The height of a red-black tree is at most twice the height of
its associated (2,4) tree, which is O(log n)
The search algorithm for a binary search tree is the
same as that for a binary search tree
By the above theorem, searching in a red-black tree
takes O(log n) time

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 51


Insertion
To insert (k, o), we execute the insertion algorithm for binary
search trees and color red the newly inserted node z unless it is
the root
 We preserve the root, external, and depth properties
 If the parent v of z is black, we also preserve the internal property and
we are done
 Else (v is red ) we have a double red (i.e., a violation of the internal
property), which requires a reorganization of the tree
Example where the insertion of 4 causes a double red:

6 6
v v
3 8 3 8
z z
4

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 52


Remedying a Double Red
Consider a double red with child z and parent v, and let w be
the sibling of v
Case 1: w is black Case 2: w is red
 The double red is an incorrect  The double red corresponds
replacement of a 4-node to an overflow
 Restructuring: we change the  Recoloring: we perform the
4-node replacement equivalent of a split

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

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 55


Recoloring
A recoloring remedies a child-parent double red when the parent
red node has a red sibling
The parent v and its sibling w become black and the grandparent u
becomes red, unless it is the root
It is equivalent to performing a split on a 5-node
The double red violation may propagate to the grandparent u

4 4
w v w v
2 7 2 7
z z
6 6

… 4 …
2 4 6 7
2 6 7

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 56


Analysis of Insertion
Algorithm insert(k, o) Recall that a red-black tree
has O(log n) height
1. We search for key k to locate the Step 1 takes O(log n) time
insertion node z because we visit O(log n)
nodes
2. We add the new entry (k, o) at Step 2 takes O(1) time
node z and color z red
Step 3 takes O(log n) time
because we perform
3. while doubleRed(z)  O(log n) recolorings, each
if isBlack(sibling(parent(z))) taking O(1) time, and
z  restructure(z)  at most one restructuring
return taking O(1) time
else { sibling(parent(z) is red } Thus, an insertion in a red-
z  recolor(z) black tree takes O(log n) time

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 57


Deletion
To perform operation remove(k), we first execute the deletion
algorithm for binary search trees
Let v be the internal node removed, w the external node removed,
and r the sibling of w
 If either v of r was red, we color r black and we are done
 Else (v and r were both black) we color r double black, which is a
violation of the internal property requiring a reorganization of the tree
Example where the deletion of 8 causes a double black:

6 6
v r
3 8 3
r w
4 4

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 58


Remedying a Double Black
The algorithm for remedying a double black node w with sibling
y considers three cases
Case 1: y is black and has a red child
 We perform a restructuring, equivalent to a transfer , and we are
done
Case 2: y is black and its children are both black
 We perform a recoloring, equivalent to a fusion, which may
propagate up the double black violation
Case 3: y is red
 We perform an adjustment, equivalent to choosing a different
representation of a 3-node, after which either Case 1 or Case 2
applies
Deletion in a red-black tree takes O(log n) time

© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 59


Red-Black Tree Reorganization
Insertion remedy double red
Red-black tree action (2,4) tree action result
change of 4-node
restructuring double red removed
representation
double red removed
recoloring split
or propagated up

Deletion remedy double black


Red-black tree action (2,4) tree action result
restructuring transfer double black removed
double black removed
recoloring fusion
or propagated up
change of 3-node restructuring or
adjustment
representation recoloring follows
© 2014 Goodrich, Tamassia, Goldwasser Red-Black Trees 60
Red-Black tree advantages
• Space: only requires 1 bit of information per node
• Search: same as binary search tree
• Insertion:
• in worst case of color changes (very quick)
• on average of color changes
• No more than 2 tree rotations
• Deletion:
• in worst case of color changes (very quick)
• on average of color changes
• No more than 3 tree rotations
Source: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
https://cmps-people.ok.ubc.ca/ylucet/DS/RedBlack.html

© 2014 Goodrich, Tamassia, Goldwasser (2,4) Trees 62


AVL vs. Red-Black tree
By Cburnett - Own work, CC BY-SA 3.0,
https://commons.wikimedia.org/w/index.php?curi
d=1508398

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

© 2014 Goodrich, Tamassia, Goldwasser 65


66

Date Files Readings


1 readings-Java-UnitTesting.txtDownload 1 readings-Java-UnitTesting.txt
Sep 13
1 Unit Testing handout.pdfDownload 1 Unit Testing handout.pdf

Sep 15 The readings should be


2 readings-complexity.txtDownload 2 readings-complexity.txt
done before the indicated
Sep 20 3 readings-Array-List.txtDownload 3 readings-Array-List.txt
date with questions posted
5_6 readings-recursion-stack-queue.txt on Ms Teams.
Sep 27
Download 5_6 readings-recursion-stack-queue.txt
Oct 4 7 readings-iterator.txtDownload 7 readings-iterator.txt
Oct 11 8_9 readings Trees-PQ.txtDownload 8_9 readings Trees-PQ.txt
Oct 13 10 reading hash skiplist.txtDownload 10 reading hash skiplist.txt
Oct 18 11 readings Dijkstra.txtDownload 11 readings Dijkstra.txt
Oct 20 12 reading union-find.txtDownload 12 reading union-find.txt
Oct 27 14 readings search trees.txtDownload 14 readings search trees.txt
Nov 17 15 reading (2,4)-B trees.txtDownload 15 reading (2,4)-B trees.txt

Nov 24 21 readings text processing.txtDownload 21 readings text processing.txt

Nov 29 22 readings RK Trie.txtDownload 22 readings RK Trie.txt


Dec 1 23 readings Huffman.txt
67

End of class This Photo by Unknown Author is licensed under CC BY

You might also like