Docsity

Extending Data Structures: Implementing Dynamic Order Statistics using Red/Black Trees - P, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS673-2008F-08 Extending Data Structures 1
08-0: Dynamic Order Statistics
•Data structure with following operations:
•Insert / Remove / Find Θ(lg n)
•Find nth smallest element
•Find the rank of any element (is it smallest, second smallest, etc)
How can we do this with red/black trees?
•How to find the rank of any element in a red/black tree
•How to find the nth element in a red/black tree
08-1: Dynamic Order Statistics
•Addinging functionality to red/black trees
•Finding the nth element in a red/black tree
•Finding the rank of any element in a red/black tree
•What if we could add some other data to a red/black tree, to make this easier?
•What should we add?
•How could we use it?
08-2: Size Field
•Add a “size” field to each node in a red/black tree
•How can we use this size field to find the nth element in the tree?
•How can we use this size field to find the rank of anyelement?
•Can we maintain the size field, and still have insert/remove/find take time O(lg n)?
08-3: Using Size Field
•To find the kth element in a red/black tree with size field:
•If # of elements in left subtree =k−1, then root is the kth element
•if # of elements in left subtree > k −1, then the kth element is the kth element in the left subtree
•if # of elements in the left subtree =n < k −1, then the kth element is the (n−(k+ 1))th element in the
right subtree
08-4: Updating Size: Insert
•Updating size field on insert:
08-5: Updating Size: Insert
•Updating size field on insert:
•As we go down the tree looking for the correct place to insert an element, add one to the size field of every
node along the path from the root to the inserted element
•(examples on board)
pf3
pf4
pf5

Partial preview of the text

Download Extending Data Structures: Implementing Dynamic Order Statistics using Red/Black Trees - P and more Study notes Algorithms and Programming in PDF only on Docsity!

08-0: Dynamic Order Statistics

  • Data structure with following operations:
    • Insert / Remove / Find Θ(lg n)
    • Find nth smallest element
    • Find the rank of any element (is it smallest, second smallest, etc)

How can we do this with red/black trees?

  • How to find the rank of any element in a red/black tree
  • How to find the nth element in a red/black tree

08-1: Dynamic Order Statistics

  • Addinging functionality to red/black trees
    • Finding the nth element in a red/black tree
    • Finding the rank of any element in a red/black tree
  • What if we could add some other data to a red/black tree, to make this easier?
    • What should we add?
    • How could we use it?

08-2: Size Field

  • Add a “size” field to each node in a red/black tree
    • How can we use this size field to find the nth element in the tree?
    • How can we use this size field to find the rank of any element?
    • Can we maintain the size field, and still have insert/remove/find take time O(lg n)?

08-3: Using Size Field

  • To find the kth element in a red/black tree with size field:
    • If # of elements in left subtree = k − 1 , then root is the kth element
    • if # of elements in left subtree > k − 1 , then the kth element is the kth element in the left subtree
    • if # of elements in the left subtree = n < k − 1 , then the kth element is the (n − (k + 1))th element in the right subtree

08-4: Updating Size: Insert

  • Updating size field on insert:

08-5: Updating Size: Insert

  • Updating size field on insert:
    • As we go down the tree looking for the correct place to insert an element, add one to the size field of every node along the path from the root to the inserted element - (examples on board)

08-6: Updating Size: Delete

  • Updating size field on delete:

08-7: Updating Size: Delete

  • Updating size field on delete:
    • As we go down the tree looking for the element to delete, delete one from the size of evey node along the path from the root to the deleted element - (examples on board)
    • Need to be careful about trying to delete elements that aren’t in the tree

08-8: Updating Size Field: Rotate

  • Updating size on rotations
    • How should sizes be updated on rotations?
      • (Picture on board)

08-9: Updating Size Field: Rotate

Y

X (^) C

A B

Y

X

C

A

B

Right Rotate

Sizes of A,B,C not changed Size(Y) = Size(B) + Size(C) Size(X) = Size(A) + Size(Y)

Sizes of A,B,C not changed Size(X) = Size(A) + Size(B) Size(Y) = Size(X) + Size(C)

Left Rotate

08-10: Augmenting Data Structures

  • Decide what extra information to add to each element of the data structure
  • Make sure we can update this extra information for each operation on the data structure
  • Add operations that use this extra information
    • New operations
    • Do old operations more efficiently

(Finding rank example) 08-11: Augmenting Data Structures

  • For Red/Black trees:
    • If extra information in a node is dependent only on the node itself, and values in left & right children
    • Then, we can always update this information during insert and delete in time O(lg n)
  • Three cases
    • i and i′^ overlap (including one is a subset of the other)
    • i is to the left of i′
    • i is to the right if i′

08-17: Intervals

  • Data structure that manipulates intervals
    • Insert a closed interval (using endpoints)
    • Delete a closed interval (using endpoints)
    • Find an interval
      • Given an interval i, find an interval that overlaps i
      • If > 1 interval overlaps i, return one arbitrarily

08-18: Interval Trees

  • First Try:
    • Each node stores low/high endpoint of interval
    • Sorted based on low endpoint
    • No extra information (yet!)

08-19: Interval Trees

0 2 10

[9,14]

[4,7] [11,12]

4 6 8 12 14 16

[3,6] [5,6]

[0,1]

[10,13] [15,17]

[14,16]

08-20: Interval Trees

  • Data Structure
    • Each node stores low/high endpoint of interval
    • Sorted based on low endpoint
    • Extra information: Maximum value of any interval stored in the subtree rooted at this node

08-21: Interval Trees

0 2 10

[9,14]

[4,7] [11,12]

4 6 8 12 14 16

[3,6]

17

7 17

[5,6]

6 6

[0,1]

1

[10,13] [15,17]

13 17

[14,16]

16

08-22: Interval Trees

  • Extra Information:
    • Maximum value of any interval stored in the subtree
  • Can we maintain this extra information through inserts and deletes?

08-23: Interval Trees

  • Extra Information:
    • Maximum value of any interval stored in the subtree
  • Can we maintain this extra information through inserts and deletes?
    • Yes: Maximum value in a subtree only depends upon:
      • High endpoint stored in current node
      • Maximum value in left subtree
      • Maximum value in right subtree

(Example updates on board) 08-24: Interval Trees

  • How do we find an interval?
    • Given an interval i, find an interval that overlaps i.
    • If > 1 interval overlaps i, return any one of the intervals that overlap i

(examples in next slide) 08-25: Interval Trees