SlideShare a Scribd company logo
1 of 23
Download to read offline
Data Structure
M.Sc. Computer Science
I Semester
Ms. Arati Singh
Department of Computer Science
Unit-IV
DataStructureandAlgorithms-Tree
Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree
specifically.
Binary Tree is a special data structure used for data storage purposes. A binary tree has a special
condition that each node can have a maximum of two children. A binary tree has the benefits
of both an ordered array and a linked list as search is as quick as in a sorted array and insertion
or deletion operation are as fast as in linked list.
ImportantTerms
Following are the important terms with respect to tree.
Path βˆ’ Path refers to the sequence of nodes along the edges of a tree.
Root βˆ’ The node at the top of the tree is called root. There is only one root per tree and one
path from the root node to any node.
Parent βˆ’ Any node except the root node has one edge upward to a node called parent.
Child βˆ’ The node below a given node connected by its edge downward is called its child node.
Leaf βˆ’ The node which does not have any child node is called the leaf node.
Sub tree – Sub tree represents the descendants of a node.
Visiting βˆ’ Visiting refers to checking the value of a node when control is on the node.
Traversing βˆ’ Traversing means passing through nodes in a specific order.
Levels βˆ’ Level of a node represents the generation of a node. If the root node is at level 0, then
its next child node is at level 1, its grandchild is at level 2, and so on.
keys βˆ’ Key represents a value of a node based on which a search operation is to be carried out
for a node.
BinarySearchTreeRepresentation
Binary Search tree exhibits a special behaviour. A node's left child must have a value less than
its parent's value and the node's right child must have a value greater than its parent value.
We're going to implement tree using node object and connecting them through references.
TreeNode
The code to write a tree node would be similar to what is given below. It has a data part and
references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
In a tree, all nodes share common construct.
BSTBasicOperations
The basic operations that can be performed on a binary search tree data structure, are the
following βˆ’
Insert βˆ’ Inserts an element in a tree/create a tree.
Search βˆ’ Searches an element in a tree.
Preorder Traversal βˆ’ Traverses a tree in a pre-order manner.
Inorder Traversal βˆ’ Traverses a tree in an in-order manner.
Postorder Traversal βˆ’ Traverses a tree in a post-order manner.
We shall learn creating (inserting into) a tree structure and searching a data item in a tree in
this chapter. We shall learn about tree traversing methods in the coming chapter.
InsertOperation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first
locate its proper location. Start searching from the root node, then if the data is less than the
key value, search for the empty location in the left sub tree and insert the data. Otherwise,
search for the empty location in the right sub tree and insert the data.
Algorithm
If root is NULL
then create root node
return
If root exists then
compare the data with node.data
while until insertion position is located
If data is greater than node.data
goto right subtree
else
goto left subtree
endwhile
insert data
end If
Implementation
The implementation of insert function should look like this βˆ’
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty, create root node
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
//go to left of the tree
if(data < parent->data) {
current = current->leftChild;
//insert to the left
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
//go to right of the tree
else {
current = current->rightChild;
//insert to the right
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}
SearchOperation
Whenever an element is to be searched, start searching from the root node, then if the data is
less than the key value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.
Algorithm
If root.data is equal to search.data
return root
else
while data not found
If data is greater than node.data
goto right subtree
else
goto left subtree
If data found
return node
endwhile
return data not found
end if
The implementation of this algorithm should look like this.
struct node* search(int data) {
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//go to left tree
if(current->data > data) {
current = current->leftChild;
}
//else go to right tree
else {
current = current->rightChild;
}
//not found
if(current == NULL) {
return NULL;
}
return current;
}
}
To know about the implementation of binary search tree data structure.
DataStructure&Algorithms-TreeTraversal
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree
βˆ’
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all
the values it contains.
In-orderTraversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-
tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be βˆ’
D β†’ B β†’ E β†’ A β†’ F β†’ C β†’ G
Algorithm
Until all nodes are traversed βˆ’
Step 1 βˆ’ Recursively traverse left subtree.
Step 2 βˆ’ Visit root node.
Step 3 βˆ’ Recursively traverse right subtree.
Pre-orderTraversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its
left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be βˆ’
A β†’ B β†’ D β†’ E β†’ C β†’ F β†’ G
Algorithm
Until all nodes are traversed βˆ’
Step 1 βˆ’ Visit root node.
Step 2 βˆ’ Recursively traverse left subtree.
Step 3 βˆ’ Recursively traverse right subtree.
Post-orderTraversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-
order traversal of this tree will be βˆ’
D β†’ E β†’ B β†’ F β†’ G β†’ C β†’ A
Algorithm
Until all nodes are traversed βˆ’
Step 1 βˆ’ Recursively traverse left subtree.
Step 2 βˆ’ Recursively traverse right subtree.
Step 3 βˆ’ Visit root node.
DataStructure-BinarySearchTree
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties βˆ’
The left sub-tree of a node has a key less than or equal to its parent node's key.
The right sub-tree of a node has a key greater than to its parent node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree
and can be defined as βˆ’
left_subtree (keys) ≀ node (key) ≀ right_subtree (keys)
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node
has a key and an associated value. While searching, the desired key is compared to the keys in
BST and if found, the associated value is retrieved.
Following is a pictorial representation of BST βˆ’
We observe that the root node key (27) has all less-valued keys on the left sub-tree and the
higher valued keys on the right sub-tree.
BasicOperations
Following are the basic operations of a tree βˆ’
Search βˆ’ Searches an element in a tree.
Insert βˆ’ Inserts an element in a tree.
Pre-order Traversal βˆ’ Traverses a tree in a pre-order manner.
In-order Traversal βˆ’ Traverses a tree in an in-order manner.
Post-order Traversal βˆ’ Traverses a tree in a post-order manner.
Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
SearchOperation
Whenever an element is to be searched, start searching from the root node. Then if the data is
less than the key value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.
Algorithm
struct node* search(int data){
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data){
if(current != NULL) {
printf("%d ",current->data);
//go to left tree
if(current->data > data){
current = current->leftChild;
} //else go to right tree
else {
current = current->rightChild;
}
//not found
if(current == NULL){
return NULL;
}
}
} return current;
}
InsertOperation
Whenever an element is to be inserted, first locate its proper location. Start searching from the
root node, then if the data is less than the key value, search for the empty location in the left
subtree and insert the data. Otherwise, search for the empty location in the right subtree and
insert the data.
Algorithm
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
//go to left of the tree
if(data < parent->data) {
current = current->leftChild;
//insert to the left
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
} //go to right of the tree
else {
current = current->rightChild;
//insert to the right
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}
HeapDataStructures
Heap is a special case of balanced binary tree data structure where the root-node key is
compared with its children and arranged accordingly. If Ξ± has child node Ξ² then βˆ’
key(Ξ±) β‰₯ key(Ξ²)
As the value of parent is greater than that of child, this property generates Max Heap. Based
on this criteria, a heap can be of two types βˆ’
For Input β†’ 35 33 42 10 14 19 27 44 26 31
Min-Heap βˆ’ Where the value of the root node is less than or equal to either of its children.
Max-Heap βˆ’ Where the value of the root node is greater than or equal to either of its children.
Both trees are constructed using the same input and order of arrival.
MaxHeapConstructionAlgorithm
We shall use the same example to demonstrate how a Max Heap is created. The procedure to
create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any
point of time, heap must maintain its property. While insertion, we also assume that we are
inserting a node in an already heapified tree.
Step 1 βˆ’ Create a new node at the end of heap.
Step 2 βˆ’ Assign new value to the node.
Step 3 βˆ’ Compare the value of this child node with its parent.
Step 4 βˆ’ If value of parent is less than child, then swap them.
Step 5 βˆ’ Repeat step 3 & 4 until Heap property holds.
Note βˆ’ In Min Heap construction algorithm, we expect the value of the parent node to be less
than that of the child node.
Let's understand Max Heap construction by an animated illustration. We consider the same
input sample that we used earlier.
MaxHeapDeletionAlgorithm
Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always
happens at the root to remove the Maximum (or minimum) value.
Step 1 βˆ’ Remove root node.
Step 2 βˆ’ Move the last element of last level to root.
Step 3 βˆ’ Compare the value of this child node with its parent.
Step 4 βˆ’ If value of parent is less than child, then swap them.
Step 5 βˆ’ Repeat step 3 & 4 until Heap property holds.
Huffman Coding
Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length
codes to input characters, lengths of the assigned codes are based on the frequencies of
corresponding characters. The most frequent character gets the smallest code and the least
frequent character gets the largest code.
The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit
sequences) are assigned in such a way that the code assigned to one character is not prefix of
code assigned to any other character. This is how Huffman Coding makes sure that there is no
ambiguity when decoding the generated bit stream.
Let us understand prefix codes with a counter example. Let there be four characters a, b, c and
d, and their corresponding variable length codes be 00, 01, 0 and 1. This coding leads to
ambiguity because code assigned to c is prefix of codes assigned to a and b. If the compressed
bit stream is 0001, the de-compressed output may be β€œcccd” or β€œccb” or β€œacd” or β€œab”.
See this for applications of Huffman Coding.
There are mainly two major parts in Huffman Coding
1) Build a Huffman Tree from input characters.
2) Traverse the Huffman Tree and assign codes to characters.
Steps to build Huffman Tree
Input is array of unique characters along with their frequency of occurrences and output is
Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min
Heap is used as a priority queue. The value of frequency field is used to compare two nodes in
min heap. Initially, the least frequent character is at root)
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with frequency equal to the sum of the two nodes frequencies.
Make the first extracted node as its left child and the other extracted node as its right child.
Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root
node and the tree is complete.
Let us understand the algorithm with an example:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Step 1. Build a min heap that contains 6 nodes where each node represents root of a tree with
single node.
Step 2 Extract two minimum frequency nodes from min heap. Add a new internal node with
frequency 5 + 9 = 14.
Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each, and
one heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Step 3: Extract two minimum frequency nodes from heap. Add a new internal node with
frequency 12 + 13 = 25
Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each, and
two heap nodes are root of tree with more than one nodes.
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 +
16 = 30
Now min heap contains 3 nodes.
character Frequency
Internal Node 25
Internal Node 30
f 45
Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 +
30 = 55
Now min heap contains 2 nodes.
character Frequency
f 45
Internal Node 55
Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 +
55 = 100
Now min heap contains only one node.
character Frequency
Internal Node 100
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to
the left child, write 0 to the array. While moving to the right child, write 1 to the array. Print
the array when a leaf node is encountered.
The codes are as follows:
character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111
GraphTheoryTerminology
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed as vertices,
and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of
edges, connecting the pairs of vertices. Take a look at the following graph βˆ’
In the above graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
GraphinDataStructure
Mathematical graphs can be represented in data structure. We can represent a graph using an
array of vertices and a two-dimensional array of edges. Before we proceed further, let's
familiarize ourselves with some important terms βˆ’
β€’ Vertex βˆ’ Each node of the graph is represented as a vertex. In the following example,
the labeled circle represents vertices. Thus, A to G are vertices. We can represent them
using an array as shown in the following image. Here A can be identified by index 0.
B can be identified using index 1 and so on.
β€’ Edge βˆ’ Edge represents a path between two vertices or a line between two vertices. In
the following example, the lines from A to B, B to C, and so on represents edges. We
can use a two-dimensional array to represent an array as shown in the following image.
Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and
so on, keeping other combinations as 0.
β€’ Adjacency βˆ’ Two node or vertices are adjacent if they are connected to each other
through an edge. In the following example, B is adjacent to A, C is adjacent to B, and
so on.
β€’ Path βˆ’ Path represents a sequence of edges between the two vertices. In the following
example, ABCD represents a path from A to D.
BasicOperations
Following are basic primary operations of a Graph βˆ’
β€’ Add Vertex βˆ’ Adds a vertex to the graph.
β€’ Add Edge βˆ’ Adds an edge between the two vertices of the graph.
β€’ Display Vertex βˆ’ Displays a vertex of the graph.
Graph Data Structure and Algorithms
Recent Articles on Graph
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 consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of
nodes.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34,
04, 14, 13}.
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also
used in social networks like linked In, Facebook. For example, in Facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like
person id, name, gender, locale etc.
Graph and its representations
Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.
The pair is ordered because (u, v) is not same as (v, u) in case of a directed graph(di-graph).
The pair of the form (u, v) indicates that there is an edge from vertex u to vertex v. The edges
may contain weight/value/cost.
Graphs are used to represent many real-life applications: Graphs are used to represent
networks. The networks may include paths in a city or telephone network or circuit network.
Graphs are also used in social networks like linked In, Facebook. For example, in Facebook,
each person is represented with a vertex(or node). Each node is a structure and contains
information like person id, name, gender and locale. See this for more applications of graph.
Following is an example of an undirected graph with 5 vertices.
Following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of
the graph representation is situation specific. It totally depends on the type of operations to be
performed and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex
j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used
to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j
with weight w.
Adjacency Matrix in Graph
Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time.
Queries like whether there is an edge from vertex β€˜u’ to vertex β€˜v’ are efficient and can be done
O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of
edges), it consumes the same space. Adding a vertex is O(V^2) time.
Please see this for a sample Python implementation of adjacency matrix.
Adjacency List:
An array of lists is used. Size of the array is equal to the number of vertices. Let the array be
array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be
represented as lists of pairs. Following is adjacency list representation of the above
Reference
https://keithlyons.me/blog/2018/11/07/el30-graphing/
https://www.tutorialspoint.com/data_structures_algorithms/index.htm
https://www.geeksforgeeks.org/data-structures/

More Related Content

What's hot

1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
Β 
Binary tree
Binary treeBinary tree
Binary treeRajendran
Β 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREEER Punit Jain
Β 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)M Sajid R
Β 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 treesVictor Palmar
Β 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search treeMayeesha Samiha
Β 
Binary tree
Binary tree Binary tree
Binary tree Rajendran
Β 
Tree
TreeTree
Treebhumish
Β 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in javaIrfan CH
Β 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
Β 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data StructureAshim Lamichhane
Β 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
Β 

What's hot (20)

Binary tree
Binary  treeBinary  tree
Binary tree
Β 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Β 
Binary tree
Binary treeBinary tree
Binary tree
Β 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
Β 
Binary trees
Binary treesBinary trees
Binary trees
Β 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
Β 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
Β 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Β 
Tree
TreeTree
Tree
Β 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Β 
Binary tree
Binary treeBinary tree
Binary tree
Β 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
Β 
Binary search trees
Binary search treesBinary search trees
Binary search trees
Β 
Binary tree
Binary tree Binary tree
Binary tree
Β 
Tree
TreeTree
Tree
Β 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
Β 
Tree data structure
Tree data structureTree data structure
Tree data structure
Β 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
Β 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Β 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
Β 

Similar to Unit iv data structure-converted

TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxasimshahzad8611
Β 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
Β 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docxsowmya koneru
Β 
Binary search tree
Binary search treeBinary search tree
Binary search treemaamir farooq
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
Β 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptxskilljiolms
Β 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
Β 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
Β 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
Β 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
Β 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
Β 
Part 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docxPart 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docxherbertwilson5999
Β 
binarysearch-180319163432.pdf 11111111111
binarysearch-180319163432.pdf 11111111111binarysearch-180319163432.pdf 11111111111
binarysearch-180319163432.pdf 11111111111ArsalanAslam23
Β 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationnakulvarshney371
Β 
Binary search tree
Binary search treeBinary search tree
Binary search treeKousalya M
Β 

Similar to Unit iv data structure-converted (20)

TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
Β 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Β 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
Β 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Β 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Β 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
Β 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Β 
Trees
TreesTrees
Trees
Β 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Β 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Β 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
Β 
Trees
TreesTrees
Trees
Β 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Β 
Binary tree
Binary treeBinary tree
Binary tree
Β 
Binary tree
Binary treeBinary tree
Binary tree
Β 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
Β 
Part 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docxPart 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docx
Β 
binarysearch-180319163432.pdf 11111111111
binarysearch-180319163432.pdf 11111111111binarysearch-180319163432.pdf 11111111111
binarysearch-180319163432.pdf 11111111111
Β 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
Β 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Β 

More from Shri Shankaracharya College, Bhilai,Junwani

More from Shri Shankaracharya College, Bhilai,Junwani (20)

Environment Economics &Ethics invisible hand & Malthusian theory
Environment Economics &Ethics invisible hand & Malthusian theoryEnvironment Economics &Ethics invisible hand & Malthusian theory
Environment Economics &Ethics invisible hand & Malthusian theory
Β 
Azadi ka amrut mahotsav, Mahilayon ka yogdan swatantrata Sangram mein
Azadi ka amrut mahotsav, Mahilayon ka yogdan swatantrata Sangram meinAzadi ka amrut mahotsav, Mahilayon ka yogdan swatantrata Sangram mein
Azadi ka amrut mahotsav, Mahilayon ka yogdan swatantrata Sangram mein
Β 
B.ed 1,scientific temper
B.ed 1,scientific temperB.ed 1,scientific temper
B.ed 1,scientific temper
Β 
Aims and objectives of bio. sci. 14 9-20
Aims and objectives of bio. sci. 14 9-20Aims and objectives of bio. sci. 14 9-20
Aims and objectives of bio. sci. 14 9-20
Β 
Ict application in bio.sc.24 9
Ict application in bio.sc.24 9Ict application in bio.sc.24 9
Ict application in bio.sc.24 9
Β 
Runges kutta method
Runges kutta methodRunges kutta method
Runges kutta method
Β 
Isolation & preservation of culture of microorganism
Isolation & preservation of  culture of microorganismIsolation & preservation of  culture of microorganism
Isolation & preservation of culture of microorganism
Β 
Learners understanding,unit 1, 15-9-20
Learners understanding,unit 1, 15-9-20Learners understanding,unit 1, 15-9-20
Learners understanding,unit 1, 15-9-20
Β 
Basics concept of physical chemistry
Basics concept of physical chemistryBasics concept of physical chemistry
Basics concept of physical chemistry
Β 
equilibrium of Firm
equilibrium  of Firmequilibrium  of Firm
equilibrium of Firm
Β 
indifference curve
 indifference curve indifference curve
indifference curve
Β 
Equilibrium
  Equilibrium  Equilibrium
Equilibrium
Β 
Crystal field theory
Crystal field theoryCrystal field theory
Crystal field theory
Β 
Utility
UtilityUtility
Utility
Β 
New economic reform
New economic reform New economic reform
New economic reform
Β 
Iso product Curve
Iso product CurveIso product Curve
Iso product Curve
Β 
Malnutrition
MalnutritionMalnutrition
Malnutrition
Β 
Demand theory
Demand theoryDemand theory
Demand theory
Β 
Land reform
Land reformLand reform
Land reform
Β 
Isomerism
IsomerismIsomerism
Isomerism
Β 

Recently uploaded

(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)riyaescorts54
Β 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptArshadWarsi13
Β 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPirithiRaju
Β 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
Β 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
Β 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
Β 
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.aasikanpl
Β 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxPABOLU TEJASREE
Β 
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.aasikanpl
Β 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
Β 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxBerniceCayabyab1
Β 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
Β 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
Β 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naJASISJULIANOELYNV
Β 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
Β 
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
Β 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
Β 
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |aasikanpl
Β 

Recently uploaded (20)

(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
Β 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
Β 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.ppt
Β 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Β 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
Β 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
Β 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
Β 
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Mayapuri Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Β 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
Β 
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Call Girls in Munirka Delhi πŸ’―Call Us πŸ”9953322196πŸ” πŸ’―Escort.
Β 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
Β 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
Β 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
Β 
Hot Sexy call girls in Moti Nagar,πŸ” 9953056974 πŸ” escort Service
Hot Sexy call girls in  Moti Nagar,πŸ” 9953056974 πŸ” escort ServiceHot Sexy call girls in  Moti Nagar,πŸ” 9953056974 πŸ” escort Service
Hot Sexy call girls in Moti Nagar,πŸ” 9953056974 πŸ” escort Service
Β 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
Β 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by na
Β 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
Β 
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❀️8860477959 EscorTs Service In 24/7 Delh...
Β 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
Β 
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 β‰Ό Call Girls In Lajpat Nagar (Delhi) |
Β 

Unit iv data structure-converted

  • 1. Data Structure M.Sc. Computer Science I Semester Ms. Arati Singh Department of Computer Science
  • 2. Unit-IV DataStructureandAlgorithms-Tree Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree specifically. Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in linked list. ImportantTerms Following are the important terms with respect to tree. Path βˆ’ Path refers to the sequence of nodes along the edges of a tree. Root βˆ’ The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node. Parent βˆ’ Any node except the root node has one edge upward to a node called parent. Child βˆ’ The node below a given node connected by its edge downward is called its child node. Leaf βˆ’ The node which does not have any child node is called the leaf node. Sub tree – Sub tree represents the descendants of a node. Visiting βˆ’ Visiting refers to checking the value of a node when control is on the node. Traversing βˆ’ Traversing means passing through nodes in a specific order. Levels βˆ’ Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
  • 3. keys βˆ’ Key represents a value of a node based on which a search operation is to be carried out for a node. BinarySearchTreeRepresentation Binary Search tree exhibits a special behaviour. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value. We're going to implement tree using node object and connecting them through references. TreeNode The code to write a tree node would be similar to what is given below. It has a data part and references to its left and right child nodes. struct node { int data; struct node *leftChild; struct node *rightChild; }; In a tree, all nodes share common construct. BSTBasicOperations The basic operations that can be performed on a binary search tree data structure, are the following βˆ’ Insert βˆ’ Inserts an element in a tree/create a tree. Search βˆ’ Searches an element in a tree. Preorder Traversal βˆ’ Traverses a tree in a pre-order manner. Inorder Traversal βˆ’ Traverses a tree in an in-order manner. Postorder Traversal βˆ’ Traverses a tree in a post-order manner.
  • 4. We shall learn creating (inserting into) a tree structure and searching a data item in a tree in this chapter. We shall learn about tree traversing methods in the coming chapter. InsertOperation The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left sub tree and insert the data. Otherwise, search for the empty location in the right sub tree and insert the data. Algorithm If root is NULL then create root node return If root exists then compare the data with node.data while until insertion position is located If data is greater than node.data goto right subtree else goto left subtree endwhile insert data end If Implementation The implementation of insert function should look like this βˆ’ void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty, create root node if(root == NULL) { root = tempNode; } else { current = root; parent = NULL;
  • 5. while(1) { parent = current; //go to left of the tree if(data < parent->data) { current = current->leftChild; //insert to the left if(current == NULL) { parent->leftChild = tempNode; return; } } //go to right of the tree else { current = current->rightChild; //insert to the right if(current == NULL) { parent->rightChild = tempNode; return; } } } } } SearchOperation Whenever an element is to be searched, start searching from the root node, then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node. Algorithm If root.data is equal to search.data return root else while data not found If data is greater than node.data goto right subtree else goto left subtree If data found return node endwhile return data not found
  • 6. end if The implementation of this algorithm should look like this. struct node* search(int data) { struct node *current = root; printf("Visiting elements: "); while(current->data != data) { if(current != NULL) printf("%d ",current->data); //go to left tree if(current->data > data) { current = current->leftChild; } //else go to right tree else { current = current->rightChild; } //not found if(current == NULL) { return NULL; } return current; } } To know about the implementation of binary search tree data structure. DataStructure&Algorithms-TreeTraversal Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree βˆ’ In-order Traversal Pre-order Traversal Post-order Traversal Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains.
  • 7. In-orderTraversal In this traversal method, the left subtree is visited first, then the root and later the right sub- tree. We should always remember that every node may represent a subtree itself. If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be βˆ’ D β†’ B β†’ E β†’ A β†’ F β†’ C β†’ G Algorithm Until all nodes are traversed βˆ’ Step 1 βˆ’ Recursively traverse left subtree. Step 2 βˆ’ Visit root node. Step 3 βˆ’ Recursively traverse right subtree. Pre-orderTraversal In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
  • 8. We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be βˆ’ A β†’ B β†’ D β†’ E β†’ C β†’ F β†’ G Algorithm Until all nodes are traversed βˆ’ Step 1 βˆ’ Visit root node. Step 2 βˆ’ Recursively traverse left subtree. Step 3 βˆ’ Recursively traverse right subtree. Post-orderTraversal In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. We start from A, and following Post-order traversal, we first visit the left subtree B. B is also traversed post-order. The process goes on until all the nodes are visited. The output of post- order traversal of this tree will be βˆ’
  • 9. D β†’ E β†’ B β†’ F β†’ G β†’ C β†’ A Algorithm Until all nodes are traversed βˆ’ Step 1 βˆ’ Recursively traverse left subtree. Step 2 βˆ’ Recursively traverse right subtree. Step 3 βˆ’ Visit root node. DataStructure-BinarySearchTree A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties βˆ’ The left sub-tree of a node has a key less than or equal to its parent node's key. The right sub-tree of a node has a key greater than to its parent node's key. Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be defined as βˆ’ left_subtree (keys) ≀ node (key) ≀ right_subtree (keys) Representation BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a key and an associated value. While searching, the desired key is compared to the keys in BST and if found, the associated value is retrieved. Following is a pictorial representation of BST βˆ’ We observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher valued keys on the right sub-tree. BasicOperations Following are the basic operations of a tree βˆ’ Search βˆ’ Searches an element in a tree.
  • 10. Insert βˆ’ Inserts an element in a tree. Pre-order Traversal βˆ’ Traverses a tree in a pre-order manner. In-order Traversal βˆ’ Traverses a tree in an in-order manner. Post-order Traversal βˆ’ Traverses a tree in a post-order manner. Node Define a node having some data, references to its left and right child nodes. struct node { int data; struct node *leftChild; struct node *rightChild; }; SearchOperation Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node. Algorithm struct node* search(int data){ struct node *current = root; printf("Visiting elements: "); while(current->data != data){ if(current != NULL) { printf("%d ",current->data); //go to left tree if(current->data > data){ current = current->leftChild; } //else go to right tree else { current = current->rightChild; } //not found if(current == NULL){ return NULL; } } } return current; } InsertOperation Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left
  • 11. subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert the data. Algorithm void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode; } else { current = root; parent = NULL; while(1) { parent = current; //go to left of the tree if(data < parent->data) { current = current->leftChild; //insert to the left if(current == NULL) { parent->leftChild = tempNode; return; } } //go to right of the tree else { current = current->rightChild; //insert to the right if(current == NULL) { parent->rightChild = tempNode; return; } } } } } HeapDataStructures Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If Ξ± has child node Ξ² then βˆ’
  • 12. key(Ξ±) β‰₯ key(Ξ²) As the value of parent is greater than that of child, this property generates Max Heap. Based on this criteria, a heap can be of two types βˆ’ For Input β†’ 35 33 42 10 14 19 27 44 26 31 Min-Heap βˆ’ Where the value of the root node is less than or equal to either of its children. Max-Heap βˆ’ Where the value of the root node is greater than or equal to either of its children. Both trees are constructed using the same input and order of arrival. MaxHeapConstructionAlgorithm We shall use the same example to demonstrate how a Max Heap is created. The procedure to create Min Heap is similar but we go for min values instead of max values. We are going to derive an algorithm for max heap by inserting one element at a time. At any point of time, heap must maintain its property. While insertion, we also assume that we are inserting a node in an already heapified tree. Step 1 βˆ’ Create a new node at the end of heap. Step 2 βˆ’ Assign new value to the node. Step 3 βˆ’ Compare the value of this child node with its parent. Step 4 βˆ’ If value of parent is less than child, then swap them. Step 5 βˆ’ Repeat step 3 & 4 until Heap property holds.
  • 13. Note βˆ’ In Min Heap construction algorithm, we expect the value of the parent node to be less than that of the child node. Let's understand Max Heap construction by an animated illustration. We consider the same input sample that we used earlier. MaxHeapDeletionAlgorithm Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always happens at the root to remove the Maximum (or minimum) value. Step 1 βˆ’ Remove root node. Step 2 βˆ’ Move the last element of last level to root. Step 3 βˆ’ Compare the value of this child node with its parent. Step 4 βˆ’ If value of parent is less than child, then swap them. Step 5 βˆ’ Repeat step 3 & 4 until Heap property holds.
  • 14. Huffman Coding Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters. The most frequent character gets the smallest code and the least frequent character gets the largest code. The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences) are assigned in such a way that the code assigned to one character is not prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the generated bit stream. Let us understand prefix codes with a counter example. Let there be four characters a, b, c and d, and their corresponding variable length codes be 00, 01, 0 and 1. This coding leads to ambiguity because code assigned to c is prefix of codes assigned to a and b. If the compressed bit stream is 0001, the de-compressed output may be β€œcccd” or β€œccb” or β€œacd” or β€œab”. See this for applications of Huffman Coding. There are mainly two major parts in Huffman Coding 1) Build a Huffman Tree from input characters. 2) Traverse the Huffman Tree and assign codes to characters. Steps to build Huffman Tree Input is array of unique characters along with their frequency of occurrences and output is Huffman Tree. 1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min Heap is used as a priority queue. The value of frequency field is used to compare two nodes in min heap. Initially, the least frequent character is at root) 2. Extract two nodes with the minimum frequency from the min heap. 3. Create a new internal node with frequency equal to the sum of the two nodes frequencies. Make the first extracted node as its left child and the other extracted node as its right child. Add this node to the min heap. 4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root node and the tree is complete.
  • 15. Let us understand the algorithm with an example: character Frequency a 5 b 9 c 12 d 13 e 16 f 45 Step 1. Build a min heap that contains 6 nodes where each node represents root of a tree with single node. Step 2 Extract two minimum frequency nodes from min heap. Add a new internal node with frequency 5 + 9 = 14. Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each, and one heap node is root of tree with 3 elements character Frequency c 12 d 13 Internal Node 14 e 16 f 45 Step 3: Extract two minimum frequency nodes from heap. Add a new internal node with frequency 12 + 13 = 25
  • 16. Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each, and two heap nodes are root of tree with more than one nodes. character Frequency Internal Node 14 e 16 Internal Node 25 f 45 Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30 Now min heap contains 3 nodes. character Frequency Internal Node 25 Internal Node 30 f 45 Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55
  • 17. Now min heap contains 2 nodes. character Frequency f 45 Internal Node 55 Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100 Now min heap contains only one node. character Frequency Internal Node 100 Since the heap contains only one node, the algorithm stops here. Steps to print codes from Huffman Tree: Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to the left child, write 0 to the array. While moving to the right child, write 1 to the array. Print the array when a leaf node is encountered.
  • 18. The codes are as follows: character code-word f 0 c 100 d 101 a 1100 b 1101 e 111 GraphTheoryTerminology A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges. Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of vertices. Take a look at the following graph βˆ’ In the above graph, V = {a, b, c, d, e}
  • 19. E = {ab, ac, bd, cd, de} GraphinDataStructure Mathematical graphs can be represented in data structure. We can represent a graph using an array of vertices and a two-dimensional array of edges. Before we proceed further, let's familiarize ourselves with some important terms βˆ’ β€’ Vertex βˆ’ Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices. Thus, A to G are vertices. We can represent them using an array as shown in the following image. Here A can be identified by index 0. B can be identified using index 1 and so on. β€’ Edge βˆ’ Edge represents a path between two vertices or a line between two vertices. In the following example, the lines from A to B, B to C, and so on represents edges. We can use a two-dimensional array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0. β€’ Adjacency βˆ’ Two node or vertices are adjacent if they are connected to each other through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so on. β€’ Path βˆ’ Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a path from A to D.
  • 20. BasicOperations Following are basic primary operations of a Graph βˆ’ β€’ Add Vertex βˆ’ Adds a vertex to the graph. β€’ Add Edge βˆ’ Adds an edge between the two vertices of the graph. β€’ Display Vertex βˆ’ Displays a vertex of the graph. Graph Data Structure and Algorithms Recent Articles on Graph 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 consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes. In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34, 04, 14, 13}. Graphs are used to solve many real-life problems. Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linked In, Facebook. For example, in Facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender, locale etc.
  • 21. Graph and its representations Graph is a data structure that consists of following two components: 1. A finite set of vertices also called as nodes. 2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in case of a directed graph(di-graph). The pair of the form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost. Graphs are used to represent many real-life applications: Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linked In, Facebook. For example, in Facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender and locale. See this for more applications of graph. Following is an example of an undirected graph with 5 vertices. Following two are the most commonly used representations of a graph. 1. Adjacency Matrix 2. Adjacency List There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph representation is situation specific. It totally depends on the type of operations to be performed and ease of use. Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used
  • 22. to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. Adjacency Matrix in Graph Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex β€˜u’ to vertex β€˜v’ are efficient and can be done O(1). Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time. Please see this for a sample Python implementation of adjacency matrix. Adjacency List: An array of lists is used. Size of the array is equal to the number of vertices. Let the array be array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs. Following is adjacency list representation of the above