Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues

1 / 79
About This Presentation
Title:

Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues

Description:

Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues. Andy Wang ... H = floor(lg n) Binary Trees. Theorem: In a complete binary tree with n ... –

Number of Views:73
Avg rating:3.0/5.0
Slides: 80
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues


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

2
Trees 1 Overview
  • General treestheory and terminology
  • Tree traversals
  • Binary trees
  • Vector implementation of binary trees
  • Partially ordered trees (POT)
  • Heap algorithms
  • Heapsort
  • Priority queue

3
Theory and Terminology
  • Definition A tree is a connected graph with no
    cycles
  • Consequences
  • Between any two vertices, there is exactly one
    unique path

4
A Tree?
5
A Tree? Nope
9
10
12
11
5
6
8
7
2
3
4
1
6
A Tree?
7
A Tree? Yup
9
10
12
11
5
6
8
7
2
3
4
1
8
Theory 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

9
Theory 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

10
A Rooted Tree
11
Theory and Terminology
  • Definition A descending path in a rooted tree
    is a path, whose edges go from a vertex to a
    deeper vertex

12
Theory 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
13
Theory 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.

14
Theory 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.

15
Theory 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
16
Theory 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
17
Theory and Terminology
  • Definitions
  • A vertex with no children is called a leaf

18
Theory 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
19
Theory and Terminology
  • Definitions
  • The root is the only vertex of depth 0. The root
    has no parent.

20
Tree 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

21
Preorder 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

22
Preorder Traversal
23
Preorder Traversal
24
Postorder 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

25
Postorder Traversal
26
Postorder Traversal
27
Postorder Traversal
28
Postorder Traversal
29
Levelorder 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

30
Levelorder Traversal
31
Levelorder Traversal
32
Tree 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

33
Binary 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
34
Binary 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
35
Binary 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
36
Binary 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)

37
Binary Trees
  • Theorem In a complete binary tree with n
    vertices and height H
  • 2H lt n lt 2H 1

38
Binary 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

39
Binary 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

40
Binary Tree Traversals
  • Inorder traversal
  • Definition left child, visit, right child
    (recursive)
  • Algorithm depth-first search (visit between
    children)

41
Inorder Traversal
42
Inorder Traversal
43
Inorder Traversal
44
Inorder Traversal
45
Binary 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

46
Vector 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

47
Vector 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

48
Vector 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

49
Vector 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

50
Vector 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

51
Vector 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

52
Vector 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

53
Vector 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

54
Vector 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

55
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

56
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

57
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
58
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

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

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

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

62
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

63
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

64
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

65
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

66
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

67
The Push Heap Algorithm
  • Repeat
  • Locate parent of vk v(k 1)/2
  • if POT not satisfied
  • swap
  • else
  • stop

68
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

69
The Pop Heap Algorithm
  • Copy last leaf to root

70
The Pop Heap Algorithm
  • Copy last leaf to root

71
The Pop Heap Algorithm
  • Remove last leaf

72
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

73
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

74
The Pop Heap Algorithm
  • Repeat
  • find the larger child
  • if POT not satisfied
  • swap
  • else
  • stop

75
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

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

77
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

78
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

79
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