Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
description
Transcript of Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
![Page 1: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/1.jpg)
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Andy Wang
Data Structures, Algorithms, and Generic Programming
![Page 2: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/2.jpg)
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
![Page 3: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/3.jpg)
Review Question
D B F E A C
A
B
D
E
F
C
![Page 4: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/4.jpg)
Review Question
D B F E A C
A x
B x
D x
E x
F x
C x
![Page 5: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/5.jpg)
Partially Ordered Trees
Definition: A partially ordered tree is a tree T such that:
There is an order relation >= defined for the vertices of T
For any vertex p and any child c of p, p >= c
![Page 6: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/6.jpg)
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
![Page 7: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/7.jpg)
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
3 2
1 0
root
![Page 8: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/8.jpg)
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
![Page 9: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/9.jpg)
The Push Heap Algorithm
Add new data at next leafRepair upwardRepeat
Locate parentif POT not satisfied
swap
elsestop
Until POT
![Page 10: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/10.jpg)
The Push Heap Algorithm
Add new data at next leaf
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 5 4 3
![Page 11: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/11.jpg)
The Push Heap Algorithm
Add new data at next leaf
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 5 4 3 8
![Page 12: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/12.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 5 4 3 8
![Page 13: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/13.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 5 4 3 8
![Page 14: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/14.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 8 4 3 5
![Page 15: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/15.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 8 4 3 5
![Page 16: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/16.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 8 4 3 5
![Page 17: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/17.jpg)
The Push Heap Algorithm
RepeatLocate parent of v[k] = v[(k – 1)/2]
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
8 6 7 4 3 5
![Page 18: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/18.jpg)
The Pop Heap Algorithm
Copy last leaf to rootRemove last leafRepeat
find the larger childif POT not satisfied
swap
elsestop
Until POT
![Page 19: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/19.jpg)
The Pop Heap Algorithm
Copy last leaf to root
0 l r ll lr rl rr
0 1 2 3 4 5 6
8 6 7 4 3 5
![Page 20: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/20.jpg)
The Pop Heap Algorithm
Copy last leaf to root
0 l r ll lr rl rr
0 1 2 3 4 5 6
5 6 7 4 3 5
![Page 21: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/21.jpg)
The Pop Heap Algorithm
Remove last leaf
0 l r ll lr rl rr
0 1 2 3 4 5 6
5 6 7 4 3
![Page 22: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/22.jpg)
The Pop Heap Algorithm
Repeatfind the larger child
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
5 6 7 4 3
![Page 23: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/23.jpg)
The Pop Heap Algorithm
Repeatfind the larger child
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
5 6 7 4 3
![Page 24: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/24.jpg)
The Pop Heap Algorithm
Repeatfind the larger child
if POT not satisfiedswap
elsestop
0 l r ll lr rl rr
0 1 2 3 4 5 6
7 6 5 4 3
![Page 25: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/25.jpg)
Generic Heap Algorithms
Apply to ranges
Specified by random access iterators
Current supportArrays
TVector<T>
TDeque<T>
Source code file: gheap.h
Test code file: fgss.cpp
![Page 26: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/26.jpg)
Priority Queues
Element type with prioritytypename T t
Predicate class P p
Associative queue operationsvoid Push(t)
void Pop()
T& Front()
![Page 27: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/27.jpg)
Priority Queues
Associative propertyPriority 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
![Page 28: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/28.jpg)
The Priority Queue Generic Adaptor
template <typename T, class C, class P>
class CPriorityQueue {
C c;
P LessThan;
public:
typedef typename C::value_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;
}
![Page 29: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues](https://reader035.fdocuments.in/reader035/viewer/2022062519/56814dce550346895dbb2927/html5/thumbnails/29.jpg)
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 << “error” << endl;
exit(EXIT_FALIURE);
}
g_pop_heap(c.Begin(), c.End(), LessThan);
c.PopBack();
}
};