Unit 4
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.
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.
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:
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.
Terminology
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.
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.
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.
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)
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.
The graph has two types of traversal algorithms. These are called the
Breadth First Search and Depth First Search.
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
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.
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.
, let's see the algorithm to search an element in the Binary search tree.