Title: CS 130a DISCUSSION SESSION 3
1CS 130a - DISCUSSION SESSION 3
Shravan Samindla
2Binomial 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
3Binomial Heap (contd...)?
- Each binomial tree obeys heap property
- there can be at-most one binomial tree for each
order
Why binomial?
4Binomial Heap Operations
- Insert
- Find-Min
- Delete-Min
- Decrease-key
- Delete
- Merge
5Binomial 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.
6structure
7Example
8Binomial Heap - properties
9Binomial Heap - Merge
- 2 cases
- both are binomial trees of order k
- each of the two inputs are heaps with gt 1 root
each.
10Binomial Heap - Merge
11Binomial Heap - Merge
12Binomial Heap - Merge
13Binomial Heap - Merge
14Binomial Heap - Merge
15Binomial 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?
16Binomial 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
17Binomial Heap - DeleteMin
18Binomial Heap - DeleteMin
so whats the running time of deleteMin?
19Binomial Heap - decreaseKey
will it affect other binomial trees in the heap?
20Binomial Heaps - Delete
- Change the value to -?
- percolate up the tree
- deleteMin
- So, whats the characteristic of binomial heaps
w.r.t running times?
21In 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
22Red-Black Trees
- A search tree with basically
- black node count to the leaves being the same
- children of red node must be black
23Red-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
24Red-Black Trees - Insertion
25Red-Black Trees - Insertion
what about case 3?
26Red 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?
27Red-Black Trees - Insertion
28Red-Black Trees - Insertion
29Red 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
30THANK YOU