Trees%201:%20Theory,%20Models,%20Generic%20Heap%20Algorithms,%20Priority%20Queues

About This Presentation
Title:

Trees%201:%20Theory,%20Models,%20Generic%20Heap%20Algorithms,%20Priority%20Queues

Description:

Add new data at next leaf. Repair upward. Repeat. Locate parent. if POT not satisfied ... leaf to root. Remove last leaf. Repeat. find the larger child. if POT ... –

Number of Views:23
Avg rating:3.0/5.0
Slides: 30
Provided by: csF2
Learn more at: https://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Trees%201:%20Theory,%20Models,%20Generic%20Heap%20Algorithms,%20Priority%20Queues


1
Trees 1 Theory, Models, Generic Heap
Algorithms, Priority Queues
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Review Question
  • Given the following information, can I uniquely
    identify a tree?
  • Nodes listed based on an inorder traversal
  • D, B, F, E, A, C
  • Nodes listed based on a preorder traversal
  • A, B, D, E, F, C

3
Review Question
  • D B F E A C
  • A
  • B
  • D
  • E
  • F
  • C

4
Review Question
  • D B F E A C
  • A x
  • B x
  • D x
  • E x
  • F x
  • C x

5
Partially Ordered Trees
  • Definition A partially ordered tree is a tree T
    such that
  • There is an order relation gt defined for the
    vertices of T
  • For any vertex p and any child c of p, p gt c

6
Partially Ordered Trees
  • Consequences
  • The largest element in a partially ordered tree
    (POT) is the root
  • No conclusion can be drawn about the order of
    children

7
Heaps
  • Definition A heap is a partially ordered
    complete (almost) binary tree. The tree is
    completely filled on all levels except possibly
    the lowest.

4
root
3
2
1
0
8
Heaps
  • Consequences
  • The largest element in a heap is the root
  • A heap can be stored using the vector
    implementation of binary tree
  • Heap algorithms
  • Push Heap
  • Pop Heap

9
The Push Heap Algorithm
  • Add new data at next leaf
  • Repair upward
  • Repeat
  • Locate parent
  • if POT not satisfied
  • swap
  • else
  • stop
  • Until POT

10
The Push Heap Algorithm
  • Add new data at next leaf

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 5 4 3
11
The Push Heap Algorithm
  • Add new data at next leaf

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 5 4 3 8
12
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 5 4 3 8
13
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 5 4 3 8
14
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 8 4 3 5
15
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 8 4 3 5
16
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 8 4 3 5
17
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
8 6 7 4 3 5
18
The Pop Heap Algorithm
  • Copy last leaf to root
  • Remove last leaf
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop
  • Until POT

19
The Pop Heap Algorithm
  • Copy last leaf to root

0 1 2 3 4 5 6
0 l r ll lr rl rr
8 6 7 4 3 5
20
The Pop Heap Algorithm
  • Copy last leaf to root

0 1 2 3 4 5 6
0 l r ll lr rl rr
5 6 7 4 3 5
21
The Pop Heap Algorithm
  • Remove last leaf

0 1 2 3 4 5 6
0 l r ll lr rl rr
5 6 7 4 3
22
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
5 6 7 4 3
23
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
5 6 7 4 3
24
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

0 1 2 3 4 5 6
0 l r ll lr rl rr
7 6 5 4 3
25
Generic Heap Algorithms
  • Apply to ranges
  • Specified by random access iterators
  • Current support
  • Arrays
  • TVectorltTgt
  • TDequeltTgt
  • Source code file gheap.h
  • Test code file fgss.cpp

26
Priority Queues
  • Element type with priority
  • typename T t
  • Predicate class P p
  • Associative queue operations
  • void Push(t)
  • void Pop()
  • T Front()

27
Priority Queues
  • Associative property
  • Priority value determined by p
  • Push(t) inserts t, increases size by 1
  • Pop() removes element with highest priority
    value, decreases size by 1
  • Front() returns element with highest priority
    value, no state change

28
The Priority Queue Generic Adaptor
  • template lttypename T, class C, class Pgt
  • class CPriorityQueue
  • C c
  • P LessThan
  • public
  • typedef typename Cvalue_type value_type
  • int Empty() const return c.Empty()
  • unsigned int Size() const return c.Size()
  • void Clear() c.Clear()
  • CPriorityQueue operator(const CPriorityQueue
    q)
  • if (this ! q)
  • c q.c
  • LessThan q.LessThan
  • return this

29
The Priority Queue Generic Adaptor
  • void Display(ostream os, char ofc \0) const
  • c.Display(os, ofc)
  • void Push(const value_type t)
  • c.PushBack(t)
  • g_push_heap(c.Begin(), c.End(), LessThan)
  • void Pop()
  • if (Empty())
  • cerr ltlt error ltlt endl
  • exit(EXIT_FALIURE)
  • g_pop_heap(c.Begin(), c.End(), LessThan)
  • c.PopBack()
Write a Comment
User Comments (0)
About PowerShow.com