Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Binary Tree

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

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.

Binary Tree Representation

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

Basic Operation On Binary Tree:

Inserting an element.
Removing an element.
Searching for an element.
Traversing the tree.
There are four types of Binary tree:

Full/ proper/ strict Binary tree


Complete Binary tree
Perfect Binary tree
Degenerate Binary tree
Balanced Binary tree

1. Full/ proper/ strict 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.

Complete 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.

Perfect Binary Tree


A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf nodes are
at the same level.
The below tree is not a perfect binary tree because all the leaf nodes are not
at the same level.

Degenerate Binary Tree

The degenerate binary tree is a tree in which all the internal nodes have only
one children.

Let's understand the Degenerate binary tree through examples.

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.

Graph and its representations


What is Graph Data Structure?
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).

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.

● If there is an edge from vertex i to j, mark adjMat[i][j] as 1.


● If there is no edge from vertex i to j, mark adjMat[i][j] as 0.
Representation of Undirected Graph to Adjacency Matrix:

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.

Representation of Directed Graph to Adjacency Matrix:

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

In this representation, every vertex of a graph contains list of its


adjacent vertices.each vertex in the graph has a list of its neighbors.

For example, consider the following directed graph representation


implemented using linked list…

Directed Graph Representation

Undirected Graph Representation


Degree
In the tree data structure, the total number of children of a node is called
the degree of the node.
The highest degree of the node among all the nodes in a tree is called the
Degree of Tree.

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.

What is Weighted Graph?


A weighted graph is defined as a special type of graph in which the edges are
assigned some weights which represent cost, distance, and many other relative
measuring units.

Given below is an example of a weighted graph:


The graph consists of five nodes and seven edges (each has a weight). For example,
the edge {0,3} has the weight 7, the edge {1,2}has the weight 1.
Weighted graphs are often used to model real objects and relationships between nodes.
The above example can be considered as a part of Google maps, where the nodes are
cities and the edges are roads. The weight of each edge is the time/distance between
two cities.
The above example is a weighted undirected graph with undirected edges. But
weighted graphs can also be directed. Given below is an example of a directed
weighted graph:

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.

Tree traversal (Inorder, Preorder


an Postorder)
The term 'tree traversal' means traversing or visiting each node of a tree.
There is a single way to traverse the linear data structure such as linked list,
queue, and stack. Whereas,
there are multiple ways to traverse a tree that are listed as follows -

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

Step 1 - Visit the root node


Step 2 - Traverse the left subtree recursively.
Step 3 - Traverse the right subtree recursively.

Example

Now, let's see the example of the preorder traversal technique.


the output of the preorder traversal of the above tree is
-A → B → D → E → C → F → G

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

1. Until all nodes of the tree are not visited

2.

3. Step 1 - Traverse the left subtree recursively.

4. Step 2 - Visit the root node.

5. Step 3 - Traverse the right subtree recursively.


Example

Now, let's see the example of the Inorder traversal technique.

The output of the inorder traversal of the above tree is -

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).

there are different types of graphs, such as:

● 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

Another name for the directed graphs is digraphs. A graph is called a


directed graph or digraph if all the edges present between any vertices or
nodes of the graph are directed or have a defined direction. By directed
edges, we mean the edges of the graph that have a direction to determine
from which node it is starting and at which node it is ending.
Connected Graph

For a graph to be labelled as a connected graph, there must be at least a


single path between every pair of the graph's vertices. In other words, we
can say that if we start from one vertex, we should be able to move to any
of the vertices that are present in that particular graph, which means there
exists at least one path between all the vertices of the graph.
Disconnected 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.

Construct Special Binary Tree from


given Inorder traversal
Given Inorder Traversal of a Special Binary Tree in which the key of every node is
greater than keys in left and right children, construct the Binary Tree and return
root.

Examples:

Input: inorder[] = {5, 10, 40, 30, 28}


Output: root of following tree
40
/ \
10 30
/ \
5 28

Input: inorder[] = {1, 5, 10, 40, 30,


15, 28, 20}
Output: root of following tree
40
/ \
10 30
/ \
5 28
/ / \
1 15 20
Let the given array is {1, 5, 10, 40, 30, 15, 28, 20}. The maximum element in the
given array must be root. The elements on the left side of the maximum element
are in the left subtree and the elements on the right side are in the right subtree.

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

Find Adjacency List Representation of Graph

Undirected Graph
Adjacency List of Undirected Graph

Directed Graph

Adjacency List of Directed Graph


Find Adjacency Matrix using the given graph.

If a graph has n vertices, we use n x n matrix to represent the graph.

Let's assume the n x n matrix as adj[n][n].

if there is an edge from vertex i to j, mark adj[i][j] as 1. i.e. adj[i][j] == 1

if there is no edge from vertex i to j, mark adj[i][j] as 0. i.e. adj[i][j] == 0


Inserting element in a Binary search tree.
Breadth-first traversal (BFS)

Breadth-first traversal (BFS) is a graph traversal algorithm that explores all


the vertices of a graph or tree in breadth-first order. Breadth First Traversal
in data structure starts at a given vertex (or root in the case of a tree) and
visits all the vertices at the same level before moving to the next level.How
does BFS work?
Starting from the root, all the nodes at a particular level are visited first and
then the nodes of the next level are traversed till all the nodes are visited.
Huffman Coding

Huffman Coding is a technique of compressing data to reduce its size


without losing any of the details.

How Huffman Coding works?

Suppose the string below is to be sent over a network.

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.

You might also like