Algorithms - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Algorithms

Description:

CSIE NCYU, Fall 2006. 3. Binary Search Tree. Binary-search-tree property ... i and i' satisfy the interval trichotomy; that exactly one of the following ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 74
Provided by: webNc9
Category:

less

Transcript and Presenter's Notes

Title: Algorithms


1
Algorithms
  • Unit 7. Searching in Trees
  • Chih-Hung Wang
  • Binary Search Tree
  • Red-Black Tree
  • Augmenting Data Structures

2
  • Binary Search Tree

3
Binary 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.

4
Example of Binary Search Tree
5
Inorder-Tree-Walk
6
Theorem
  • 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

7
Query a Binary Tree
8
Tree Search Algorithm
9
Iterative-Tree-Search
10
Tree Minimum
11
Tree Maximum
12
Tree Successor
13
Theorem
  • 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.

14
Tree Insertion
15
Example of Tree Insertion
16
Tree Deletion
Case 3 (y?z)
Case 2 Case 3
z is the root
17
Case 1 z has no children
y
x NIL
18
Case 2 z has only a single child
y
x
px?py
19
Case 3 z has two children
keyz?keyy
px?py
x
20
Theorem
  • 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
  • Red-Black Tree

22
Property
  • 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.

23
Example (1)
24
Example (2)
25
Lemma
  • 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).

26
Rotations
27
Left Rotation Algorithm
28
More 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.

29
Example of Rotations
30
Time Evaluation of Rotations
  • Time O(1) for both LEFT-ROTATE and RIGHT-ROTATE,
    since a constant number of pointers are modified.

31
Insertion
  • Start by doing regular binary-search-tree
    insertion

32
Insertion - 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.

33
Property 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)

34
Insertion - Fixup
35
Three 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

36
The Operation of RB-INSERT-FIXUP (1)
37
The Operation of RB-INSERT-FIXUP (2)
38
Case 1 of RB Insertion
39
Case 2 and Case 3 of RB Insertion
40
Analysis 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.

41
Deletion
42
Property 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.

43
Deletion- FIXUP
44
Four 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

45
Case 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.

46
Case 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.

47
Case 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.

48
Case 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.

49
Analysis 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

51
Dynamic 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.

52
Dynamic 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

53
An order-statistic Tree
54
Retrieving an element with a given rank
55
Analysis
  • 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.

56
Determining the rank of an element
57
Example
  • Find the rank of the node with key 38
  • Iteration keyy r
  • 38 2
  • 30 4
  • 41 4
  • 26 17
  • Return 17

58
Analysis
  • Analysis y goes up one level in each iteration ?
    O(lg n) time.

59
Maintaining 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

60
Updating subtree sizes during rotations
61
How 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.

62
Augmenting 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.

63
Augmenting 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.

64
Interval 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)

65
Interval Trees
66
Data 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.

67
Operations
  • 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.

68
Data Structure(1)
69
Data Structure (2)
70
Maintaining 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.

71
New OperationInterval Search
72
Example
  • Successful Search
  • i22, 25
  • 8, 9 -gt 15, 23 (answer)
  • Unsuccessful Search
  • i 11, 14
  • 8, 9 -gt 15, 23 -gt nilT

73
Theorem
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com