Locationaware heapbased PQ - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Locationaware heapbased PQ

Description:

Location-aware heap-based PQ. Should a position be. fixed with respect to the tree? ... regardless of where it ends up in the heap structure. public class LAPQ ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 8
Provided by: CSBR
Category:

less

Transcript and Presenter's Notes

Title: Locationaware heapbased PQ


1
Location-aware heap-based PQ
  • Should a position be
  • fixed with respect to the tree?
  • the container for a specific key-value pair?
  • GT use first option
  • The following implementation uses second option.
  • Guarantees that
  • Position p myPQ.insert(entry1)
  • myPq.insert(entry2)
  • myPQ.insert(entry3)
  • myPQ.delete(p)
  • deletes the entry1, regardless of where it ends
    up in the heap structure.

2
  • public class LAPQ
  • public class EntryltK, Vgt
  • K _key
  • V _value
  • public Entry(K key, V value)

3
  • public class LAPQ
  • public class Entry
  • int _key Object _value
  • public Entry(int key, Object value)
  • _key key _value value
  • // omitted getKey, getValue,
  • private class Node
  • private Entry _entry
  • private Node _lChild Null, _rChild Null
  • private Node _parent Null
  • // omitted constructor, setLChild, setRChild,
  • // setParent, hasLeft, hasRight, amRightChild,
    etc.
  • public Entry getEntry() return _entry
  • public boolean amLeftChild()
  • if (_parent null) return false
  • if (!parent.hasLeft) return false
  • return (parent.left this)

4
  • public class LAPQ
  • ... (declarations of Entry, Node, Position) ...
  • Node _root null
  • Node _lastNode null
  • int _count 0 // keep track of number of
    entries
  • // Constructor do nothing.
  • public Position insert(int k, Object v)
  • count // Need to decrement in delete and
    extractMin
  • Entry e new Entry(k, v)
  • if (_root null)
  • root new Node(e)
  • return new Position(_root)
  • else
  • Node n insertNext(e) // put into next avail
    spot
  • percolateUp(n)
  • return new Position(n)

5
  • public class LAPQ
  • ... (declarations of Entry, Node, Position) ...
  • private percolateUp(Node n)
  • while(n.hasParent() n.parent().getKey() gt
    n.getKey())
  • swap(n, n.parent())
  • private swap(n1, n2)
  • Node t
  • t n1._parent n1._parent n2._parent
    n2._parent t
  • t n1._leftChild n1._leftChild
    n2._leftChild
  • n2._leftChile t
  • ltsame for _rightChildgt
  • if (_lastNode n1)
  • _lastNode n2
  • else if (_lastNode n2)
  • _lastNode n1)

6
  • public class LAPQ
  • ... (declarations of Entry, Node, Position) ...
  • private Node insertNext(Entry e)
  • _lastNode buildNewNode()
  • n.setEntry(e)
  • _lastNode n
  • return n
  • private Node buildNewNode()
  • find place to insert next node in heap,
  • create an empty node, insert it there, and
    return it

7
  • public class LAPQ
  • ...
  • private replaceValue(Position p, Value v)
  • p._node.setValue(v)
  • private replaceKey(Position p, Key k)
  • Node n p._node
  • n.setKey(k) // may be out of order!
  • if (n _root)
  • percDown(n)
  • else if (n.getParent().getKey gt n.getKey)
  • percUp(n)
  • else
  • percDown(n)
Write a Comment
User Comments (0)
About PowerShow.com