0% found this document useful (0 votes)
60 views22 pages

trees notes

Tree

Uploaded by

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

trees notes

Tree

Uploaded by

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

Trees

A graph which has no cycle is called an acyclic graph. A tree is an acyclic graph or graph
having no cycles.

A tree or general trees is defined as a non-empty finite set of elements called vertices or
nodes having the property that each node can have minimum degree 1 and maximum
degree n. It can be partitioned into n+1 disjoint subsets such that the first subset
contains the root of the tree and remaining n subsets includes the elements of the n
subtree.

Directed Trees:
A directed tree is an acyclic directed graph. It has one node with indegree 1, while all
other nodes have indegree 1 as shown in fig:
The node which has outdegree 0 is called an external node or a terminal node or a leaf.
The nodes which have outdegree greater than or equal to one are called internal node.

25.8M
556
How to find Nth Highest Salary in SQL

Ordered Trees:
If in a tree at each level, an ordering is defined, then such a tree is called an ordered
tree.

Example: The trees shown in the figures represent the same tree but have different
orders.
Properties of Trees:
1. There is only one path between each pair of vertices of a tree.
2. If a graph G there is one and only one path between each pair of vertices G is a
tree.
3. A tree T with n vertices has n-1 edges.
4. A graph is a tree if and only if it a minimal connected.

Rooted Trees:
If a directed tree has exactly one node or vertex called root whose incoming degrees is 0
and all other vertices have incoming degree one, then the tree is called rooted tree.

Note: 1. A tree with no nodes is a rooted tree (the empty tree)


2. A single node with no children is a rooted tree.

Path length of a Vertex:


The path length of a vertex in a rooted tree is defined to be the number of edges in the
path from the root to the vertex.

Example: Find the path lengths of the nodes b, f, l, q as shown in fig:

Solution: The path length of node b is one.


The path length of node f is two.
The path length of node l is three
The path length of the node q is four.

Binary Trees:
If the outdegree of every node is less than or equal to 2, in a directed tree than the tree is called a binary tree. A
tree consisting of the nodes (empty tree) is also a binary tree. A binary tree is shown in fig:

Basic Terminology:
Root: A binary tree has a unique node called the root of the tree.

Left Child: The node to the left of the root is called its left child.

Right Child: The node to the right of the root is called its right child.

Traversing Binary Trees


Traversing means to visit all the nodes of the tree. There are three standard methods to
traverse the binary trees. These are as follows:

1. Preorder Traversal
2. Postorder Traversal
3. Inorder Traversal
1. Preorder Traversal: The preorder traversal of a binary tree is a recursive process.
The preorder traversal of a tree is

o Visit the root of the tree.


o Traverse the left subtree in preorder.
o Traverse the right subtree in preorder.

2. Postorder Traversal: The postorder traversal of a binary tree is a recursive process.


The postorder traversal of a tree is

o Traverse the left subtree in postorder.


o Traverse the right subtree in postorder.
o Visit the root of the tree.

3. Inorder Traversal: The inorder traversal of a binary tree is a recursive process. The
inorder traversal of a tree is

32.7M
565
Hello Java Program for Beginners

o Traverse in inorder the left subtree.


o Visit the root of the tree.
o Traverse in inorder the right subtree.

Example: Determine the preorder, postorder and inorder traversal of the binary tree as
shown in fig:
Solution: The preorder, postorder and inorder traversal of the tree is as follows:

Preorder 1 2 3 4 5 6 7 8 9 10 11

Postorder 3 5 4 2 7 10 9 11 8 6 1

Inorder 3 2 5 4 1 7 6 9 10 8 11

Binary Search Trees


Binary search trees have the property that the node to the left contains a smaller value
than the node pointing to it and the node to the right contains a larger value than the
node pointing to it.

It is not necessary that a node in a 'Binary Search Tree' point to the nodes whose value
immediately precede and follow it.

Example: The tree shown in fig is a binary search tree.


Inserting into a Binary Search Tree: Consider a binary tree T. Suppose we have
given an ITEM of information to insert in T. The ITEM is inserted as a leaf in the tree. The
following steps explain a procedure to insert an ITEM in the binary search tree T.

31.1M
676
C++ vs Java

1. Compare the ITEM with the root node.


2. If ITEM>ROOT NODE, proceed to the right child, and it becomes a root node for the
right subtree.
3. If ITEM<ROOT NODE, proceed to the left child.
4. Repeat the above steps until we meet a node which has no left and right subtree.
5. Now if the ITEM is greater than the node, then the ITEM is inserted as the right
child, and if the ITEM is less than the node, then the ITEM is inserted as the left
child.

Example: Show the binary search tree after inserting 3, 1,4,6,9,2,5,7 into an initially
empty binary search tree.

Solution: The insertion of the above nodes in the empty binary search tree is shown in
fig:
Deletion in a Binary Search Tree: Consider a binary tree T. Suppose we want to
delete a given ITEM from binary search tree. To delete an ITEM from a binary search tree
we have three cases, depending upon the number of children of the deleted node.

1. Deleted Node has no children: Deleting a node which has no children is very
simple, as replace the node with null.
2. Deleted Node has Only one child: Replace the value of a deleted node with the
only child.
3. Deletion node has only two children: In this case, replace the deleted node
with the node that is closest in the value to the deleted node. To find the nearest
value, we move once to the left and then to the right as far as possible. This node
is called the immediate predecessor. Now replace the value of the deleted node
with the immediate predecessor and then delete the replaced node by using case1
or case2.

Example: Show that the binary tree shown in fig (viii) after deleting the root node.

Solution: To delete the root node, first replace the root node with the closest elements
of the root. For this, first, move one step left and then to the right as far as possible to
the node.Then delete the replaced node. The tree after deletion shown in fig:
Spanning Tree
A subgraph T of a connected graph G is called spanning tree of G if T is a tree and T
include all vertices of G.

Minimum Spanning Tree:


Suppose G is a connected weight graph i.e., each edge of G is assigned a non-negative
number called the weight of edge, then any spanning tree T of G is assigned a total
weight obtained by adding the weight of the edge in T.

A minimum spanning tree of G is a tree whose total weight is as small as possible.


Prim’s Algorithm

The implementation of Prim’s Algorithm is explained in the following steps-

Step-01:

 choose any vertex. connecting to the edge having least weight .

Step-02:

 Select the next minimum cost edge which is adjacent to the initial minimum cost edge.

Step-03:

 Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree (MST) is
obtained that satisfies the following conditions
a) There should be all (n) vertices
b) The spanning tree consisting of minimum number of edges(n-1)
c) The spanning tree should not form a loop.

Problem-01:

Construct the minimum spanning tree (MST) for the given graph using Prim’s Algorithm-

Solution-

The above discussed steps are followed to find the minimum cost spanning tree using Prim’s Algorithm-
Step-01:

Step-02:

Step-03:

Step-04:
Step-05:

Step-06:

Since all the vertices have been included in the MST, so we stop.

Now, Cost of Minimum Spanning Tree


= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Problem-02:

Using Prim’s Algorithm, find the cost of minimum spanning tree (MST) of the given graph-

Solution-

The minimum spanning tree obtained by the application of Prim’s Algorithm on the given graph is as
shown below-

Now, Cost of Minimum Spanning Tree


= Sum of all edge weights
= 1 + 4 + 2 + 6 + 3 + 10
= 26 units
Kruskal's Algorithm to find a minimum spanning tree: This algorithm finds the
minimum spanning tree T of the given connected weighted graph G.

Kruskal’s Algorithm Implementation-

The implementation of Kruskal’s Algorithm is explained in the following steps-

Step-01:

 Sort all the edges from low weight to high weight.


Step-02:

 Take the edge with the lowest weight and use it to connect the vertices of graph.
 If adding an edge creates a cycle, then reject that edge and go for the next least weight edge.

Step-03:
 Keep adding edges until all the vertices are connected and a Minimum Spanning Tree (MST) is
obtained.

Problem-01:

Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm-

Solution-

To construct MST using Kruskal’s Algorithm,


 Simply draw all the vertices on the paper.
 Connect these vertices using edges with minimum weights such that no cycle gets formed.

Step-01:
Step-02:

Step-03:

Step-04:
Step-05:

Step-06:

Step-07:
Since all the vertices have been connected / included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units

31.9M
734

Features of Java - Javatpoint

1. Input the given connected weighted graph G with n vertices whose minimum
spanning tree T, we want to find.
2. Order all the edges of the graph G according to increasing weights.
3. Initialize T with all vertices but do include an edge.
4. Add each of the graphs G in T which does not form a cycle until n-1 edges are
added.

Example1: Determine the minimum spanning tree of the weighted graph shown in fig:
Solution: Using kruskal's algorithm arrange all the edges of the weighted graph in
increasing order and initialize spanning tree T with all the six vertices of G. Now start
adding the edges of G in T which do not form a cycle and having minimum weights until
five edges are not added as there are six vertices.

Step1:

Step2:

Step3:

Step4:
Step5:

Step6: Edge (A, B), (D, E) and (E, F) are discarded because they will form the cycle in a
graph.

So, the minimum spanning tree form in step 5 is output, and the total cost is 18.

Example2: Find all the spanning tree of graph G and find which is the minimal spanning
tree of G shown in fig:
Solution: There are total three spanning trees of the graph G which are shown in fig:
To find the minimum spanning tree, use the KRUSKAL'S ALGORITHM. The minimal
spanning tree is shown in fig:
The first one is the minimum spanning having the minimum weight = 11.

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