Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views32 pages

Graph Theory: Trees & Binary Trees

Graph theory concepts were outlined, including trees, forests, and properties. A tree is a connected, acyclic graph. A forest is a disjoint collection of trees. Properties of trees include having at least two leaves and (n-1) edges for a tree with n vertices. Binary trees were also defined, where each node has a maximum of two child nodes. Full binary trees require each internal node to have two children, while complete binary trees fill all levels except possibly the last.

Uploaded by

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

Graph Theory: Trees & Binary Trees

Graph theory concepts were outlined, including trees, forests, and properties. A tree is a connected, acyclic graph. A forest is a disjoint collection of trees. Properties of trees include having at least two leaves and (n-1) edges for a tree with n vertices. Binary trees were also defined, where each node has a maximum of two child nodes. Full binary trees require each internal node to have two children, while complete binary trees fill all levels except possibly the last.

Uploaded by

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

Graph Theory

Outline for today’s Lecture

Properties of Trees and Forests


Binary tree
1. What is Tree and Forest?

Tree
•In graph theory, a tree is an undirected, connected and acyclic graph. In other
words, a connected graph that does not contain even a single cycle is called a tree.

•A tree represents hierarchical structure in a graphical form.

•The elements of trees are called their nodes and the edges of the tree are called
branches.

•A tree with n vertices has (n-1) edges.

•Trees provide many useful applications as simple as a family tree to as complex as


trees in data structures of computer science.

•A leaf in a tree is a vertex of degree 1 or any vertex having no children is called a


leaf.
Example

In the above example, all are trees with fewer than 6 vertices.
The two graphs at left (white and yellow vertices) are trees, but the two at right aren’t:
the one at upper right (with green vertices) has multiple connected components (and so
it isn’t connected) while the one at lower right (blue vertices) contains a cycle. The graph
at upper right is, however, a forest as each of its connected components is a tree.
In the two trees above the internal nodes are white, while the leaf nodes are
colored green or yellow.
Forest
In graph theory, a forest is an undirected, disconnected, acyclic graph. In other
words, a disjoint collection of trees is known as forest. Each component of a forest is
tree.

Example

The above graph looks like a two sub-


graphs but it is a single disconnected
graph. There are no cycles in the above
graph. Therefore it is a forest.
2. Properties of Trees

1.Every tree which has at least two vertices should have at least two leaves.

2.Trees have many characterizations:

Let T be a graph with n vertices, then the following statements are equivalent:
1. T is a tree.
2. T contains no cycles and has n-1 edges.
3. T is connected and has (n -1) edge.
4. T is connected graph, and every edge is a cut-edge.
5. Any two vertices of graph T are connected by exactly one path.
6. T contains no cycles, and for any new edge e, the graph T+ e has exactly one cycle.
3. Every edge of a tree is cut -edge.
4. Adding one edge to a tree defines exactly one cycle.
5. Every connected graph contains a spanning tree.
6. Every tree has at least two vertices of degree two.
3. Spanning Tree

A spanning tree in a connected graph G is a sub-graph H of G that includes all the


vertices of G and is also a tree.

Example

Consider the following graph G:

From the above graph G we can implement following three spanning trees H:
Methods to find the spanning Tree
We can find the spanning tree systematically by using either of two methods:
1.Cutting- down Method
2.Building- up Method

1. Cutting- down method


•Start choosing any cycle in Graph G.
•Remove one of cycle's edges.
•Repeat this process until there are no cycles left.

Example
•Consider a graph G,

•If we remove the edge ac which destroy the cycle a-d-c-a in the above graph then we
get the following graph:
•Remove the edge cb, which destroy the cycle a-d-c-b-a from the above graph then we get the
following graph:

•If we remove the edge ec, which destroy the cycle


d-e-c-d from the above graph then we get the
following spanning tree:
2. Building - up method
•Select edges of graph G one at a time. In such a way that there are no cycles
are created.
•Repeat this process until all the vertices are included.

Example
Consider the following graph G, •Choose the edge ab,

•Choose the edge de,


•After that , choose the edge ec,

•Next, choose the edge cb, then finally we get the following spanning tree:
Circuit Rank
The number of edges we need to delete from G in order to get a spanning tree.
Spanning tree G = m- (n-1), which is called the circuit rank of G.
1.Where, m = No. of edges.
2. n = No. of vertices.

Example

In the above graph, edges m = 7 and vertices


n=5
Then the circuit rank is,
1. G = m - (n - 1)
2. = 7 - (5 - 1)
3. = 3
Binary Tree
A binary tree is a finite set of nodes that is either empty or consist a root node and two
disjoint binary trees called the left sub tree and the right sub tree.

In other words, a binary tree is a non-linear data structure in which each node has
maximum of two child nodes. The tree connections can be called as branches.

1. Trees are used to represent data in hierarchical form.


2. Binary tree is the one in which each node has maximum of two child- node.
3. The order of binary tree is ‘2’.
4. Binary tree does not allow duplicate values.
5. While constructing a binary, if an element is less than the value of its parent
node, it is placed on the left side of it otherwise right side.
A binary tree is shown for the element 40, 56, 35, 48, 22, 65, 28.
Advantages of Binary Tree:

• Searching in Binary tree become faster.


• Binary tree provides six traversals.
• Two of six traversals give sorted order of elements.
• Maximum and minimum elements can be directly picked up.
• It is used for graph traversal and to convert an expression to postfix and prefix forms.
Full Binary Tree

A full binary tree is also known as 2-tree in which every node other than the leaf
nodes has two child nodes. It means all the leaf nodes should be at the same
level and all other internal nodes should contain two child nodes each.

Let’s see some examples:

If we look at this tree, we can see that it


has two nodes for all the internal nodes
except the leaf nodes. In addition to that,
all the leaf nodes D, E, F, G are one the
same level. Hence, we can say that this is
an example of a full binary tree.
Let’s see another tree example:

If we check this tree, the node C is an internal node


here and is not a leaf node. Although all the leaf
nodes are on the same level, node C has only one
child node which doesn’t satisfy the definition.
Therefore, this example is not a full binary tree.
Complete Binary Tree

In a complete binary tree, all the levels of a tree are filled entirely except the last
level. In the last level, nodes might or might not be filled fully. Also, let’s note that all
the nodes should be filled from the left.
A complete binary tree has two child nodes at each level. In the definition,
we mentioned all the levels are filled completely except at the last level.
Let’s denote the level of a tree by L . If a binary tree is completely filled then at
each level we’ll have 2 L nodes.

In our case, it is absolutely mandatory to contain 2 L nodes at each level except the
last level. Another point we mentioned in the definition is that we fill the nodes
from the left. So it is not allowed for a node to have a right child node without
having a left child node.
Example
Now we know the theory, let’s verify the definition with the help of a couple of
examples:

According to the definition, all the levels


except the last level should be completely
filled. Here, the level 2 is the last level. The
tree has the root node A at level 0 and two
nodes B and C at level 1 . Till now, it satisfies
the definition. Also, let’s note that the nodes
are filled from the left.
At level 2, there are 2 nodes. But most importantly,
the nodes are filled from left. Therefore we can
conclude that it is a complete binary tree.
Let’s take another example:

First, we’ll check if the given tree has the required


number of nodes at each level except the last level
which is level 2 in this case. We can see that the
given tree satisfies this condition.

Next, we’ll check whether the nodes are filled from


the left or not. At level 2, the node B has no child
nodes. That’s fine according to the definition.

But the problem with this tree is that the node C has two child nodes. Now according
to the definition, the nodes need to be filled from the left. So there should be a left
child node for the node B which is missing here. Therefore, this example doesn’t
follow the definition. Hence, it is not a complete binary tree.
Applications of Complete Binary Tree

We can use it in the heap data structure. In computer science, heaps can be
of two types: max-heap and min-heap. Algorithms like Heap Sort uses it for
sorting.
It is also used in implementing priority queues and
external sorting algorithms.
Almost Complete Binary Tree

An almost complete binary tree is a special kind of binary tree where insertion
takes place level by level and from left to right order at each level and the last
level is not filled fully always. It also contains 2 L nodes at each level except the last
level.
The main point here is that if we want to insert any node at the lowest level of the
tree, it should be inserted from the left. All the internal nodes should be completely
filled.

An almost complete tree is also a complete binary tree.


Example

Let’s take a couple of examples:


This example satisfies the definition as it has
the required number of nodes at level 0 and 1.
Level 2 is the last level here. Therefore, it is not
required to have all the 2 L nodes at level 2.

the node B has two child nodes D, E and the


node C has one child node F but most
importantly it is left child node. Therefore it
satisfies the definition of an almost complete
binary tree.
Example 2

Let’s take another example:


this is not an almost binary tree nor
complete. REASON ?
Balanced Binary Tree
In case of binary trees, if the trees are skewed, they become computationally
inefficient to perform operations on.

This is the motivation behind making sure that trees are not skewed. Hence the
need for balanced binary trees.
What is a Balanced Binary Tree
Balanced Binary trees are computationally efficient to perform operations on.
A balanced binary tree will follow the following conditions:
For any node in the tree, the absolute difference in the height (or depth) of its left
and right subtrees is either 0 or 1.
This means that the height difference between the left and right subtrees can be
either 0 (perfectly balanced) or 1 (slightly imbalanced), but it should not exceed 1.
This balance condition ensures that the tree remains reasonably well-balanced,
and operations like searching, inserting, and deleting elements have efficient time
complexity.
Directed and Undirected rooted tree

In graph theory, a tree is an undirected graph in which any two vertices are
connected by exactly one path, or equivalently a connected acyclic undirected
graph.

A rooted tree may be directed, called a directed rooted tree, either making all
its edges point away from the root—in which case it is called an out-tree—or
making all its edges point towards the root—in which case it is called an in-tree.

You might also like