Binary Tree
Binary Tree
Binary Tree
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since
each element in a binary tree can have only 2 children, we typically name them the left and right
child.
A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of
the tree. If the tree is empty, then the value of the root is NULL. Each node of a Binary Tree
contains the following parts:
Data
Pointer to left child
Pointer to right child
Inserting an element.
Removing an element.
Searching for an element.
Traversing the tree.
There are four types of Binary tree:
The full binary tree is also known as a strict binary tree. The tree can only be considered as the
full binary tree if each node must contain either 0 or 2 children. The full binary tree can also be
defined as the tree in which each node must contain 2 children except the leaf nodes.
In the above tree, we can observe that each node is either containing zero or two children;
therefore, it is a Full Binary tree.
The complete binary tree is a tree in which all the nodes are completely filled except the last
level. In the last level, all the nodes must be as left as possible. In a complete binary tree, the
nodes should be added from the left.
The above tree is a complete binary tree because all the nodes are completely filled, and all the
nodes in the last level are added at the left first.
The degenerate binary tree is a tree in which all the internal nodes have only
one children.
The above tree is a degenerate binary tree because all the nodes have only
one child. It is also known as a right-skewed tree as all the nodes have a
right child only.
The above tree is also a degenerate binary tree because all the nodes have
only one child. It is also known as a left-skewed tree as all the nodes have a
left child only.
Representations of Graph
Here are the two most common ways to represent a graph :
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s
and 1’s).
Let’s assume there are n vertices in the graph So, create a 2D matrix adjMat[n][n]
having dimension n x n.
The below figure shows an undirected graph. Initially, the entire Matrix is initialized
to 0. If there is an edge from source to destination, we insert 1 to both cases
(adjMat[destination] and adjMat[destination]) because we can go either way.
The below figure shows a directed graph. Initially, the entire Matrix is initialized to 0.
If there is an edge from source to destination, we insert 1 for that particular
adjMat[destination].
Adjacency List Representation
To do this we simply divide the summation of all nodes’ degree by the total
number of nodes. For example in the graph above the nodes have the
following degrees: A=2, B=2, C=4, D=2, E=3, F=2, G=2, H=1. Adding
these all together we get 18, and since there are 8 nodes the average
degree is 18 divided by 8, or 2.25.
Path
The shortest paths are the first two. Note that as the direction is not
important the paths are symmetrica path is a sequence of nodes in which
each node is connected by an edge to the next.
The path length corresponds to the number of edges in the path. For
example, in the network above the paths between A and F are: ACDF, ACEF,
ABCDF, ABCEF, with path lengths 3,3,4,4 respectively.
Weighted directed graphs can also be used to model some real processes. For
instance, the node 0 in the example above can be considered as a storage place, from
where we need to transfer some resources to the destination point, say to the node 2.
The remaining nodes can be considered as intermediate places. Each edge shows a
direction in which the resources can be transferred. The weight of an edge shows the
maximum amount of resources that can be sent through it.
Preorder traversal
Inorder traversal
Postorder traversal
Preorder traversal
This technique follows the 'root left right' policy. It means that, first root
node is visited after that the left subtree is traversed recursively, and finally,
right subtree is recursively traversed. As the root node is traversed before
(or pre) the left and right subtree, it is called preorder traversal.
Algorithm
Example
Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left
subtree of the root node is traversed, after that recursively traverses the
right subtree, and finally, the root node is traversed. As the root node is
traversed after (or post) the left and right subtree, it is called postorder
traversal.
Algorithm
Step 1 - Traverse the left subtree recursively.
Step 2 - Traverse the right subtree recursively.
Step 3 - Visit the root node.
Example
the output of the postorder traversal of the above tree is -
D→E→B→F→G→C→A
Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is
visited after that root node is traversed, and finally, the right subtree is traversed.
As the root node is traversed between the left and right subtree, it is named
inorder traversal.
Algorithm
2.
D→B→E→A→F→C→G
Connected component
Connected component in an undirected graph refers to a group of vertices that are
connected to each other through edges, but not connected to other vertices outside the
group.
For example in the graph shown below, {0, 1, 2} form a connected component and {3, 4}
form another connected component.
the undirected graph has three connected components:
Types of Graph
A Graph is a non-linear data structure consisting of vertices and edges. The
vertices are sometimes also referred to as nodes and the edges are lines or arcs
that connect any two nodes in the graph. More formally a Graph is composed of a
set of vertices( V ) and a set of edges( E ). The graph is denoted by G(V, E).
● Null Graph
● Trivial Graph
● Non-directed Graph
● Directed Graph
● Connected Graph
● Disconnected Graph
● Regular Graph
● Complete Graph
● Cycle Graph
● Cyclic Graph
● Acyclic Graph
Null Graph
The Null Graph is also known as the order zero graph. The term "null graph"
refers to a graph with an empty edge set. In other words, a null graph has
no edges, and the null graph is present with only isolated vertices in the
graph.
Trivial Graph
A graph is called a trivial graph if it has only one vertex present in it. The
trivial graph is the smallest possible graph that can be created with the least
number of vertices that is one vertex only.
Non-Directed Graph
A graph is called a non-directed graph if all the edges present between any
graph nodes are non-directed. By non-directed edges, we mean the edges of
the graph that cannot be determined from the node it is starting and at
which node it is ending. All the edges for a graph need to be non-directed to
call it a non-directed graph. All the edges of a non-directed graph don't have
any direction.
Directed Graph
A graph is said to be a disconnected graph where there does not exist any
path between at least one pair of vertices.
Regular Graph
If all the graph nodes have the same degree value, then the graph is called
a regular graph.If all the vertices in a graph are of degree 'k', then it is
called a "k-regular graph".
The graphs that are displayed above are regular graphs. In graph 1, there
are three vertices named vertex A, vertex B, and vertex C, All the vertices in
graph 1, have the degree of each node as 2. The degree of each vertex is
calculated by counting the number of edges connected to that particular
vertex.the second graph shown above, there are four vertices named vertex
E, vertex F, vertex G, and vertex F. The degree of all the four vertices of this
graph is 2. Each vertex of the graph is 2 because only two edges are
associated with all of the graph's vertices. As all the nodes of this graph
have the same degree of 2, this graph is called a regular graph.
Complete Graph
all the vertices are connected to the rest of all the vertices of the graph.
Cycle graph
The graph in which the graph forms cycle.
Cyclic Graph
For a graph to be called a cyclic graph, it should consist of at least one cycle.
If a graph has a minimum of one cycle present, it is called a cyclic graph.
Acyclic Graph
A graph is called an acyclic graph if zero cycles are present, and an acyclic
graph is the complete opposite of a cyclic graph.
Examples:
40
/ \
{1,5,10} {30,15,28,20}
We recursively follow the above step for left and right subtrees, and finally, get the
following tree.
40
/ \
10 30
/ \
5 28
/ / \
1 15 20
Undirected Graph
Adjacency List of Undirected Graph
Directed Graph
Initial string
Each character occupies 8 bits. There are a total of 15 characters in the
above string. Thus, a total of 8 * 15 = 120 bits are required to send this string.