Title: Algorithms
1Algorithms
- Unit 7. Searching in Trees
- Chih-Hung Wang
- Binary Search Tree
- Red-Black Tree
- Augmenting Data Structures
2 3Binary Search Tree
- Binary-search-tree property
- If y is in left subtree of x, then keyy
keyx. - If y is in right subtree of x, then keyy
keyx.
4Example of Binary Search Tree
5Inorder-Tree-Walk
6Theorem
- Theorem 12.1
- If x is the root of an n-node subtree, then the
call INORDER-TREE-WALK(x) takes ?(n) time. - Preorder tree walk
- Postorder tree walk
7Query a Binary Tree
8Tree Search Algorithm
9Iterative-Tree-Search
10Tree Minimum
11Tree Maximum
12Tree Successor
13Theorem
- Theorem 12.2
- The dynamic-set operations, SEARCH, MINIMUM,
MAXIMUM, SUCCESSOR, and PREDECESSOR can be made
to run in O(h) time on a binary search tree of
height h.
14Tree Insertion
15Example of Tree Insertion
16Tree Deletion
Case 3 (y?z)
Case 2 Case 3
z is the root
17Case 1 z has no children
y
x NIL
18Case 2 z has only a single child
y
x
px?py
19Case 3 z has two children
keyz?keyy
px?py
x
20Theorem
- Theorem 12.3
- The dynamic-set operations, INSERT and DELETE can
be made to run in O(h) time on a binary search
tree of height h.
21 22Property
- A red-black tree is a binary search tree 1 bit
per node an attribute color, which is either red
or black. - Approximate balanced
- Every node is either red or black.
- The root is black.
- Every leaf (NILT ) is black.
- If a node is red, then both its children are
black. (Hence no two reds in a row on a simple
path from the root to a leaf.) - For each node, all paths from the node to
descendant leaves contain the same number of
black nodes.
23Example (1)
24Example (2)
25Lemma
- Lemma 13.1
- A red-black tree with n internal nodes has height
at most 2 lg(n1). - Proof
- Let h and b be the height and black-height of the
root, respectively. - n 2b - 1 2h/2 - 1 .
- Adding 1 to both sides and then taking logs gives
lg(n 1) h/2, which implies - that h 2 lg(n 1).
26Rotations
27Left Rotation Algorithm
28More for Rotations
- The pseudocode for LEFT-ROTATE assumes that
- rightx ? nilT , and
- roots parent is nilT .
- Pseudocode for RIGHT-ROTATE is symmetric
exchange left and right everywhere.
29Example of Rotations
30Time Evaluation of Rotations
- Time O(1) for both LEFT-ROTATE and RIGHT-ROTATE,
since a constant number of pointers are modified.
31Insertion
- Start by doing regular binary-search-tree
insertion
32Insertion - Fixup
- RB-INSERT ends by coloring the new node z red.
- Then it calls RB-INSERT-FIXUP because we could
have violated a red-black property.
33Property Violation on Insertion
- Which property might be violated?
- If z is the root, then theres a violation.
(Property 2) - If pz is red, theres a violation both z and
pz are red. (Property 4)
34Insertion - Fixup
35Three Cases
- Case 1 zs uncle y is red
- Case 2 Zs uncle y is black and z is a right
child - Case 3 Zs uncle y is black and z is a left child
36The Operation of RB-INSERT-FIXUP (1)
37The Operation of RB-INSERT-FIXUP (2)
38Case 1 of RB Insertion
39Case 2 and Case 3 of RB Insertion
40Analysis of RB Insertion
- O(lg n) time to get through RB-INSERT up to the
call of RB-INSERT-FIXUP. - Within RB-INSERT-FIXUP
- Each iteration takes O(1) time.
- Each iteration is either the last one or it moves
z up 2 levels. - O(lg n) levels? O(lg n) time.
- Also note that there are at most 2 rotations
overall. - Thus, insertion into a red-black tree takes O(lg
n) time.
41Deletion
42Property Violation on Deletion
- If y is black, we could have violations of
red-black properties - P2. If y is the root and x is red, then the root
has become red. - P4. Violation if py and x are both red.
- P5. Any path containing y now has 1 fewer black
node.
43Deletion- FIXUP
44Four Cases
- Case 1 xs sibling w is red
- Case 2 xs sibling w is black, and both of ws
children are black - Case 3 xs sibling w is black, ws left child is
red, and ws right child is black - Case 4 xs sibling w is black, and ws right
child is red
45Case 1 of Deletion
- w must have black children.
- Make w black and px red.
- Then left rotate on px.
- New sibling of x was a child of w before rotation
?must be black. - Go immediately to case 2, 3, or 4.
46Case 2 of Deletion
- Take 1 black off x (?singly black) and off w
(?red). - Move that black to px.
- Do the next iteration with px as the new x.
- If entered this case from case 1, then px was
red - ? new x is red black
- ? color attribute of new x is RED ? loop
terminates. - Then new x is made black in the last line.
47Case 3 of Deletion
- Make w red and ws left child black.
- Then right rotate on w.
- New sibling w of x is black with a red right
child ?case 4.
48Case 4 of Deletion
- Make w be pxs color (c).
- Make px black and ws right child black.
- Then left rotate on px.
- Remove the extra black on x (? x is now singly
black) without violating any red-black
properties. - All done. Setting x to root causes the loop to
terminate.
49Analysis of Deletion
- O(lg n) time to get through RB-DELETE up to the
call of RB-DELETE-FIXUP. - Within RB-DELETE-FIXUP
- Case 2 is the only case in which more iterations
occur. - x moves up 1 level.
- Hence, O(lg n) iterations.
- Each of cases 1, 3, and 4 has 1 rotation?3
rotations in all. - Hence, O(lg n) time.
50- Augmenting Data Structures
51Dynamic Order Statistics
- We shall also see the rank of an element?its
position in the linear order of the set?can
likewise be determined in O(lg n) time.
52Dynamic Order Statistics
- Beside the usual red-black tree fields keyx,
colorx, px, leftx, and rightx in a node
x, we have another field sizex. This field
contains the number of (internal) nodes in the
subtree rooted at x (including x itself), that
is, the size of the subtree. - If we define the sentinels size to be 0, that
is, we set sizenilT to be 0, then we have the
identity - sizex sizeleftx sizerightx 1
53An order-statistic Tree
54Retrieving an element with a given rank
55Analysis
- Analysis Each recursive call goes down one
level. Since R-B tree has O(lg n) levels, have
O(lg n) calls? O(lg n) time.
56Determining the rank of an element
57Example
- Find the rank of the node with key 38
- Iteration keyy r
- 38 2
- 30 4
- 41 4
- 26 17
- Return 17
58Analysis
- Analysis y goes up one level in each iteration ?
O(lg n) time.
59Maintaining subtree size
- Referring to the code for LEFT-ROTATE(T, x) in
section 13.2 we add the following lines - 12 sizey ? sizex
- 13 sizex ? sizeleftx sizerightx 1
60Updating subtree sizes during rotations
61How to augment a data structure
- 1.Choosing an underlying data structure,
- 2.Determining additional information to be
maintained in the underlying data structure, - 3.Verifying that the additional information can
be maintained for the basic modifying operations
on the underlying data structure, and - 4.Developing new operations.
62Augmenting red-black trees
- Theorem 14.1
- Augment a R-B tree with field f , where f x
depends only on information in x, leftx, and
rightx (including f leftx and f
rightx). Then can maintain values of f in all
nodes during insert and delete without affecting
O(lg n) performance.
63Augmenting red-black trees
- Proof
- Since f x depends only on x and its children,
when we alter information in x, changes propagate
only upward (to px, ppx, . . . , root). - Height O(lg n)? O(lg n) updates, at O(1) each.
64Interval Trees
- We can represent an intervalt1,t2 as an object
i, with fields lowi t1 (the low endpoint) and
highi t2 (the high endpoint). We say that
intervals i and i overlap if i ? i ? Ø, that
is, if lowi highi and lowi highi.
Any two intervals i and i satisfy the interval
trichotomy that exactly one of the following
three properties holds - a. i and i overlap,
- b. i is to the left of i (i.e., highi lt
lowi), - c. i is to the right of i (i.e., highi
lt lowi)
65Interval Trees
66Data Structure of Interval Trees
- An interval tree is a red-black tree that
maintains a dynamic set of elements, with each
element x containing an interval intx.
67Operations
- INTERVAL-INSERT(T,x)
- Add the element x in T
- INTERVAL-DELETE(T,x)
- Remove the element x in T
- INTERVAL-SEARCH(T,i)
- Returns a pointer to an element x in T such that
intx overlaps interval i, or the sentinel
nilT if no such element is in the set.
68Data Structure(1)
69Data Structure (2)
70Maintaining the Information
- Maintaining the information
- We determine maxx given interval intx and the
max values of node xs children - maxx max(highintx, maxleftx,
maxrightx) - Thus, by Theorem 14.1, insertion and deletion run
in O(lg n) time.
71New OperationInterval Search
72Example
- Successful Search
- i22, 25
- 8, 9 -gt 15, 23 (answer)
- Unsuccessful Search
- i 11, 14
- 8, 9 -gt 15, 23 -gt nilT
73Theorem
- Theorem 14.2
- Any execution of INTERVAL-SEARCH(T,i) either
returns a node whose interval overlaps i, or it
returns nilT and the tree T contain no node
whose interval overlaps i.