Binary search Tree
Red-Black Trees
Binary Trees
• Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
3. Only what you get from 1 by a finite number of
applications of 2 is a binary tree.
Is this a binary tree?
56
26 200
18 28 190 213
12 24 27
Binary Search Trees
• View today as data structures that can support
dynamic set operations.
– Search, Minimum, Maximum, Predecessor, Successor,
Insert, and Delete.
• Can be used to build
– Dictionaries.
– Priority Queues.
• Basic operations take time proportional to the
height of the tree – O(h).
BST – Representation
• Represented by a linked data structure of nodes.
• root(T) points to the root of tree T.
• Each node contains fields:
– key
– left – pointer to left child: root of left subtree.
– right – pointer to right child : root of right subtree.
– p – pointer to parent. p[root[T]] = NIL (optional).
Binary Search Tree Property
• Stored keys must
satisfy the binary
search tree property.
–  y in left subtree of x,
then key[y]  key[x].
–  y in right subtree of
x, then key[y]  key[x].
56
26 200
18 28 190 213
12 24 27
Inorder Traversal
Inorder-Tree-Walk (x)
1. if x  NIL
2. then Inorder-Tree-Walk(left[p])
3. print key[x]
4. Inorder-Tree-Walk(right[p])
 How long does the walk take?
 Can you prove its correctness?
The binary-search-tree property allows the keys of a binary search tree to be
printed, in (monotonically increasing) order, recursively.
56
26 200
18 28 190 213
12 24 27
Correctness of Inorder-Walk
• Must prove that it prints all elements, in order, and
that it terminates.
• By induction on size of tree. Size=0: Easy.
• Size >1:
– Prints left subtree in order by induction.
– Prints root, which comes after all elements in left
subtree (still in order).
– Prints right subtree in order (all elements come after
root, so still in order).
Querying a Binary Search Tree
• All dynamic-set search operations can be
supported in O(h) time.
• h = (lg n) for a balanced binary tree (and for an
average tree built by adding nodes in random
order.)
• h = (n) for an unbalanced tree that resembles a
linear chain of n nodes in the worst case.
Tree Search
Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
5. else return Tree-Search(right[x], k)
Running time: O(h)
Aside: tail-recursion
56
26 200
18 28 190 213
12 24 27
Iterative Tree Search
Iterative-Tree-Search(x, k)
1. while x  NIL and k  key[x]
2. do if k < key[x]
3. then x  left[x]
4. else x  right[x]
5. return x
The iterative tree search is more efficient on most computers.
The recursive tree search is more straightforward.
56
26 200
18 28 190 213
12 24 27
Finding Min & Max
Tree-Minimum(x) Tree-Maximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x
Q: How long do they take?
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
Predecessor and Successor
• Successor of node x is the node y such that key[y]
is the smallest key greater than key[x].
• The successor of the largest key is NIL.
• Search consists of two cases.
– If node x has a non-empty right subtree, then x’s
successor is the minimum in the right subtree of x.
– If node x has an empty right subtree, then:
• As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
• x’s successor y is the node that x is the predecessor of (x is the
maximum in y’s left subtree).
• In other words, x’s successor y, is the lowest ancestor of x whose
left child is also an ancestor of x.
Pseudo-code for Successor
Tree-Successor(x)
• if right[x]  NIL
2. then return Tree-Minimum(right[x])
3. y  p[x]
4. while y  NIL and x = right[y]
5. do x  y
6. y  p[y]
7. return y
Code for predecessor is symmetric.
Running time: O(h)
56
26 200
18 28 190 213
12 24 27
BST Insertion – Pseudocode
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
• Change the dynamic
set represented by a
BST.
• Ensure the binary-
search-tree property
holds after change.
• Insertion is easier than
deletion. 56
26 200
18 28 190 213
12 24 27
Analysis of Insertion
• Initialization: O(1)
• While loop in lines 3-7
searches for place to
insert z, maintaining
parent y.
This takes O(h) time.
• Lines 8-13 insert the
value: O(1)
 TOTAL: O(h) time to
insert a node.
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
Exercise: Sorting Using BSTs
Sort (A)
for i  1 to n
do tree-insert(A[i])
inorder-tree-walk(root)
– What are the worst case and best case running
times?
– In practice, how would this compare to other
sorting algorithms?
Tree-Delete (T, x)
if x has no children  case 0
then remove x
if x has one child  case 1
then make p[x] point to child
if x has two children (subtrees)  case 2
then swap x with its successor
perform case 0 or case 1 to delete it
 TOTAL: O(h) time to delete a node
Deletion – Pseudocode
Tree-Delete(T, z)
/* Determine which node to splice out: either z or z’s successor. */
• if left[z] = NIL or right[z] = NIL
• then y  z
• else y  Tree-Successor[z]
/* Set x to a non-NIL child of x, or to NIL if y has no children. */
4. if left[y]  NIL
5. then x  left[y]
6. else x  right[y]
/* y is removed from the tree by manipulating pointers of p[y] and x */
7. if x  NIL
8. then p[x]  p[y]
/* Continued on next slide */
Deletion – Pseudocode
Tree-Delete(T, z) (Contd. from previous slide)
9. if p[y] = NIL
10. then root[T]  x
11. else if y  left[p[i]]
12. then left[p[y]]  x
13. else right[p[y]]  x
/* If z’s successor was spliced out, copy its data into z */
14. if y  z
15. then key[z]  key[y]
16. copy y’s satellite data into z.
17. return y
Correctness of Tree-Delete
• How do we know case 2 should go to case 0 or
case 1 instead of back to case 2?
– Because when x has 2 children, its successor is the
minimum in its right subtree, and that successor has
no left child (hence 0 or 1 child).
• Equivalently, we could swap with predecessor
instead of successor. It might be good to
alternate to avoid creating lopsided tree.
Binary Search Trees
• View today as data structures that can support
dynamic set operations.
– Search, Minimum, Maximum, Predecessor, Successor,
Insert, and Delete.
• Can be used to build
– Dictionaries.
– Priority Queues.
• Basic operations take time proportional to the
height of the tree – O(h).
Red-black trees: Overview
• Red-black trees are a variation of binary search
trees to ensure that the tree is balanced.
– Height is O(lg n), where n is the number of nodes.
• Operations take O(lg n) time in the worst case.
Red-black Tree
• Binary search tree + 1 bit per node: the attribute
color, which is either red or black.
• All other attributes of BSTs are inherited:
–key, left, right, and p.
• All empty trees (leaves) are colored black.
– We use a single sentinel, nil, for all the leaves of red-
black tree T, with color[nil] = black.
– The root’s parent is also nil[T ].
24
Red-Black Trees
• “Balanced” binary search trees guarantee an
O(lgn) running time
• Red-black-tree
– Binary search tree with an additional attribute for its
nodes: color which can be red or black
– Constrains the way nodes can be colored on any path
from the root to a leaf:
Ensures that no path is more than twice as long as any other
path  the tree is balanced
25
Example: RED-BLACK-TREE
• For convenience we use a sentinel NIL[T] to represent all
the NIL nodes at the leafs
– NIL[T] has the same fields as an ordinary node
– Color[NIL[T]] = BLACK
– The other fields may be set to arbitrary values
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
26
Red-Black-Trees Properties
(**Satisfy the binary search tree property**)
1. Every node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then both its children are black
• No two consecutive red nodes on a simple path
from the root to a leaf
5. For each node, all paths from that node to descendant
leaves contain the same number of black nodes
27
Black-Height of a Node
• Height of a node: the number of edges in the longest
path to a leaf
• Black-height of a node x: bh(x) is the number of
black nodes (including NIL) on the path from x to a leaf,
not counting x
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
28
Most important property of
Red-Black-Trees
A red-black tree with n internal nodes
has height at most 2lg(n + 1)
• Need to prove two claims first …
29
Claim 1
• Any node x with height h(x) has bh(x) ≥ h(x)/2
• Proof
– By property 4, at most h/2 red nodes on the path from the
node to a leaf
– Hence at least h/2 are black
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
30
Claim 2
• The subtree rooted at any node x contains
at least 2bh(x) - 1 internal nodes
26
17 41
30 47
38 50
NIL NIL
NIL
NIL NIL NIL NIL
NIL
h = 4
bh = 2
h = 3
bh = 2
h = 2
bh = 1
h = 1
bh = 1
h = 1
bh = 1
h = 2
bh = 1 h = 1
bh = 1
31
Claim 2 (cont’d)
Proof: By induction on h[x]
Basis: h[x] = 0 
x is a leaf (NIL[T]) 
bh(x) = 0 
# of internal nodes: 20 - 1 = 0
Inductive Hypothesis: assume it is true for
h[x]=h-1
NIL
x
32
Claim 2 (cont’d)
Inductive step:
• Prove it for h[x]=h
• Let bh(x) = b, then any child y of x has:
– bh (y) =
– bh (y) =
b (if the child is red), or
b - 1 (if the child is black)
26
17 41
30 47
38 50
x
y1 y2
bh = 2
bh = 2
bh = 1
NIL NIL
NIL NIL
NIL
NIL
33
Claim 2 (cont’d)
• Using inductive hypothesis, the
number of internal nodes for each
child of x is at least:
2bh(x) - 1 - 1
• The subtree rooted at x contains at
least:
(2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 =
2 · (2bh(x) - 1 - 1) + 1 =
2bh(x) - 1 internal nodes
x
l r
h
h-1
bh(l)≥bh(x)-1
bh(r)≥bh(x)-1
34
Height of Red-Black-Trees (cont’d)
Lemma: A red-black tree with n internal nodes has
height at most 2lg(n + 1).
Proof:
n
• Add 1 to both sides and then take logs:
n + 1 ≥ 2b ≥ 2h/2
lg(n + 1) ≥ h/2 
h ≤ 2 lg(n + 1)
root
l r
height(root) = h
bh(root) = b
number n
of internal
nodes
≥ 2b - 1 ≥ 2h/2 - 1
since b  h/2
35
Operations on Red-Black-Trees
• The non-modifying binary-search-tree operations
MINIMUM, MAXIMUM, SUCCESSOR,
PREDECESSOR, and SEARCH run in O(h) time
– They take O(lgn) time on red-black trees
• What about TREE-INSERT and TREE-DELETE?
– They will still run on O(lgn)
– We have to guarantee that the modified tree will still be
a red-black tree
36
INSERT
INSERT: what color to make the new node?
• Red? Let’s insert 35!
– Property 4 is violated: if a node is red, then both its
children are black
• Black? Let’s insert 14!
– Property 5 is violated: all paths from a node to its leaves
contain the same number of black nodes
26
17 41
30 47
38 50
37
DELETE
DELETE: what color was the
node that was removed? Black?
1. Every node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then both its children are black
5. For each node, all paths from the node to descendant
leaves contain the same number of black nodes
OK!
OK!
Not OK! Could create
two red nodes in a row
Not OK! Could change the
black heights of some nodes
26
17 41
30 47
38 50
Not OK! If removing
the root and the child
that replaces it is red
38
Rotations
• Operations for re-structuring the tree after insert
and delete operations on red-black trees
• Rotations take a red-black-tree and a node within
the tree and:
– Together with some node re-coloring they help restore
the red-black-tree property
– Change some of the pointer structure
– Do not change the binary-search tree property
• Two types of rotations:
– Left & right rotations
39
Left Rotations
• Assumptions for a left rotation on a node x:
– The right child of x (y) is not NIL
• Idea:
– Pivots around the link from x to y
– Makes y the new root of the subtree
– x becomes y’s left child
– y’s left child becomes x’s right child
40
Example: LEFT-ROTATE
41
LEFT-ROTATE(T, x)
1. y ← right[x] ►Set y
2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree
3. if left[y]  NIL
4. then p[left[y]] ← x ► Set the parent relation from left[y] to x
5. p[y] ← p[x] ► The parent of x becomes the parent of y
6. if p[x] = NIL
7. then root[T] ← y
8. else if x = left[p[x]]
9. then left[p[x]] ← y
10. else right[p[x]] ← y
11. left[y] ← x ► Put x on y’s left
12. p[x] ← y ► y becomes x’s parent
42
Right Rotations
• Assumptions for a right rotation on a node x:
– The left child of y (x) is not NIL
• Idea:
– Pivots around the link from y to x
– Makes x the new root of the subtree
– y becomes x’s right child
– x’s right child becomes y’s left child
43
Insertion
• Goal:
– Insert a new node z into a red-black-tree
• Idea:
– Insert node z into the tree as for an ordinary binary
search tree
– Color the node red
– Restore the red-black-tree properties
• Use an auxiliary procedure RB-INSERT-FIXUP
44
RB Properties Affected by Insert
1. Every node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then both its children are black
5. For each node, all paths
from the node to descendant
leaves contain the same number
of black nodes
OK!
If z is the root
 not OK
OK!
26
17 41
47
38
50
If p(z) is red  not OK
z and p(z) are both red
OK!
45
RB-INSERT-FIXUP – Case 1
z’s “uncle” (y) is red
Idea: (z is a right child)
• p[p[z]] (z’s grandparent) must
be black: z and p[z] are both red
• Color p[z] black
• Color y black
• Color p[p[z]] red
• z = p[p[z]]
– Push the “red” violation up the tree
46
RB-INSERT-FIXUP – Case 1
z’s “uncle” (y) is red
Idea: (z is a left child)
• p[p[z]] (z’s grandparent) must be
black: z and p[z] are both red
• color p[z]  black
• color y  black
• color p[p[z]]  red
• z = p[p[z]]
– Push the “red” violation up the tree
47
RB-INSERT-FIXUP – Case 3
Case 3:
• z’s “uncle” (y) is black
• z is a left child
Case 3
Idea:
• color p[z]  black
• color p[p[z]]  red
• RIGHT-ROTATE(T, p[p[z]])
• No longer have 2 reds in a row
• p[z] is now black
48
RB-INSERT-FIXUP – Case 2
Case 2:
• z’s “uncle” (y) is black
• z is a right child
Idea:
• z  p[z]
• LEFT-ROTATE(T, z)
 now z is a left child, and both z and p[z] are red  case 3
Case 2 Case 3
49
RB-INSERT-FIXUP(T, z)
1. while color[p[z]] = RED
2. do if p[z] = left[p[p[z]]]
3. then y ← right[p[p[z]]]
4. if color[y] = RED
5. then Case1
6. else if z = right[p[z]]
7. then Case2
8. Case3
9. else (same as then clause with “right”
and “left” exchanged)
10. color[root[T]] ← BLACK
The while loop repeats only when
case1 is executed: O(lgn) times
Set the value of x’s “uncle”
We just inserted the root, or
The red violation reached
the root
50
Example
11
Insert 4
2 14
1 15
7
8
5
4
y
11
2 14
1 15
7
8
5
4
z
Case 1
y
z and p[z] are both red
z’s uncle y is red
z
z and p[z] are both red
z’s uncle y is black
z is a right child
Case 2
11
2
14
1
15
7
8
5
4
z
y
Case 3
z and p[z] are red
z’s uncle y is black
z is a left child
11
2
14
1
15
7
8
5
4
z
51
RB-INSERT(T, z)
1. y ← NIL
2. x ← root[T]
3. while x  NIL
4. do y ← x
5. if key[z] < key[x]
6. then x ← left[x]
7. else x ← right[x]
8. p[z] ← y
• Initialize nodes x and y
• Throughout the algorithm y points
to the parent of x
• Go down the tree until
reaching a leaf
• At that point y is the
parent of the node to be
inserted
• Sets the parent of z to be y
26
17 41
30 47
38 50
52
RB-INSERT(T, z)
9. if y = NIL
10. then root[T] ← z
11. else if key[z] < key[y]
12. then left[y] ← z
13. else right[y] ← z
14. left[z] ← NIL
15. right[z] ← NIL
16. color[z] ← RED
17. RB-INSERT-FIXUP(T, z)
The tree was empty:
set the new node to be the root
Otherwise, set z to be the left or
right child of y, depending on
whether the inserted node is
smaller or larger than y’s key
Set the fields of the newly added node
Fix any inconsistencies that could have
been introduced by adding this new red
node
26
17 41
30 47
38 50
53
Analysis of RB-INSERT
• Inserting the new element into the tree O(lgn)
• RB-INSERT-FIXUP
– The while loop repeats only if CASE 1 is executed
– The number of times the while loop can be executed
is O(lgn)
• Total running time of RB-INSERT: O(lgn)
54
Red-Black Trees - Summary
• Operations on red-black-trees:
– SEARCH O(h)
– PREDECESSOR O(h)
– SUCCESOR O(h)
– MINIMUM O(h)
– MAXIMUM O(h)
– INSERT O(h)
– DELETE O(h)
• Red-black-trees guarantee that the height of the
tree will be O(lgn)
55
Problems
• What is the ratio between the longest path and
the shortest path in a red-black tree?
- The shortest path is at least bh(root)
- The longest path is equal to h(root)
- We know that h(root)≤2bh(root)
- Therefore, the ratio is ≤2
56
Problems
• What red-black tree property is violated in the tree
below? How would you restore the red-black tree
property in this case?
– Property violated: if a node is red, both its children are black
– Fixup: color 7 black, 11 red, then right-rotate around 11
11
2
14
1
15
7
8
5
4
z
57
Problems
• Let a, b, c be arbitrary nodes in subtrees , ,  in the
tree below. How do the depths of a, b, c change when
a left rotation is performed on node x?
– a: increases by 1
– b: stays the same
– c: decreases by 1
58
Problems
• When we insert a node into a red-black tree, we
initially set the color of the new node to red. Why
didn’t we choose to set the color to black?
• (Exercise 13.4-7, page 294) Would inserting a
new node to a red-black tree and then
immediately deleting it, change the tree?

BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptx

  • 1.
  • 2.
    Binary Trees • Recursivedefinition 1. An empty tree is a binary tree 2. A node with two child subtrees is a binary tree 3. Only what you get from 1 by a finite number of applications of 2 is a binary tree. Is this a binary tree? 56 26 200 18 28 190 213 12 24 27
  • 3.
    Binary Search Trees •View today as data structures that can support dynamic set operations. – Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. • Can be used to build – Dictionaries. – Priority Queues. • Basic operations take time proportional to the height of the tree – O(h).
  • 4.
    BST – Representation •Represented by a linked data structure of nodes. • root(T) points to the root of tree T. • Each node contains fields: – key – left – pointer to left child: root of left subtree. – right – pointer to right child : root of right subtree. – p – pointer to parent. p[root[T]] = NIL (optional).
  • 5.
    Binary Search TreeProperty • Stored keys must satisfy the binary search tree property. –  y in left subtree of x, then key[y]  key[x]. –  y in right subtree of x, then key[y]  key[x]. 56 26 200 18 28 190 213 12 24 27
  • 6.
    Inorder Traversal Inorder-Tree-Walk (x) 1.if x  NIL 2. then Inorder-Tree-Walk(left[p]) 3. print key[x] 4. Inorder-Tree-Walk(right[p])  How long does the walk take?  Can you prove its correctness? The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively. 56 26 200 18 28 190 213 12 24 27
  • 7.
    Correctness of Inorder-Walk •Must prove that it prints all elements, in order, and that it terminates. • By induction on size of tree. Size=0: Easy. • Size >1: – Prints left subtree in order by induction. – Prints root, which comes after all elements in left subtree (still in order). – Prints right subtree in order (all elements come after root, so still in order).
  • 8.
    Querying a BinarySearch Tree • All dynamic-set search operations can be supported in O(h) time. • h = (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.) • h = (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.
  • 9.
    Tree Search Tree-Search(x, k) 1.if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) Running time: O(h) Aside: tail-recursion 56 26 200 18 28 190 213 12 24 27
  • 10.
    Iterative Tree Search Iterative-Tree-Search(x,k) 1. while x  NIL and k  key[x] 2. do if k < key[x] 3. then x  left[x] 4. else x  right[x] 5. return x The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward. 56 26 200 18 28 190 213 12 24 27
  • 11.
    Finding Min &Max Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x 3. return x Q: How long do they take? The binary-search-tree property guarantees that: » The minimum is located at the left-most node. » The maximum is located at the right-most node.
  • 12.
    Predecessor and Successor •Successor of node x is the node y such that key[y] is the smallest key greater than key[x]. • The successor of the largest key is NIL. • Search consists of two cases. – If node x has a non-empty right subtree, then x’s successor is the minimum in the right subtree of x. – If node x has an empty right subtree, then: • As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. • x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree). • In other words, x’s successor y, is the lowest ancestor of x whose left child is also an ancestor of x.
  • 13.
    Pseudo-code for Successor Tree-Successor(x) •if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y Code for predecessor is symmetric. Running time: O(h) 56 26 200 18 28 190 213 12 24 27
  • 14.
    BST Insertion –Pseudocode Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z • Change the dynamic set represented by a BST. • Ensure the binary- search-tree property holds after change. • Insertion is easier than deletion. 56 26 200 18 28 190 213 12 24 27
  • 15.
    Analysis of Insertion •Initialization: O(1) • While loop in lines 3-7 searches for place to insert z, maintaining parent y. This takes O(h) time. • Lines 8-13 insert the value: O(1)  TOTAL: O(h) time to insert a node. Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z
  • 16.
    Exercise: Sorting UsingBSTs Sort (A) for i  1 to n do tree-insert(A[i]) inorder-tree-walk(root) – What are the worst case and best case running times? – In practice, how would this compare to other sorting algorithms?
  • 17.
    Tree-Delete (T, x) ifx has no children  case 0 then remove x if x has one child  case 1 then make p[x] point to child if x has two children (subtrees)  case 2 then swap x with its successor perform case 0 or case 1 to delete it  TOTAL: O(h) time to delete a node
  • 18.
    Deletion – Pseudocode Tree-Delete(T,z) /* Determine which node to splice out: either z or z’s successor. */ • if left[z] = NIL or right[z] = NIL • then y  z • else y  Tree-Successor[z] /* Set x to a non-NIL child of x, or to NIL if y has no children. */ 4. if left[y]  NIL 5. then x  left[y] 6. else x  right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ 7. if x  NIL 8. then p[x]  p[y] /* Continued on next slide */
  • 19.
    Deletion – Pseudocode Tree-Delete(T,z) (Contd. from previous slide) 9. if p[y] = NIL 10. then root[T]  x 11. else if y  left[p[i]] 12. then left[p[y]]  x 13. else right[p[y]]  x /* If z’s successor was spliced out, copy its data into z */ 14. if y  z 15. then key[z]  key[y] 16. copy y’s satellite data into z. 17. return y
  • 20.
    Correctness of Tree-Delete •How do we know case 2 should go to case 0 or case 1 instead of back to case 2? – Because when x has 2 children, its successor is the minimum in its right subtree, and that successor has no left child (hence 0 or 1 child). • Equivalently, we could swap with predecessor instead of successor. It might be good to alternate to avoid creating lopsided tree.
  • 21.
    Binary Search Trees •View today as data structures that can support dynamic set operations. – Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete. • Can be used to build – Dictionaries. – Priority Queues. • Basic operations take time proportional to the height of the tree – O(h).
  • 22.
    Red-black trees: Overview •Red-black trees are a variation of binary search trees to ensure that the tree is balanced. – Height is O(lg n), where n is the number of nodes. • Operations take O(lg n) time in the worst case.
  • 23.
    Red-black Tree • Binarysearch tree + 1 bit per node: the attribute color, which is either red or black. • All other attributes of BSTs are inherited: –key, left, right, and p. • All empty trees (leaves) are colored black. – We use a single sentinel, nil, for all the leaves of red- black tree T, with color[nil] = black. – The root’s parent is also nil[T ].
  • 24.
    24 Red-Black Trees • “Balanced”binary search trees guarantee an O(lgn) running time • Red-black-tree – Binary search tree with an additional attribute for its nodes: color which can be red or black – Constrains the way nodes can be colored on any path from the root to a leaf: Ensures that no path is more than twice as long as any other path  the tree is balanced
  • 25.
    25 Example: RED-BLACK-TREE • Forconvenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs – NIL[T] has the same fields as an ordinary node – Color[NIL[T]] = BLACK – The other fields may be set to arbitrary values 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL
  • 26.
    26 Red-Black-Trees Properties (**Satisfy thebinary search tree property**) 1. Every node is either red or black 2. The root is black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black • No two consecutive red nodes on a simple path from the root to a leaf 5. For each node, all paths from that node to descendant leaves contain the same number of black nodes
  • 27.
    27 Black-Height of aNode • Height of a node: the number of edges in the longest path to a leaf • Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to a leaf, not counting x 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1
  • 28.
    28 Most important propertyof Red-Black-Trees A red-black tree with n internal nodes has height at most 2lg(n + 1) • Need to prove two claims first …
  • 29.
    29 Claim 1 • Anynode x with height h(x) has bh(x) ≥ h(x)/2 • Proof – By property 4, at most h/2 red nodes on the path from the node to a leaf – Hence at least h/2 are black 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1
  • 30.
    30 Claim 2 • Thesubtree rooted at any node x contains at least 2bh(x) - 1 internal nodes 26 17 41 30 47 38 50 NIL NIL NIL NIL NIL NIL NIL NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1
  • 31.
    31 Claim 2 (cont’d) Proof:By induction on h[x] Basis: h[x] = 0  x is a leaf (NIL[T])  bh(x) = 0  # of internal nodes: 20 - 1 = 0 Inductive Hypothesis: assume it is true for h[x]=h-1 NIL x
  • 32.
    32 Claim 2 (cont’d) Inductivestep: • Prove it for h[x]=h • Let bh(x) = b, then any child y of x has: – bh (y) = – bh (y) = b (if the child is red), or b - 1 (if the child is black) 26 17 41 30 47 38 50 x y1 y2 bh = 2 bh = 2 bh = 1 NIL NIL NIL NIL NIL NIL
  • 33.
    33 Claim 2 (cont’d) •Using inductive hypothesis, the number of internal nodes for each child of x is at least: 2bh(x) - 1 - 1 • The subtree rooted at x contains at least: (2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 = 2 · (2bh(x) - 1 - 1) + 1 = 2bh(x) - 1 internal nodes x l r h h-1 bh(l)≥bh(x)-1 bh(r)≥bh(x)-1
  • 34.
    34 Height of Red-Black-Trees(cont’d) Lemma: A red-black tree with n internal nodes has height at most 2lg(n + 1). Proof: n • Add 1 to both sides and then take logs: n + 1 ≥ 2b ≥ 2h/2 lg(n + 1) ≥ h/2  h ≤ 2 lg(n + 1) root l r height(root) = h bh(root) = b number n of internal nodes ≥ 2b - 1 ≥ 2h/2 - 1 since b  h/2
  • 35.
    35 Operations on Red-Black-Trees •The non-modifying binary-search-tree operations MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR, and SEARCH run in O(h) time – They take O(lgn) time on red-black trees • What about TREE-INSERT and TREE-DELETE? – They will still run on O(lgn) – We have to guarantee that the modified tree will still be a red-black tree
  • 36.
    36 INSERT INSERT: what colorto make the new node? • Red? Let’s insert 35! – Property 4 is violated: if a node is red, then both its children are black • Black? Let’s insert 14! – Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes 26 17 41 30 47 38 50
  • 37.
    37 DELETE DELETE: what colorwas the node that was removed? Black? 1. Every node is either red or black 2. The root is black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! OK! Not OK! Could create two red nodes in a row Not OK! Could change the black heights of some nodes 26 17 41 30 47 38 50 Not OK! If removing the root and the child that replaces it is red
  • 38.
    38 Rotations • Operations forre-structuring the tree after insert and delete operations on red-black trees • Rotations take a red-black-tree and a node within the tree and: – Together with some node re-coloring they help restore the red-black-tree property – Change some of the pointer structure – Do not change the binary-search tree property • Two types of rotations: – Left & right rotations
  • 39.
    39 Left Rotations • Assumptionsfor a left rotation on a node x: – The right child of x (y) is not NIL • Idea: – Pivots around the link from x to y – Makes y the new root of the subtree – x becomes y’s left child – y’s left child becomes x’s right child
  • 40.
  • 41.
    41 LEFT-ROTATE(T, x) 1. y← right[x] ►Set y 2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree 3. if left[y]  NIL 4. then p[left[y]] ← x ► Set the parent relation from left[y] to x 5. p[y] ← p[x] ► The parent of x becomes the parent of y 6. if p[x] = NIL 7. then root[T] ← y 8. else if x = left[p[x]] 9. then left[p[x]] ← y 10. else right[p[x]] ← y 11. left[y] ← x ► Put x on y’s left 12. p[x] ← y ► y becomes x’s parent
  • 42.
    42 Right Rotations • Assumptionsfor a right rotation on a node x: – The left child of y (x) is not NIL • Idea: – Pivots around the link from y to x – Makes x the new root of the subtree – y becomes x’s right child – x’s right child becomes y’s left child
  • 43.
    43 Insertion • Goal: – Inserta new node z into a red-black-tree • Idea: – Insert node z into the tree as for an ordinary binary search tree – Color the node red – Restore the red-black-tree properties • Use an auxiliary procedure RB-INSERT-FIXUP
  • 44.
    44 RB Properties Affectedby Insert 1. Every node is either red or black 2. The root is black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! If z is the root  not OK OK! 26 17 41 47 38 50 If p(z) is red  not OK z and p(z) are both red OK!
  • 45.
    45 RB-INSERT-FIXUP – Case1 z’s “uncle” (y) is red Idea: (z is a right child) • p[p[z]] (z’s grandparent) must be black: z and p[z] are both red • Color p[z] black • Color y black • Color p[p[z]] red • z = p[p[z]] – Push the “red” violation up the tree
  • 46.
    46 RB-INSERT-FIXUP – Case1 z’s “uncle” (y) is red Idea: (z is a left child) • p[p[z]] (z’s grandparent) must be black: z and p[z] are both red • color p[z]  black • color y  black • color p[p[z]]  red • z = p[p[z]] – Push the “red” violation up the tree
  • 47.
    47 RB-INSERT-FIXUP – Case3 Case 3: • z’s “uncle” (y) is black • z is a left child Case 3 Idea: • color p[z]  black • color p[p[z]]  red • RIGHT-ROTATE(T, p[p[z]]) • No longer have 2 reds in a row • p[z] is now black
  • 48.
    48 RB-INSERT-FIXUP – Case2 Case 2: • z’s “uncle” (y) is black • z is a right child Idea: • z  p[z] • LEFT-ROTATE(T, z)  now z is a left child, and both z and p[z] are red  case 3 Case 2 Case 3
  • 49.
    49 RB-INSERT-FIXUP(T, z) 1. whilecolor[p[z]] = RED 2. do if p[z] = left[p[p[z]]] 3. then y ← right[p[p[z]]] 4. if color[y] = RED 5. then Case1 6. else if z = right[p[z]] 7. then Case2 8. Case3 9. else (same as then clause with “right” and “left” exchanged) 10. color[root[T]] ← BLACK The while loop repeats only when case1 is executed: O(lgn) times Set the value of x’s “uncle” We just inserted the root, or The red violation reached the root
  • 50.
    50 Example 11 Insert 4 2 14 115 7 8 5 4 y 11 2 14 1 15 7 8 5 4 z Case 1 y z and p[z] are both red z’s uncle y is red z z and p[z] are both red z’s uncle y is black z is a right child Case 2 11 2 14 1 15 7 8 5 4 z y Case 3 z and p[z] are red z’s uncle y is black z is a left child 11 2 14 1 15 7 8 5 4 z
  • 51.
    51 RB-INSERT(T, z) 1. y← NIL 2. x ← root[T] 3. while x  NIL 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8. p[z] ← y • Initialize nodes x and y • Throughout the algorithm y points to the parent of x • Go down the tree until reaching a leaf • At that point y is the parent of the node to be inserted • Sets the parent of z to be y 26 17 41 30 47 38 50
  • 52.
    52 RB-INSERT(T, z) 9. ify = NIL 10. then root[T] ← z 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z 14. left[z] ← NIL 15. right[z] ← NIL 16. color[z] ← RED 17. RB-INSERT-FIXUP(T, z) The tree was empty: set the new node to be the root Otherwise, set z to be the left or right child of y, depending on whether the inserted node is smaller or larger than y’s key Set the fields of the newly added node Fix any inconsistencies that could have been introduced by adding this new red node 26 17 41 30 47 38 50
  • 53.
    53 Analysis of RB-INSERT •Inserting the new element into the tree O(lgn) • RB-INSERT-FIXUP – The while loop repeats only if CASE 1 is executed – The number of times the while loop can be executed is O(lgn) • Total running time of RB-INSERT: O(lgn)
  • 54.
    54 Red-Black Trees -Summary • Operations on red-black-trees: – SEARCH O(h) – PREDECESSOR O(h) – SUCCESOR O(h) – MINIMUM O(h) – MAXIMUM O(h) – INSERT O(h) – DELETE O(h) • Red-black-trees guarantee that the height of the tree will be O(lgn)
  • 55.
    55 Problems • What isthe ratio between the longest path and the shortest path in a red-black tree? - The shortest path is at least bh(root) - The longest path is equal to h(root) - We know that h(root)≤2bh(root) - Therefore, the ratio is ≤2
  • 56.
    56 Problems • What red-blacktree property is violated in the tree below? How would you restore the red-black tree property in this case? – Property violated: if a node is red, both its children are black – Fixup: color 7 black, 11 red, then right-rotate around 11 11 2 14 1 15 7 8 5 4 z
  • 57.
    57 Problems • Let a,b, c be arbitrary nodes in subtrees , ,  in the tree below. How do the depths of a, b, c change when a left rotation is performed on node x? – a: increases by 1 – b: stays the same – c: decreases by 1
  • 58.
    58 Problems • When weinsert a node into a red-black tree, we initially set the color of the new node to red. Why didn’t we choose to set the color to black? • (Exercise 13.4-7, page 294) Would inserting a new node to a red-black tree and then immediately deleting it, change the tree?