PartiallyOrdered Trees POT, and Priority Queues - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

PartiallyOrdered Trees POT, and Priority Queues

Description:

Partially Ordered Trees (POT) ... POT is an ADT, defined with only ... max-index of non-leaf node. 11/11/09. DS - Partially Ordered Binary Trees. 21. Build Heap ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 31
Provided by: yon5
Category:

less

Transcript and Presenter's Notes

Title: PartiallyOrdered Trees POT, and Priority Queues


1
Partially-Ordered Trees (POT),and Priority
Queues
2
Dynamic Sets Priority Queues
  • Priority queues - are a common abstract data
    type -
  • a dynamic set of elements, where each element is
    associated with some priority. Elements are
    entered into the queue as they come, but are
    removed from the queue according to their
    priority.
  • Operations
  • insert an element to the set
  • delete the element with maximal (minimal)
    priority.

3
Available Data Structures
  • Queue implemented with an Array
  • Not dynamic
  • Queue implemented as a linked list
  • Unordered delete-max (search element with max
    priority) is O(n)
  • Ordered Insert is O(n)
  • Queue implemented as a Binary Search Tree (BST)
  • Average time for operations O( lg(n)). But tree
    could become unbalanced.
  • Define a new abstract data type (ADT).
  • Need less operations than BST, so maybe can have
    O(lg n) without the balancing problem.

4
Partially Ordered Trees (POT)
  • A data structure with efficient algorithms to
    deal with priority queues.
  • Definition of POT
  • A labeled, binary tree
  • label contains a priority field (a field with
    total order)
  • for every node n in POT
  • n.priority gt n.left.priority of all nodes in
    n.left-subtree (n.left)
  • n.priority gt n.right.priority of all nodes in
    n.right-subtree (n.right)

gt
lt
The priority of the root is always the maximal
among all nodes in the tree, recursively.
5
Partially Ordered Trees
ex
19
12
9
8
7
6
3
5
2
4
1
No ordering between left and right subtrees.
6
Balanced POT
  • A POT is balanced if it is a complete tree

19
19
12
12
9
9
8
7
8
6
10
6
3
7
3
5
2
4
1
4
3 2 5 1
unbalanced POT
balanced POT
7
Operations on Balanced POT
  • Operations on balanced POT, which maintain both
    the balanced and the POT properties are both
    simple and fast
  • Insert -
  • Insert at the bottom of the tree, maintaining the
    completeproperty.
  • Restore the POT property using bubble-up.
  • Delete-Max -
  • Delete the root (node with maximal priority)
  • Replace (the root) with the last element in tree
    (to maintain completeness)
  • Restore the POT property using bubble-down.

bubble-up
bubble-down
insert
Delete-max
8
Reminder Representing a Binary Tree
parent
data
public class TreeNode private Object
data private TreeNode left private TreeNode
right private TreeNode parent ..
left
right
9
Operations on Balanced POT
void bubble-up (Node n) if n.parent
null or n.key lt n.parent.key return
else swap node n with n.parent bubble-up(n.p
arent)
void bubble-down (Node n) If (n.left
n.right null) return Node m max
(n.left.key, n.right.key) if n.key gt
m.key return else swap nodes n,
m bubble-down (m)
10
Running Time
  • Running time of bubble-down and bubble-up (insert
    and delete-max)

T(n) O(h), h O(lg(n))
11
ADT Priority Queues
  • POT is an ADT, defined with only 2 basic
    operations
  • Insert - equivalent to enqueue of regular queues
  • Delete-max (min) - equivalent to dequeue of
    regular queues
  • Like in regular queues,
  • dequeue from head of queue,
  • but
  • enqueue by priority.

12
Heap An array, viewed as a
complete binary tree
13
The Full Binary Tree Theorem
The number of leaves in a non-empty, full
binary tree is one more than the number of
internal nodes
Proof induction on the of internal
nodes.
14
POT and Heap
  • Since a complete tree is either a full tree or
    full tree with one node having a single son,
  • Then, in a complete tree either
    -of-leaves -of-internals 1 or
    -of-leaves -of-internals
  • Since a POT is a complete tree, the first
    non-leaf node is at n/2

20
20
21
21
15
Balanced POT and Heaps (cont)
  • Balanced POT can be implemented using an array
    data structure called Heap.
  • A0 is not used.
  • root of the POT stored in A1
  • the nodes of tree are stored a level at a time

0 1 2 3 4 5 6 7
8 9 10
19 12 9 10 8 6 7 3 2
5 1 4
A
19
1
12
A0
9
Ai/2
3
8
10
6
2
7
Ai
4
5
6
7
A2i1
root
12
3 2 5 1
4
A2i
8
9
10
11
n/2
16
Heap Array Representation of a POT
left(i) return 2i right(i) return
2i1 parent(i) return i/2
left(i) and right(i) can be computed by a simple
1-bit right or left shift.
17
Operations on HeapsHeapify (bubble-down on an
array)
heapify (A, i) // it is assumed that T1 and T2
are already heaps l left (i) //
indexes of arrays used as pointers r
right (i) if l lt heap-sizeA and Al gt
Ai largest l else largest i
if r lt heap-size A and Ar gt Alargest
largest r if largest ! i exchange
Ai with Alargest heapify A, largest
// recursively correct subtree
Ai
T1
T2
T1 and T2 are Heaps. Ai is possibly smaller
then root of T1 or root of T2. Correct it
recursively.
T(n) O(h) O(lg n)
18
Heapify
heapify (A, 2)
heapify (A, 4)
19
19
10
1
17
17
8
16
8
16
10
1
7
7
4
4
3 2 5 6
3 2 5 6
heapify (A, 8)
19
10
17
8
3
16
7
4
1 2 5 6
19
Heapifying an Array
  • Given an (unordered) array A, we can transform
    the array into a heap.
  • This operation is called heapifying the array.
  • To heapify an array, use heapify recursively.

20
Build Heap
4
1
. use heapify recursively, bottom up.
3
16
9
2
10
i
1
14 8 7
Build-heap (A) heap-sizeA lengthA
for j length A / 2 downto 1
heapify(A, j)
4
1
3
16
i
9
2
10
2
max-index of non-leaf node
14 8 7
4
4
1
3
i
i
16
9
14
1
10
10
16
14
9
3
2 8 7
3
4
2 8 7
21
Build Heap
i
4
16
16
10
14
10
7
9
14
3
7
9
8
3
2 8 1
2 4 1
5
6
22
Heapsort Using the Heap Data Structure To Sort
an Array
- transform array A into a heap
(buildHeap) - swap A1 with Ak -
restore the heap property (Heapify)
unsorted
A
BuildHeap
heap
A
delete-max
delete-max heapify
heap
sorted
A
n
1
k
23
HeapSort
Sorting an array, with no extra space, in O (n
lg n)
Heapsort (A) build-heap(A) for k lengthA
downto 2 exchange A1 with Ak heapsizeA
heapsizeA - 1 heapify (A,1)
Sorting a heap
Delete-max
24
heapsort
16
14
14
10
8
10
7
9
8
3
7
9
4
3
2 4 1
2 1
16
10
9
8
9
8
7
3
1
4
3
7
1
4
2
2
14 16
10 14 16
25
heapsort
8
7
1
3
2
...
1
4
2 3
9
4 7 8 9
10 14 16
10 14 16
1 2 3 4 7 8 9 10 14 16
A
26
Running time of buildheap
  • n/2 Theapify(n) or (n/2 h) or (n/2 lg n)
    thus nlgn
  • Performing a closer evaluatation Tbuildheap(n)
    O(n)

27
Running time of HeapSort
T heapsort(n) Tbuildheap(n) n
Theapify(n) O(n) O(nlgn)
O (n lg(n))
28
ADT Partially Ordered Trees
  • Partially Ordered Trees (with either tree or heap
    representation), are an ADT defined with only 2
    basic operations
  • Insert - equivalent to enqueue of regular queues
  • Delete-max (min) - equivalent to dequeue of
    regular queues
  • POTs are efficient for dealing with
    priority-queue-like problems.
  • Like in regular queues,
  • dequeue from head of queue,
  • but
  • enqueue by priority.

29
Sorting
  • Insertion Sort
  • in-place
  • T(n) Q(n2)
  • Merge Sort
  • merge not in-place
  • Q(n lg n)
  • Heap Sort
  • ??
  • ??

30
Exercise The selection Problem
  • Given a file with n records (n gtgt l), with
    comparable keys.
  • The file is not sorted
  • Select the l maximal elements in the file, in
    time not larger than n lg l
Write a Comment
User Comments (0)
About PowerShow.com