0% found this document useful (0 votes)
8 views21 pages

Unit 4

The document provides an overview of binary trees and graphs, including their terminology, traversal methods, and applications. It explains key concepts such as nodes, edges, root, leaf, and various traversal algorithms like Inorder, Preorder, Postorder for trees, and Breadth First Search and Depth First Search for graphs. Additionally, it discusses the applications of trees and graphs in computer science, including data structures, algorithms, and real-world scenarios like social networks and navigation systems.

Uploaded by

donax13349
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)
8 views21 pages

Unit 4

The document provides an overview of binary trees and graphs, including their terminology, traversal methods, and applications. It explains key concepts such as nodes, edges, root, leaf, and various traversal algorithms like Inorder, Preorder, Postorder for trees, and Breadth First Search and Depth First Search for graphs. Additionally, it discusses the applications of trees and graphs in computer science, including data structures, algorithms, and real-world scenarios like social networks and navigation systems.

Uploaded by

donax13349
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/ 21

Unit-4

Q.1) Binary Tree and it’s terminology: The Binary tree means that
the node can have maximum two children. Here, binary name itself suggests
that 'two'; therefore, each node can have either 0, 1 or 2 children.

Let's understand the binary tree through an example.


Terminology
In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have
a root node. We can say that the root node is the origin of the tree data structure. In
any tree, there must be only one root node. We never have multiple root nodes in a
tree.

2. Edge
In a tree data structure, the connecting link between any two nodes is called
as EDGE. In a tree with 'N' number of nodes there will be a maximum of 'N-1'
number of edges.
3. Parent
In a tree data structure, the node which is a predecessor of any node is called
as PARENT NODE. In simple words, the node which has a branch from it to any
other node is called a parent node. Parent node can also be defined as "The node
which has child / children".

4. Child
In a tree data structure, the node which is descendant of any node is called
as CHILD Node. In simple words, the node which has a link from its parent node is
called as child node. In a tree, any parent node can have any number of child nodes.
In a tree, all the nodes except root are child nodes.
5. Siblings
In a tree data structure, nodes which belong to same Parent are called
as SIBLINGS. In simple words, the nodes with the same parent are called Sibling
nodes.

6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF
Node. In simple words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External
node is also a node with no child. In a tree, leaf node is also called as 'Terminal'
node.

7. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL
Node. In simple words, an internal node is a node with atleast one child.

In a tree data structure, nodes other than leaf nodes are called as Internal
Nodes. The root node is also said to be Internal Node if the tree has more than
one node. Internal nodes are also called as 'Non-Terminal' nodes.
8. Degree
In a tree data structure, the total number of children of a node is called
as DEGREE of that Node. In simple words, the Degree of a node is total number of
children it has. The highest degree of a node among all the nodes in a tree is called
as 'Degree of Tree'

9. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root
node are at Level 1 and the children of the nodes which are at Level 1 will be at
Level 2 and so on... In simple words, in a tree each step from top to bottom is called
as a Level and the Level count starts with '0' and incremented by one at each level
(Step).
10. Height
In a tree data structure, the total number of edges from leaf node to a particular
node in the longest path is called as HEIGHT of that Node. In a tree, height of the
root node is said to be height of the tree. In a tree, height of all leaf nodes is '0'.

11. Depth
In a tree data structure, the total number of egdes from root node to a particular
node is called as DEPTH of that Node. In a tree, the total number of edges from root
node to a leaf node in the longest path is said to be Depth of the tree. In simple
words, the highest depth of any leaf node in a tree is said to be depth of that tree. In
a tree, depth of the root node is '0'.

12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to
another node is called as PATH between that two Nodes. Length of a Path is total
number of nodes in that path. In below example the path A - B - E - J has length 4.
13. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.

Q.2) Binary Tree Traversal:

Types of Traversal of Binary Tree


There are three types of traversal of a binary tree.

1. Inorder tree traversal


2. Preorder tree traversal
3. Postorder tree traversal

Inorder Traversal:
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left->subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right->subtree)
Uses of Inorder Traversal:

In the case of binary search trees (BST), Inorder traversal gives nodes
in non-decreasing order. To get nodes of BST in non-increasing order,
a variation of Inorder traversal where Inorder traversal is reversed can
be used.

Preorder Traversal:
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left->subtree)
3. Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder:

Preorder traversal is used to create a copy of the tree. Preorder


traversal is also used to get prefix expressions on an expression tree.

Postorder Traversal:
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left->subtree)
2. Traverse the right subtree, i.e., call Postorder(right->subtree)
3. Visit the root

Uses of Postorder:

Postorder traversal is used to delete the tree. Please see the question
for the deletion of a tree for details. Postorder traversal is also useful to
get the postfix expression of an expression tree.

Q.3) Graph and it’s terminology:


A Graph is a non-linear data structure consisting
of nodes and edges. The nodes are sometimes also referred to as
vertices and the edges are lines or arcs that connect any two nodes in
the graph. More formally a Graph can be defined as, A Graph
consisting of a finite set of vertices(or nodes) and a set of edges that
connect a pair of nodes.

Terminology

A graph consists of:

 A set, V, of vertices (nodes)


 A collection, E, of pairs of vertices from V called edges (arcs)

Outgoing edges of a vertex are directed edges that the vertex is the origin.
Incoming edges of a vertex are directed edges that the vertex is the destination.

Degree of a vertex, v, denoted deg(v) is the number of incident edges.


Out-degree, outdeg(v), is the number of outgoing edges.
In-degree, indeg(v), is the number of incoming edges.

Parallel edges or multiple edges are edges of the same type and end-vertices.
Self-loop is an edge with the end vertices the same vertex.

More Terminology

Path is a sequence of alternating vetches and edges such that each successive vertex is
connected by the edge. Frequently only the vertices are listed especially if there are
no parallel edges.
Cycle is a path that starts and end at the same vertex.
Simple path is a path with distinct vertices.
Directed path is a path of only directed edges
Directed cycle is a cycle of only directed edges.

Sub-graph is a subset of vertices and edges.


Spanning sub-graph contains all the vertices.

Connected graph has all pairs of vertices connected by at least one path.
Connected component is the maximal connected sub-graph of a unconnected graph.
Forest is a graph without cycles.
Tree is a connected forest (previous type of trees are called rooted trees, these are free
trees)
Spanning tree is a spanning subgraph that is also a tree.
Q.4) Graph Tree and it’s applications:

Graph applications:
 In Computer science graphs are used to represent the flow of
computation.
 Google maps uses graphs for building transportation systems,
where intersection of two(or more) roads are considered to be a
vertex and the road connecting two vertices is considered to be an
edge, thus their navigation system is based on the algorithm to
calculate the shortest path between two vertices.
 In Facebook, users are considered to be the vertices and if they are
friends then there is an edge running between them. Facebook’s
Friend suggestion algorithm uses graph theory. Facebook is an
example of undirected graph.
 In World Wide Web, web pages are considered to be the vertices.
There is an edge from a page u to other page v if there is a link of
page v on page u. This is an example of Directed graph. It was the
basic idea behind Google Page Ranking Algorithm.
 In Operating System, we come across the Resource Allocation
Graph where each process and resources are considered to be
vertices. Edges are drawn from resources to the allocated process,
or from requesting process to the requested resource. If this leads
to any formation of a cycle then a deadlock will occur.
 In mapping system we use graph. It is useful to find out which is
an excellent place from the location as well as your nearby location.
In GPS we also use graphs.
 Facebook uses graphs. Using graphs suggests mutual friends. it
shows a list of the f following pages, friends, and contact list.
 Microsoft Excel uses DAG means Directed Acyclic Graphs.
 In the Dijkstra algorithm, we use a graph. we find the smallest
path between two or many nodes.
 On social media sites, we use graphs to track the data of the
users. liked showing preferred post suggestions, recommendations,
etc.
 Graphs are used in biochemical applications such as structuring of
protein, DNA etc.
Tree applications:
1. Store hierarchical data, like folder structure, organization structure,
XML/HTML data.
2. Binary Search Tree is a tree that allows fast search, insert, delete on a
sorted data. It also allows finding closest item
3. Heap is a tree data structure which is implemented using arrays and
used to implement priority queues.
4. B-Tree and B+ Tree : They are used to implement indexing in databases.
5. Syntax Tree: Scanning, parsing , generation of code and evaluation of
arithmetic expressions in Compiler design.
6. K-D Tree: A space partitioning tree used to organize points in K
dimensional space.
7. Trie : Used to implement dictionaries with prefix lookup.
8. Suffix Tree : For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges
respectively in computer networks
10. As a workflow for compositing digital images for visual effects.
11. Decision trees.
12. Organization chart of a large organization.
13. In XML parser.
14. Machine learning algorithm.
15. For indexing in database.
16. IN server like DNS (Domain Name Server)
17. In Computer Graphics.
18. To evaluate an expression.
19. In chess game to store defense moves of player.
20. In java virtual machine.
21. Tree data structures are used to organize and manage files and
directories in a file system. Each file and directory is represented as a
node in the tree, with parent-child relationships indicating the hierarchical
structure of the file system.
22. Tree data structures are often used in parsing, such as in compilers
and interpreters, to represent the structure of a program or a document.
23. Tree data structures, such as binary search trees, are commonly used
to implement efficient searching and sorting algorithms.
24. Graphics and UI design
25. Tree data structures are commonly used in decision-making
algorithms in artificial intelligence, such as game-playing algorithms,
expert systems, and decision trees.
26. Tree data structures can be used to represent the topology of a
network and to calculate routing tables for efficient data transmission.

Q.5) Tree Traversal Algorithm:

Types of Traversal of Binary Tree

There are three types of traversal of a binary tree.

4. Inorder tree traversal


5. Preorder tree traversal
6. Postorder tree traversal

Inorder Traversal:
Algorithm Inorder(tree)
4. Traverse the left subtree, i.e., call Inorder(left->subtree)
5. Visit the root.
6. Traverse the right subtree, i.e., call Inorder(right->subtree)

Uses of Inorder Traversal:

In the case of binary search trees (BST), Inorder traversal gives nodes
in non-decreasing order. To get nodes of BST in non-increasing order,
a variation of Inorder traversal where Inorder traversal is reversed can
be used.

Preorder Traversal:
Algorithm Preorder(tree)
4. Visit the root.
5. Traverse the left subtree, i.e., call Preorder(left->subtree)
6. Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder
traversal is also used to get prefix expressions on an expression tree.

Postorder Traversal:
Algorithm Postorder(tree)
4. Traverse the left subtree, i.e., call Postorder(left->subtree)
5. Traverse the right subtree, i.e., call Postorder(right->subtree)
6. Visit the root

Uses of Postorder:

Postorder traversal is used to delete the tree. Please see the question
for the deletion of a tree for details. Postorder traversal is also useful to
get the postfix expression of an expression tree.

Q.6) Graph Traversal Technique with Algorithm:

The graph has two types of traversal algorithms. These are called the
Breadth First Search and Depth First Search.

Breadth First Search (BFS)


The Breadth First Search (BFS) traversal is an algorithm, which is
used to visit all of the nodes of a given graph. In this traversal
algorithm one node is selected and then all of the adjacent nodes are
visited one by one. After completing all of the adjacent vertices, it
moves further to check another vertices and checks its adjacent
vertices again.

Algorithm
bfs(vertices, start)
Input: The list of vertices, and the start vertex.
Output: Traverse all of the nodes, if the graph is connected.
Begin
define an empty queue que
at first mark all nodes status as unvisited
add the start vertex into the que
while que is not empty, do
delete item from que and set to u
display the vertex u
for all vertices 1 adjacent with u, do
if vertices[i] is unvisited, then
mark vertices[i] as temporarily visited
add v into the queue
mark
done
mark u as completely visited
done
End

Depth First Search (DFS)


The Depth First Search (DFS) is a graph traversal algorithm. In this
algorithm one starting vertex is given, and when an adjacent vertex is
found, it moves to that adjacent vertex first and try to traverse in the
same manner.

Algorithm
dfs(vertices, start)
Input: The list of all vertices, and the start node.
Output: Traverse all nodes in the graph.
Begin
initially make the state to unvisited for all nodes
push start into the stack
while stack is not empty, do
pop element from stack and set to u
display the node u
if u is not visited, then
mark u as visited
for all nodes i connected to u, do
if ith vertex is unvisited, then
push ith vertex into the stack
mark ith vertex as visited
done
done
End

Q.7) BST and Application: A binary search tree follows some order
to arrange the elements. In a Binary search tree, the value of left node
must be smaller than the parent node, and the value of right node
must be greater than the parent node. This rule is applied recursively
to the left and right subtrees of the root.

Let's understand the concept of Binary search tree with an example.


In the above figure, we can observe that the root node is 40, and all
the nodes of the left subtree are smaller than the root node, and all
the nodes of the right subtree are greater than the root node.

Similarly, we can see the left child of root node is greater than its left
child and smaller than its right child. So, it also satisfies the property of
binary search tree. Therefore, we can say that the tree in the above
image is a binary search tree.

Suppose if we change the value of node 35 to 55 in the above tree,


check whether the tree will be binary search tree or not.
In the above tree, the value of root node is 40, which is greater than its
left child 30 but smaller than right child of 30, i.e., 55. So, the above
tree does not satisfy the property of Binary search tree. Therefore, the
above tree is not a binary search tree.

, let's see the algorithm to search an element in the Binary search tree.

Algorithm to search an element in Binary search tree


1. Search (root, item)
2. Step 1 - if (item = root → data) or (root = NULL)
3. return root
4. else if (item < root → data)
5. return Search(root → left, item)
6. else
7. return Search(root → right, item)
8. END if
9. Step 2 - END

Applications of Binary Search tree:


 BSTs are used for indexing.
 It is also used to implement various searching algorithms.
 IT can be used to implement various data structures.
 BSTs can be used in decision support systems to store and
quickly retrieve data.
 BSTs can be used to store and quickly retrieve data in
computer simulations.
 BSTs can be used to implement fast autocomplete systems.
 BSTs can be used to implement decision trees, which are
used in machine learning and artificial intelligence to model
decisions and predict outcomes. Decision trees are used in
many applications, including medical diagnosis, financial
analysis, and marketing research.
 BSTs can be used in encryption algorithms such as RSA,
which is a public-key encryption algorithm used in secure
communication protocols. RSA uses a BST to generate
public and private keys.
 BSTs can be used to compress data by storing frequently
occurring values in a smaller space and less frequently
occurring values in a larger space. This technique is used in
many applications, including image and audio compression,
data transmission, and file compression.
Real-time Application of Binary Search tree:
 BSTs are used for indexing in databases.
 It is used to implement searching algorithms.
 BSTs are used to implement Huffman coding algorithm.
 It is also used to implement dictionaries.
 Used for data caching.
 Used in Priority queues.
 Used in spell checkers.

You might also like