Title: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
1Trees 1 Theory, Models, Generic Heap
Algorithms, Priority Queues
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Trees 1 Overview
- General treestheory and terminology
- Tree traversals
- Binary trees
- Vector implementation of binary trees
- Partially ordered trees (POT)
- Heap algorithms
- Heapsort
- Priority queue
3Theory and Terminology
- Definition A tree is a connected graph with no
cycles - Consequences
- Between any two vertices, there is exactly one
unique path
4A Tree?
5A Tree? Nope
9
10
12
11
5
6
8
7
2
3
4
1
6A Tree?
7A Tree? Yup
9
10
12
11
5
6
8
7
2
3
4
1
8Theory and Terminology
- Definition A rooted tree is a graph G such
that - G is connected
- G has no cycles
- G has exactly one vertex called the root of the
tree
9Theory and Terminology
- Consequences
- The depth of a vertex v is the length of the
unique path from root to v - G can be arranged so that the root is at the top,
its neighboring vertices are vertices of depth 1,
and so on - The set of all vertices of depth k is called
level k of the tree
10A Rooted Tree
11Theory and Terminology
- Definition A descending path in a rooted tree
is a path, whose edges go from a vertex to a
deeper vertex
12Theory and Terminology
- Consequences
- A unique path from the root to any vertex is a
descending path - The length of this path is the depth of the vertex
1
depth 0
root
depth 1
2
3
4
5
6
8
7
depth 2
9
10
12
11
depth 3
13Theory and Terminology
- Definition If there is a descending path from
v1 to v2, v1 is an ancestor of v2, and v2 is a
descendant of v1.
14Theory and Terminology
- Suppose v is a vertex of depth k
- Any vertex that is adjacent to v must have depth
k - 1 or k 1.
15Theory and Terminology
- Suppose v is a vertex of depth k
- Vertices adjacent to v of depth k 1 are called
children of v.
1
depth 0
root
depth 1
2
3
4
5
6
8
7
depth 2
9
10
12
11
depth 3
16Theory and Terminology
- Suppose v is a vertex of depth k
- If k gt 0, there is exactly one vertex of depth k
1 that is adjacent to v in the graph. This
vertex is called the parent of v.
1
depth 0
root
depth 1
2
3
4
5
6
8
7
depth 2
9
10
12
11
depth 3
17Theory and Terminology
- Definitions
- A vertex with no children is called a leaf
18Theory and Terminology
- Definitions
- The height of a tree is the maximum depth of its
vertices
1
depth 0
root
depth 1
2
3
4
height
5
6
8
7
depth 2
9
10
12
11
depth 3
19Theory and Terminology
- Definitions
- The root is the only vertex of depth 0. The root
has no parent.
20Tree Traversals
- Definition A traversal is the process for
visiting all of the vertices in a tree - Often defined recursively
- Each kind corresponds to an iterator type
- Iterators are implemented non-recursively
21Preorder Traversal
- Visit vertex, then visit child vertices
(recursive definition) - Depth-first search
- Begin at root
- Visit vertex on arrival
- Implementation may be recursive, stack-based, or
nested loop
22Preorder Traversal
23Preorder Traversal
24Postorder Traversal
- Visit child vertices, then visit vertex
(recursive definition) - Depth-first search
- Begin at root
- Visit vertex on departure
- Implementation may be recursive, stack-based, or
nested loop
25Postorder Traversal
26Postorder Traversal
27Postorder Traversal
28Postorder Traversal
29Levelorder Traversal
- Visit all vertices in level, starting with level
0 and increasing - Breadth-first search
- Begin at root
- Visit vertex on departure
- Only practical implementation is queue-based
30Levelorder Traversal
31Levelorder Traversal
32Tree Traversals
- Preoder depth-first search (possibly
stack-based), visit on arrival - Postorder depth-first search (possibly
stack-based), visit on departure - Levelorder breadth-first search (queue-based),
visit on departure
33Binary Trees
- Definition A binary tree is a rooted tree in
which no vertex has more than two children
1
root
2
3
4
5
6
7
34Binary Trees
- Definition A binary tree is complete iff every
layer but the bottom is fully populated with
vertices.
1
root
2
3
4
5
7
6
35Binary Trees
- A complete binary tree with n vertices and high H
satisfies - 2H lt n lt 2H 1
- 22 lt 7 lt 22 1
1
root
2
3
4
5
7
6
36Binary Trees
- A complete binary tree with n vertices and high H
satisfies - 2H lt n lt 2H 1
- H lt lg n lt H 1
- H floor(lg n)
37Binary Trees
- Theorem In a complete binary tree with n
vertices and height H - 2H lt n lt 2H 1
38Binary Trees
- Proof
- For level k, there are 2k vertices
- Total number of vertices
- n 20 21 2k
- n 1 21 22 2k
- n 1 2(1 21 2k-1)
- n 1 2(n - 2k)
- n 1 2n 2k 1
- n 2k 1 - 1
39Binary Trees
- Let k H
- number of vertices for height H 2H 1 1
- number of vertices for height H lt 2H 1
- Let k H 1
- number of vertices for height H - 1 2H 1
- number of vertices for height H gt 2H
- 2H lt n lt 2H 1
40Binary Tree Traversals
- Inorder traversal
- Definition left child, visit, right child
(recursive) - Algorithm depth-first search (visit between
children)
41Inorder Traversal
42Inorder Traversal
43Inorder Traversal
44Inorder Traversal
45Binary Tree Traversals
- Other traversal apply to binary case
- Preorder traversal
- vertex, left subtree, right subtree
- Inorder traversal
- left subtree, vertex, right subtree
- Postorder traversal
- left subtree, right subtree, vertex
- Levelorder traversal
- vertex, left children, right children
46Vector Representation of Complete Binary Tree
- Tree data
- Vector elements carry data
- Tree structure
- Vector indices carry tree structure
- Index order levelorder
- Tree structure is implicit
- Uses integer arithmetic for tree navigation
47Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
48Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
49Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
50Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
51Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
52Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
53Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
54Vector Representation of Complete Binary Tree
- Tree navigation
- Parent of vk v(k 1)/2
- Left child of vk v2k 1
- Right child of vk v2k 2
55Partially 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
56Partially 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
57Heaps
- 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
58Heaps
- 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
59The Push Heap Algorithm
- Add new data at next leaf
- Repair upward
- Repeat
- Locate parent
- if POT not satisfied
- swap
- else
- stop
- Until POT
60The Push Heap Algorithm
- Add new data at next leaf
61The Push Heap Algorithm
- Add new data at next leaf
62The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
63The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
64The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
65The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
66The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
67The Push Heap Algorithm
- Repeat
- Locate parent of vk v(k 1)/2
- if POT not satisfied
- swap
- else
- stop
68The 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
69The Pop Heap Algorithm
70The Pop Heap Algorithm
71The Pop Heap Algorithm
72The Pop Heap Algorithm
- Repeat
- find the larger child
- if POT not satisfied
- swap
- else
- stop
73The Pop Heap Algorithm
- Repeat
- find the larger child
- if POT not satisfied
- swap
- else
- stop
74The Pop Heap Algorithm
- Repeat
- find the larger child
- if POT not satisfied
- swap
- else
- stop
75Generic 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
76Priority Queues
- Element type with priority
- typename T t
- Predicate class P p
- Associative queue operations
- void Push(t)
- void Pop()
- T Front()
77Priority 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
78The 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
-
79The 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()
-