0% found this document useful (0 votes)
5 views25 pages

Trees

Uploaded by

vodnala srujana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Trees

Uploaded by

vodnala srujana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Trees

MAT230

Discrete Mathematics

Fall 2019

MAT230 (Discrete Math) Trees Fall 2019 1 / 19


Outline

1 What is a Tree?

2 Spanning Trees

3 Rooted Trees

4 Binary Trees

MAT230 (Discrete Math) Trees Fall 2019 2 / 19


Definitions
Definition
A tree is a connected undirected graph that has no cycles or self-loops.

Some examples:

Trees Not a tree: Not a tree:


has a cycle disconnected

Definition
A forest is an undirected graph whose components are all trees.

MAT230 (Discrete Math) Trees Fall 2019 3 / 19


A Theorem About Trees

Theorem
Let T = (V , E ) be an undirected graph with no self-loops and |V | = n.
Then the following statements are equivalent:
1 T is a tree.
2 Any two vertices of T are connected by exactly one path.
3 T is connected and every edge is an isthmus (its removal disconnects
T ).
4 T contains no cycles, but the addition of any new edge creates
exactly one cycle.
5 T is connected and has n − 1 edges.

MAT230 (Discrete Math) Trees Fall 2019 4 / 19


Spanning Trees and Minimum Spanning Trees
Suppose the following graph represents distance in miles between towns.
The towns are to be connected by high-speed network cable. Assuming
the cost of cables is proportional to their length and bandwidth is not a
limiting factor, the most cost effective network will be a tree that spans
the graph.
30
22
29
25 13 15
32
23
20 20
45
35
44
29

MAT230 (Discrete Math) Trees Fall 2019 5 / 19


Definitions

Definition
Let G = (V , E ) be a connected undirected graph. A spanning set for G
is a subset E 0 of E such that (V , E 0 ) is connected.

Definition
Let G be a connected undirected graph. The subgraph T is a spanning
tree for G if T is a tree and every node in G is a node in T .

Definition
If G is a weighted graph, then T is a minimal spanning tree of G if it is
a spanning tree and no other spanning tree of G has smaller total weight.

MAT230 (Discrete Math) Trees Fall 2019 6 / 19


Minimal Spanning Trees (MST)

Suppose G = (V , E , w ) is a weighted connected undirected graph. The


minimal spanning 0
Xtree problem is to find a spanning tree T = (V , E )
for G such that w (e) is as small as possible.
e∈E 0

Unlike the Traveling Salesman Problem, solving the MST problem is


relatively easy. We consider two algorithms.

MAT230 (Discrete Math) Trees Fall 2019 7 / 19


Prim’s Algorithm

Let G = (V , E , w ) be a weighted connected undirected graph.


1 Pick any vertex v ∈ V .
2 V 0 = {v }
3 V0 = V − {v }
4 E 0 = {}
5 While V 0 6= V do:
a Find u ∈ V 0 and v ∈ V0 such that edge e = {u, v } has minimum
weight
b E0 = E0 ∪ e
c V 0 = V 0 ∪ {v }
d V0 = V0 − {v }
Upon termination, T = (V , E 0 ) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2019 8 / 19


Kruskal’s Algorithm

Let G = (V , E , w ) be a weighted connected undirected graph.


1 Find edge e ∈ E with minimum weight
2 E 0 = {e}
3 E0 = E − {e}
4 V 0 = {v : v is a vertex for which e is an incident edge}
5 While V 0 6= V or (V 0 , E 0 ) not connected do:
a Find edge e ∈ E0 with minimum weight that will not complete a cycle
in (V 0 , E 0 ).
b E0 = E0 − e
c E0 = E0 ∪ e
d V 0 = V 0 ∪ {v : v is a vertex for which e is an incident edge}
Upon termination, T = (V , E 0 ) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2019 9 / 19


Rooted Trees

Definition
Every nonempty tree can have a particular vertex called a root. In a
rooted tree, the root is at level 0. The level of all other vertices is one
greater than the number of edges in the walk from the root to the vertex.
The height of a tree is the number of levels in the tree.

Definition
A vertex u in a rooted tree is a parent of a vertex v if v is adjacent to u
and the level of v is one greater than the level of u. In this case v is a
child of u. Two or more vertices are siblings if they have the same parent.

Definition
Nonroot vertices of degree 1 in a tree are called the leaves of the tree. All
other vertices are called internal vertices.

MAT230 (Discrete Math) Trees Fall 2019 10 / 19


Rooted Trees
On the left is a typical tree. On the right is the same tree redrawn with
vertex A identified as the root.

E B F D A

A J B C D

G C K E F G H I J

H I K

Vertex C is the parent of vertices G, H, and I. Vertices E and F are


children of vertex B and so are siblings. Vertices E, F, G, H, I, and J are
all at level 2. Vertices E, F, G, H, I, and K are leaves while A, B, C, D,
and J are internal vertices.
MAT230 (Discrete Math) Trees Fall 2019 11 / 19
Definition
A subtree is connected component of a rooted tree T that does not
include the root of T . Every subtree is itself a rooted tree.

This tree has subtrees rooted at A


B, C, and D.
There is also a subtree rooted at
J. B C D
Finally, each leaf is a subtree.
E F G H I J

MAT230 (Discrete Math) Trees Fall 2019 12 / 19


Kruskal’s Algorithm (Version 2)
Working with rooted trees makes Kruskal’s algorithm easier to describe.

Suppose there is an edge e between vertices u and v .

If u and v are both in a tree rooted However, if u is in a tree rooted at r


at r then the edge e will create a and v is in a tree rooted at s, adding
cycle. edge e does not create a cycle, but
merges the two trees.
r
r s

u
u
v
v
In this case, we can choose either r
or s to be the root of the merged
tree.
MAT230 (Discrete Math) Trees Fall 2019 13 / 19
Kruskal’s Algorithm (Version 2)

Let G = (V , E , w ) be a weighted connected undirected graph and suppose


we have function rootof(v ) that returns the root of the tree containing
vertex v .
1 Create sorted edge list EL (sort by increasing weight)
2 Initialize a list R of rooted trees with each vertex in the tree as the
root of its own tree.
3 E 0 = {}
4 While R contains more than one entry and EL not empty do:
a remove edge e = {u, v } from EL
b if rootof(u) 6= rootof(v ) then
F E 0 = E 0 ∪ {e}
F Merge tree rooted at rootof(u) into tree rooted at rootof(v ) (or
vice-versa). This reduces the number of entries in R by one.
Upon termination, T = (V , E 0 ) is a minimum spanning tree for G .

MAT230 (Discrete Math) Trees Fall 2019 14 / 19


Binary Trees

Definition
A binary tree T is a tree that has zero or more nodes in which each node
has at most two children. Each nonleaf node has left subtree and a right
subtree, either of which may be empty.

Question: How many vertices can be found on level k of a binary tree?

MAT230 (Discrete Math) Trees Fall 2019 15 / 19


Binary Trees

Definition
A binary tree T is a tree that has zero or more nodes in which each node
has at most two children. Each nonleaf node has left subtree and a right
subtree, either of which may be empty.

Question: How many vertices can be found on level k of a binary tree?


Answer: 2k ; the number of possible vertices doubles on each level.

MAT230 (Discrete Math) Trees Fall 2019 15 / 19


Binary Trees

Definition
A binary tree T is a tree that has zero or more nodes in which each node
has at most two children. Each nonleaf node has left subtree and a right
subtree, either of which may be empty.

Question: How many vertices can be found on level k of a binary tree?


Answer: 2k ; the number of possible vertices doubles on each level.

Question: How many vertices can be found in a binary tree of height h?

MAT230 (Discrete Math) Trees Fall 2019 15 / 19


Binary Trees

Definition
A binary tree T is a tree that has zero or more nodes in which each node
has at most two children. Each nonleaf node has left subtree and a right
subtree, either of which may be empty.

Question: How many vertices can be found on level k of a binary tree?


Answer: 2k ; the number of possible vertices doubles on each level.

Question: How many vertices can be found in a binary tree of height h?


Answer: 2k+1 − 1; the sum of the powers of 2 from 20 up to 2k .

MAT230 (Discrete Math) Trees Fall 2019 15 / 19


Binary Tree Traversal
A traversal of a tree visit each vertex of the tree in some order determined
by the connectivity within the tree. There are three common traversals of
binary trees, each described by a recursive algorithm.
Preorder Traversal:
1 Visit the root of the tree

2 Preorder traverse the left subtree

3 Preorder traverse the right subtree

Inorder Traversal:
1 Inorder traverse the left subtree
2 Visit the root of the tree
3 Inorder traverse the right subtree
Postorder Traversal:
1 Postorder traverse the left subtree

2 Postorder traverse the right subtree

3 Visit the root of the tree

MAT230 (Discrete Math) Trees Fall 2019 16 / 19


Expression Trees

The arithmetic operations +, −, ×, and / are called binary operators since


they each take two operands.

We can diagram them with trees. For example, trees for a + b and c/d are

+ /

a b c d

MAT230 (Discrete Math) Trees Fall 2019 17 / 19


Expression Trees

Consider the arithmetic expression 3 + 4(5 − 3) + (7 + 3)/5. A tree for this


expression is

+ /

3 × + 5

4 − 7 3

5 3

This tree is not unique, other binary trees could be used to represent the
same expression.

Construct a preorder and postorder traversal of this tree.


MAT230 (Discrete Math) Trees Fall 2019 18 / 19
Expression Trees

+ /

3 × + 5

4 − 7 3

5 3

MAT230 (Discrete Math) Trees Fall 2019 19 / 19


Expression Trees

+ /

3 × + 5

4 − 7 3

5 3

Preorder traversal: + + 3 × 4 − 5 3 / + 7 3 5

MAT230 (Discrete Math) Trees Fall 2019 19 / 19


Expression Trees

+ /

3 × + 5

4 − 7 3

5 3

Preorder traversal: + + 3 × 4 − 5 3 / + 7 3 5

Postorder traversal: 3 4 5 3 − × + 7 3 + 5 / +

MAT230 (Discrete Math) Trees Fall 2019 19 / 19


Expression Trees

+ /

3 × + 5

4 − 7 3

5 3

Preorder traversal: + + 3 × 4 − 5 3 / + 7 3 5

Postorder traversal: 3 4 5 3 − × + 7 3 + 5 / +

Both of these can be evaluated unambiguously without the need for


parentheses.
MAT230 (Discrete Math) Trees Fall 2019 19 / 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy