Data Type
Data Type
Data type is a classification identifying one of various types of data, such as floating-point, integer, or
Boolean, that determines the possible values for that type; the operations that can be done on values of
that type; and the way values of that type can be stored. It is of two types: Primitive and non-primitive
data type. Primitive data type is the basic data type that is provided by the programming language with
built-in support. This data type is native to the language and is supported by machine directly while
non-primitive data type is derived from primitive data type. For example- array, structure etc.
1. Integers,
2. Booleans,
3. Characters,
4. Floating-point numbers,
5. Alphanumeric strings.
.
❖ Data structure
In Computer Science, a data structure is a way of organizing information, so that it is easier to use. Data
structures determine the way in which information can be stored in computer and used. If the focus of
use is on the things that can be done, people often talk about an abstract data type (ADT). Data
structures are often optimized for certain operations. Finding the best data structure when solving a
problem is an important part of programming. Programs that use the right data structure are easier to
write, and work better.
1
S.NO Linear Data Structure Non-linear Data Structure
In linear data structure, single level is Whereas in non-linear data structure, multiple
2.
involved. levels are involved.
In linear data structure, data elements can be While in non-linear data structure, data elements
4.
traversed in a single run only. can’t be traversed in a single run only.
Applications of linear data structures are Applications of non-linear data structures are in
7.
mainly in application software development. Artificial Intelligence and image processing.
1. A primitive data type is one that fits the base architecture of the underlying computer such as
int, float, and pointer, and all the variations, thereof such as char short long unsigned float double
etc., are primitive data type.
2. Primitive data are only single values; they have not special capabilities.
4. The integer reals, logic data character data pointer and reference are primitive data structures
data structure that normally are directly operated upon by machine level instructions are known as
primitive structure and data type.
1. A non-primitive data type is something else such as an array structure or class is known as the
non-primitive data type.
2. The data type that are derived from primary data types are known as non-primitive data type.
3. The non-primitive data types are used to store the group of values.
The basic operations that are performed on data structures are as follows:
1. Traversing: Accessing each record exactly once so that certain items in the record may
be processed. (This accessing and processing is sometimes called “visiting” the record).
2. Searching: Searching operation finds the presence of the desired data item in the list of
data item. It may also find the locations of all elements that satisfy certain conditions.
3. Inserting: Inserting means addition of a new data element in a data structure.
4. Deleting: Deleting means removal of a data element from a data structure.
5. Sorting: Sorting is the process of arranging all data items in a data structure in a particular
order say for example, either in ascending order or in descending order.
6. Merging: Combining the records of two different sorted files into a single sorted file.
7. Updation: It update or modifies the data in data Structure.
8. Splitting: It is process of partitioning Single list to multiple list.
9. Destroy: It destroy the memory space allocated for specified data structure.
10. Selection: It deals with accessing a particular data within a data structure.
Array :
An array is defined as a set of finite number of homogeneous elements or data items. It means an
array can contain one type of data only, either all integers, all floating- point numbers, or all
3
characters.
Declaration of arrays is as follows:
int A[10];
where int specifies the data type or type of elements array stores. “A” is the name of the array, and
the number specified inside the square brackets is the number of elements an array can store, this
is also called size or length of array.
2. Linked Lists
A linked list is a linear collection of data elements, called node pointing to the next nodes by
means of pointers. Each node is divided into two parts : the first part containing the information
of the element, and the second part called the link or next pointer containing the address of the
next node in the list. Technically, each such element is referred to as a node, therefore a list can be
defined as a collection of nodes as shown in Fig. (2) below :
Applications: Representation of sparse matrices, Non-contiguous data storage, Implementation of
non-binary tree or other data structures, Dynamic memory management, Equalizing parenthesis,
Symbol tables
1.Singly Linked List: In a singly linked list, each node contains a data element and a pointer to
the next node in the list. This means that each node can only be accessed in one direction, starting
from the head of the list.
2. Doubly Linked List: In a doubly linked list, each node contains a data element and pointers to
both the next and previous nodes in the list. This allows for traversal in both directions, making it
more efficient for certain operations such as inserting or deleting nodes.
3. Circular Linked List: In a circular linked list, the last node in the list points back to the first
node, creating a circular structure. This means that the list can be traversed indefinitely, and the
last node can be accessed by following the pointer from the first node
3. Stacks
A stack is a non-primitive linear data structure. It is an ordered list in which addition of new data
item and deletion of already existing data item is done from only one end, known as Top of Stack
(TOS) . As all the deletion and insertion in a stack is done from top of the stack, the last added
element will be the first to be removed from the stack. Due to this reason, the stack is also called
Last-In-First-Out (LIFO) type of list.
Consider some examples,
1. A common model of a stack is plates in a marriage party. Fresh plates are “pushed ” onto
the top and “popped ” off the top.
2. Some of you may eat biscuits. If you assume only one side of the cover is torn and biscuits
are taken off one by one. This is called popping and similarly, if you want to preserve some
biscuits for some time later, you will put them back into the pack through the same torn end
called pushing.
Applications: Recursion, Evaluation of expressions, Backtracking, Runtime memory
management, Arrangement of books in a library
4. Queues
4
Queue is a linear data structure that permits insertion of an element at one end and deletion of an
element at the other end. The end at which the deletion of an element take place is called front,
and the end at which insertion of a new element can take place is called rear. The deletion or
insertion of elements can take place only at the front and rear end of the list respectively.
The first element that gets added into the queue is the first one to get removed from the list.
Hence, Queue is also referred to as First-In-First-Out (FIFO) list. The name ‘ Queue’ comes
from the everyday use of the term. Consider a railway reservation booth, at which we have to get
into the reservation queue. New customers got into the queue from the rear end, whereas the
customers who get their seats reserved leave the queue from the front end. It means the customers
are serviced in the order in which they arrive the service center (i.e. first come first serve type of
service). The same characteristics apply to our Queue. Fig. (3) shows the pictorial representation
of a Queue.
.
Applications: Disk scheduling, CPU scheduling, File IO, Data transmission
A priority queue:
It is a type of queue that arranges elements based on their priority values. Elements with higher priority
values are typically retrieved before elements with lower priority values.
In a priority queue, each element has a priority value associated with it. When you add an element to
the queue, it is inserted in a position based on its priority value. For example, if you add an element
with a high priority value to a priority queue, it may be inserted near the front of the queue, while an
element with a low priority value may be inserted near the back.
5. Trees
A Tree can be defined as a finite set of data items (nodes). Tree is a non-linear type of data
structure in which data items are arranged of stored in a sorted sequence. Trees represent the
hierarchical relationship between various elements. In trees :
1. There is a special data item at the top of hierarchy called the Root of the Tree.
2. The remaining data items are partitioned into number of mutually exclusive (i.e. disjoint)
subsets, each of which is itself, a tree, which is called the subtree.
3. The tree always grows in length towards bottom in data structures, unlike natural trees
which grows upwards.
4. The tree structure organizes the data into branches, which relate the information. A tree is
shown in Fig. (4).
5.
6.
5
7. Applications: Representation of data lists, Quickly accessible data storage, Representation of
hierarchal data, Routing of algorithms
8.
9.
10. 6. Graphs
11.
12. Data sometimes contain a relationship between pairs of elements which is not necessarily
hierarchical in nature. Geometrical arrangement are very important while working with
real life projects. For example, let us suppose there are five villages separated by a river.
Now we want to construct bridges to connect these villages as shown in Fig. (5)
13.
We have represented villages by dots (which are called vertex or node) and bridges by lines which
are called edges. This type of drawing is called graph. Hence a graph can be defined as a ordered
set (V,E), where V(G) represents the set of all elements called vertices and E(G) represents the
edges between these vertices.
Fig. (6) shows a graph, for which V(G)={ v1, v2, v3, v4, v5 } and E(G) = { b1, b2, b3, b4 }.
Applications: Computer networking, Problem solutions involving 'Depth-First' search or
'Breadth-First' search algorithms, Representation of matrices, Study of molecular interactions in
Chemistry
Algorithms:
Algorithm is a step-by-step procedure to solve a particular function i.e., it is a set of instructions
written to carry out certain tasks and the data structure is the way of organizing the data with their
logical relationship maintained.
An algorithm is a finite sequence of instructions, each of which has a clear meaning and
can be performed with a finite amount of effort in a finite length of time. No matter what the input
values may be, an algorithm terminates after executing a finite number of instructions.
An algorithm is defined as a step-by-step procedure 6 or method for solving a problem by a
computer in a finite number of steps. Steps of an algorithm definition may include branching or
repetition depending upon what problem the algorithm is being developed for. While defining an
algorithm steps are written in human understandable language and independent of any
programming language. We can implement it in any programming language of our choice
Need of Algorithm:
● To understand the problem
● Construction of the list of the variables
● To design the output
● Problem Development
● Testing the program
● Validating the program
Characteristic of Algorithm
● Definiteness (Unambiguous) − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear, and Every statement should have single
and exact meaning. You cannot write statement which cannot understandable.
● Input − An algorithm should have 0 or more well-defined inputs.
● Output − An algorithm should have 1 or more well-defined outputs and should match the
desired output.
● Finiteness − if we trace out the instructions of an algorithm, then for all cases the
algorithm will terminate after a finite number of steps.
● Feasibility − should be feasible with the available resources.
● Effectiveness: Every step must be accurate. Don’t write unnecessary step which are not
required.
ARRAY:
The size and type of arrays cannot be changed after its declaration.
2. Multi-Dimensional Array
Here int specifies the data type or type of elements array stores. “A” is the name of
the array, and the number specified inside the square brackets is the number of elements an
array can store, this is also called size or length of array. In single Dimensional Array
elements are stored in sequential order.
Array must be start from ‘0’ location and end with size 1.
Two-Dimensional Array
Two – dimensional array is the simplest form of a multidimensional array. We can see a
two – dimensional array as an array of one – dimensional array for easier understanding.
Two-dimensional array used for Matrix.
An element in a two-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array.
For example −
int A[2][2];
SPARSE MATRIX:
Sparse matrix is a matrix which contains very few non-zero elements. Maximum
number of Zero element is present than the Non-Zero elements i.e. Majority of element equal to
Zero element called Sparse Matrix.
In computer programming, a matrix can be defined with a 2-dimensional array. Any array
with ‘m’ columns and ‘n’ rows represents a mXn matrix.
8
When a sparse matrix is represented with 2-dimensional array, we waste lot of space to
represent that matrix. For example, consider a matrix of size 100 X 100 containing only 10
non-zero elements.
In this matrix, only 10 spaces are filled with non-zero values and remaining spaces of matrix
are filled with zero. That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to
store this integer matrix. And to access these 10 non-zero elements we have to make scanning
for 10000 times.
Hash Function: A function that converts a given big phone number to a small practical integer
value. The mapped integer value is used as an index in hash table. In simple terms, a hash
function maps a big number or string to a small integer that can be used as index in hash table.
A good hash function should have following properties
1) Efficiently computable.
2) Should uniformly distribute the keys (Each table position equally likely for each key)
For example for phone numbers a bad hash function is to take first three digits. A better
function is consider last three digits. Please note that this may not be the best hash function.
There may be better ways.
Hash Table: An array that stores pointers to records corresponding to a given phone number.
An entry in hash table is NIL if no existing phone number has hash function value equal to the
index for the entry.
4) Double hashing has the ability to have a low collision rate, as it uses two hash functions to compute the hash value and the
step size. This means that the probability of a collision occurring is lower than in other collision resolution techniques such
as linear probing or quadratic probing. 9
5) However, double hashing has a few drawbacks. First, it requires the use of two hash functions, which can increase the
computational complexity of the insertion and search operations. Second, it requires a good choice of hash functions to
achieve good performance. If the hash functions are not well-designed, the collision rate may still be high.
Collision Handling: Since a hash function gets us a small number for a big key, there is
possibility that two keys result in same value. The situation where a newly inserted key maps
to an already occupied slot in hash table is called collision and must be handled using some
collision handling technique. Following are the ways to handle collisions:
● Chaining:The idea is to make each cell of hash table point to a linked list of records
that have same hash function value. Chaining is simple, but requires additional
memory outside the table.
● Open Addressing: In open addressing, all elements are stored in the hash table itself.
Each table entry contains either a record or NIL. When searching for an element, we one
by one examine table slots until the desired element is found or it is clear that the element
is not in the table.
Separate Chaining:
The idea behind separate chaining is to implement the array as a linked list called a chain. Separate chaining is one of the
most popular and commonly used techniques in order to handle collisions.
The linked list data structure is used to implement this technique. So what happens is, when multiple elements are hashed
into the same slot index, then these elements are inserted into a singly-linked list which is known as a chain.
Here, all those elements that hash into the same slot index are inserted into a linked list. Now, we can use a key K to
search in the linked list by just linearly traversing. If the intrinsic key for any entry is equal to K then it means that we
have found our entry. If we have reached the end of the linked list and yet we haven’t found our entry then it means that
the entry does not exist. Hence, the conclusion is that in separate chaining, if two different elements have the same hash
value then we store both the elements in the same linked list one after the other.
Example: Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85, 92, 73, 101
Advantages:
Simple to implement.
Hash table never fills up, we can always add more elements to the chain.
Less sensitive to the hash function or load factors.
It is mostly used when it is unknown how many and how frequently keys may be inserted or deleted.
Disadvantages:
The cache performance of chaining is not good as keys are stored using a linked list. Open addressing
provides better cache performance as everything is stored in the same table.
Wastage of Space (Some Parts of the hash table are never used)
If the chain becomes long, then search time can become O(n) in the worst case
Uses extra space for links
●
Open Addressing
Probing is a scheme in computer programming for resolving hash collisions of values of
hash functions by sequentially searching the hash table for a free location.
Open Addressing:
Like separate chaining, open addressing is a method for handling collisions. In Open
Addressing, all elements are stored in the hash table itself. So at any point, size of the table
must be greater than or equal to the total number of keys (Note that we can increase table size
by copying old data if needed).
Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k.
Search(k): Keep probing until slot’s key doesn’t become equal to k or an empty slot is reached.
Delete(k): Delete operation is interesting. If we simply delete a key, then search may fail. So
slots of deleted keys are marked specially as “deleted”.
Insert can insert an item in a deleted slot, but the search doesn’t stop at a deleted slot. Open
Addressing is done following ways:
a) Linear Probing: In linear probing, we linearly probe for next slot. For example, typical
gap between two probes is 1 as taken in below example10also.
let hash(x) be the slot index computed using hash function and S be the table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S If
(hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
..................................................
..................................................
Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76,
85, 92, 73, 101.
For example: Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92,
73, 101
Sequential Search :
In this type of search, a sequential search is made over all items one by one. Every item is
checked and if a match is found then that particular item is returned, otherwise the search
continues till the end of the data collection.
BINARY SEARCH :
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm
works on the principle of divide and conquer. For this algorithm to work properly, the data collection
should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the collection. If a
match occurs, then the index of item is returned. If the middle item is greater than the item, then the
item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in
the sub-array to the right of the middle item. This process continues on the sub-array as well until the
size of the sub-array reduces to zero.
When memory is allocated during compilation time, it is called ‘Static Memory Allocation’. This
memory is fixed and cannot be increased or decreased after allocation. If more memory is allocated
than requirement, then memory is wasted. If less memory is allocated than requirement, then program
will not run successfully. So exact memory requirements must be known in advance.
2. Dyanamic memory allocation :
When memory is allocated during run/execution time, it is called ‘Dynamic Memory Allocation’. This
memory is not fixed and is allocated according to our requirements. Thus in it there is no wastage of
memory. So there is no need to know exact memory requirements in advance.
11
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large
block of memory with the specified size. It returns a pointer of type void which can be cast into a
pointer of any form. It doesn’t Initialize memory at execution time so that it has initialized each block
with the default garbage value initially.
Q:A threaded binary tree: is a type of binary tree data structure where the empty left and right child pointers in a binary
tree are replaced with threads that link nodes directly to their in-order predecessor or successor, thereby providing a way
to traverse the tree without using recursion or a stack.
Threaded binary trees can be useful when space is a concern, as they can eliminate the need for a stack during traversal.
However, they can be more complex to implement than standard binary trees.
There are two types of threaded binary trees.
Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if successor exists)
Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and inorder
successor respectively. The predecessor threads are useful for reverse inorder traversal and postorder traversal.
● Binary search tree is a data structure that quickly allows us to maintain a sorted list of
numbers.
● It is called a binary tree because each tree node has a maximum of two children.
● It is called a search tree because it can be used to search for the presence of a
number in O(log(n)) time.
● The properties that separate a binary search tree from a regular binary tree is
○ All nodes of left subtree are less than the root node
○ All nodes of right subtree are more than the root node
○ Both subtrees of each node are also BSTs i.e. they have the above two properties
Q: What is garbage collection?
A: Garbage collection is an automatic memory management process that helps reclaim memory that is no
longer being used by a program. It is the process of finding and removing objects that are no longer needed by
the program and freeing up the memory they were using.
Q: When will the garbage collection program run?
A: The garbage collection program runs automatically in the background, periodically checking the program's
memory usage and freeing up unused memory. The exact timing of the garbage collection process depends on the
programming language and the runtime environment used.
13
Q: Who will run the garbage collection program?
A: The garbage collection program is typically run by the programming language's runtime environment,
such as the Java Virtual Machine (JVM) in the case of Java programs. The program itself does not need to
explicitly call the garbage collector, as it runs automatically in the background. However, some
programming languages allow developers to manually trigger the garbage collection process if needed
5. If the top operator in stack has equal or higher precedence than scanned operator then POP the operator present in stack and add
it to postfix string else PUSH the scanned character to stack.
7. If the scanned character is a right parenthesis ‘)’, POP and add to postfix string from stack until an ‘(‘ is encountered. Ignore
both '(' and ')'.
9. After all character are scanned POP the characters and add to postfix string from the stack until it is not empty.
14