CS 130a DISCUSSION SESSION 3 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS 130a DISCUSSION SESSION 3

Description:

each of the two inputs are heaps with 1 root each. Binomial Heap - Merge. Binomial Heap - Merge ... trees in the heap? Binomial Heaps - Delete. Change the ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 31
Provided by: lxu3
Category:

less

Transcript and Presenter's Notes

Title: CS 130a DISCUSSION SESSION 3


1
CS 130a - DISCUSSION SESSION 3
Shravan Samindla
2
Binomial Heap
  • Set of binomial trees
  • Each tree follows heap-order property (min-heap
    or max-heap)?
  • A binomial tree of order k
  • has root which has k children
  • children are binomial trees of order 0, 1, ...,
    k-1
  • has 2k nodes
  • Can think of as binary representation of N
  • 1 represents existence of corresponding binomial
    tree

3
Binomial Heap (contd...)?
  • Each binomial tree obeys heap property
  • there can be at-most one binomial tree for each
    order

Why binomial?
4
Binomial Heap Operations
  • Insert
  • Find-Min
  • Delete-Min
  • Decrease-key
  • Delete
  • Merge

5
Binomial Heap - Implementation
  • array/linked-list of root nodes
  • 'parent' 'left-child' 'right-sibling'
  • for root node, 'parent' points to...
  • In project, for each node we also have a pointer
    to the largest element in the sub-tree rooted at
    that node.

6
structure
7
Example
8
Binomial Heap - properties
9
Binomial Heap - Merge
  • 2 cases
  • both are binomial trees of order k
  • each of the two inputs are heaps with gt 1 root
    each.

10
Binomial Heap - Merge
11
Binomial Heap - Merge
12
Binomial Heap - Merge
13
Binomial Heap - Merge
14
Binomial Heap - Merge
15
Binomial Heap - Insert
  • Create a heap with one node of element to be
    inserted.
  • Merge this new heap with the heap that the
    element is to be inserted into
  • Whats the running time of insert, merge?

16
Binomial Heap - Merge
  • A sequence of N inserts takes linear time
  • Why?
  • Note that
  • if N ....0 gt takes 1 step
  • if N ...01 gt takes 2 step
  • and so on...until log(N) steps

17
Binomial Heap - DeleteMin
18
Binomial Heap - DeleteMin
so whats the running time of deleteMin?
19
Binomial Heap - decreaseKey
will it affect other binomial trees in the heap?
20
Binomial Heaps - Delete
  • Change the value to -?
  • percolate up the tree
  • deleteMin
  • So, whats the characteristic of binomial heaps
    w.r.t running times?

21
In the project...
  • When deleting an element
  • incoming pointers can be
  • 'parent' of children
  • 'largest' of other nodes in the subtree
  • make sure to handle these correctly

22
Red-Black Trees
  • A search tree with basically
  • black node count to the leaves being the same
  • children of red node must be black

23
Red-Black Trees - Insertion
  • Basic Idea
  • Preserve rank property
  • preserve search tree property
  • Three cases
  • parent is black
  • parent is red
  • parent's sibling is black
  • parent's sibling is red

24
Red-Black Trees - Insertion
25
Red-Black Trees - Insertion
what about case 3?
26
Red Black Trees - Insertion
  • Why insert a node as red?
  • Why perform rotations, not just color change?
  • Can the grand-parent be red?
  • What if the great-grand-parent is red?
  • What's the difference in the result in case of
    rotation and double-rotation?

27
Red-Black Trees - Insertion
28
Red-Black Trees - Insertion
29
Red Black Trees - Deletion
  • Reduce all to the following form
  • root is red both children are black
  • use color flip,totation if necessary
  • If one of the children is red, then root has to
    be black
  • by appropriate rotation make sure a red node is
    on the path we are interested in

30
THANK YOU
Write a Comment
User Comments (0)
About PowerShow.com