You are on page 1of 27

Trees

Dr. Quanita Kiran


A graph is called a tree if, and only if, it is circuit-free and
connected.

A graph is called a forest if, and only if, it is circuit-free and not
connected.
Internal and Terminal vertices
Let T be a tree. If T has only one or two vertices, then each is
called a terminal vertex.
If T has at least three vertices, then a vertex of degree 1 in T is
called a terminal vertex (or a leaf ), and a vertex of degree
greater than 1 in T is called an internal vertex (or a branch
vertex).
Rooted Tree root

• A rooted tree is a tree in which there is


one vertex that is distinguished from the Level 0
others and is called the root.
• The level of a vertex is the number of v is a child of u.
edges along the unique path between it u is the parent of v.
and the root. v and w are siblings.
• The height of a rooted tree is the u Level 1
maximum level of any vertex of the tree.
Given the root or any internal vertex v of a
rooted tree,
• The children of v are all those vertices that v w Level 2

are adjacent to v and are one level farther


away from the root than v.
Level 3
• If w is a child of v, then v is called the
parent of w, and
• Two distinct vertices that are both children Level 4
of the same parent are called siblings.
• Given two distinct vertices v and w, if v lies
on the unique path between w and the
root, then v is an ancestor of w and w is a
descendant of v. Vertices in the enclosed
region
are descendants of u,
u is an ancestor of each.
Consider the tree with root 𝑣0 shown below.
a. What is the level of 𝑣5 ?
b. What is the level of 𝑣0 ?
c. What is the height of this rooted tree?
d. What are the children of 𝑣3 ?
e. What is the parent of 𝑣2 ?
f. What are the siblings of 𝑣8 ?
g. What are the descendants of 𝑣3 ?
Binary Tree Root

• A binary tree is a rooted tree in


which every parent has at most
two children.
• Each child in a binary tree is u w x is the right
designated either a left child or a v is the left child of w.
right child (but not both), child of u.

• Every parent has at most one left v x


child and one right child.
• A full binary tree is a binary tree in
which each parent has exactly two
children.
• Given any parent v in a binary tree
T , if v has a left child, then the left
sub tree of v is the binary tree
whose root is the left child of v,
whose vertices consist of the left
child of v and all its descendants,
and whose edges consist of all Left sub
Right sub
those edges of T that connect the tree of w.
tree of w
vertices of the left sub tree.
• The right sub tree of v is defined
analogously.
Spanning Trees
A spanning tree for a graph G is a subgraph of G that contains
every vertex of G and is a tree.

• Every connected graph has a spanning tree.


• Any two spanning trees for a graph have the same number of
edges.
Find all spanning trees for the graph G pictured below.
Minimum Spanning Trees
A minimum spanning tree for a connected weighted graph is a
spanning tree that has the least possible total weight compared
to all other spanning trees for the graph.
Minimum Spanning Trees - Applications

• Network design. – telephone, electrical, hydraulic, TV cable,


computer, road.
• You want to lease phone lines to connect different offices
• Phone company charges different amounts of money to
connect different pairs of cities.
• You want a set of lines that connects all your offices with a
minimum total cost.
Algorithms for finding minimum spanning
Trees
In 1956 and 1957 Joseph B. Kruskal and Robert C. Prim each
described much more efficient algorithms to construct minimum
spanning trees. Even for large graphs, both algorithms can be
implemented so as to take relatively short computing times.
Kruskal’s Algorithm-Main Idea

In Kruskal’s algorithm, the edges of a connected weighted graph


are examined one by one in order of increasing weight.
At each stage the edge being examined is added to what will
become the minimum spanning tree, provided that this addition
does not create a circuit.
Kruskal’s Algorithm-Steps
The steps are:
The forest is constructed - with each node in a separate tree.
The edges are placed in a priority queue.
Until we've added n-1 edges,
1. Extract the cheapest edge from the queue,
2. If it forms a cycle, reject it,
3. Else add it to the forest. Adding it to the forest will join two
trees together.
Every step will have joined two trees in the forest together, so
that at the end, there will only be one tree in T.
Kruskal’s Algorithm-Working

You might also like