Data Structure
Data Structure
string T.
longer text.
10
Algorithm:
5. Set K=K+1
6. Set INDEX=0
7.Exit
• Linear: A linear describes data characteristics
whether the data items are arranged in
sequential form like an array.
Data structure 2D ARRAYThe two-dimensional array • Non-Linear: A Non-Linear data structure
Data structure is a representationof can be defined as an array of arrays. The describes the characteristics of data items that
logical relationship existing between 2D array is organized as matrices which are not in sequential form like a tree, graph.
individual elements of data. In other can be represented as the collection of • Static: It is a static data structure that
words, a data structure defines a way of describes the size and structures of a collection
rows and columns. However, 2D arrays of data items associated with a memory
organizing all data items that are created to implement a relational location at compile time that are fixed.
considersnot only the elements stored Example – Array.
database lookalike data structure. It
but also their relationship to each other. • Homogenous: It is a characteristic of data
The term data structure is used to provides ease of holding the bulk of data structures representing whether the data type
describe the way data is stored. at once which can be passed to any of all elements is the same. Example- Array.
number of functions wherever • Non-Homogenous: It is a characteristic of
Primitive Data Structures:- are the basic data structures representing whether the data
data structures that directly operate required.Syntax: data_type type elements may or may not be the same
upon themachine instructions. They array_name[rows][columns]; • Dynamic: It is a dynamic data structure that
have different representations on defines the shrinking and expanding of data
different computers. Integers, floating ARRAY VS LINKED LIST items at the run time or the program’s
Array:-•Size of an array is fixed execution. It is also related to the utilization of
point numbers, character constants, memory location that can be changed at the
string constants and pointers come •Memory is allocated from stack•It is program’s run time. Example: Linked list
under this category. necessary to specify the number of • It has some rules that define how the data
Non-primitive data structures:-are more elements during declaration (i.e., items are related to each other
complicated data structures and are duringcompile time).• It occupies less • It defines some rules to display the
memory than a linked list for the same relationship between data items and how they
derived fromprimitive data structures. interact with each other
They emphasize on grouping same or number of elements. •Inserting new • It has some operations used to perform on
different data items withrelationship elements at the front ispotentially data items like insertion, searching, deletion,
between each data item. Arrays, lists expensive because existing elements etc.
and files come under this category need to be shifted over to make • It helps in reducing the usage of memory
room•Deleting an element from an resources
•Time Complexity: The execution time of a
array is not possible. program in a data structure should be
Recursion: Linked List:-•Size of a list is not minimum as possible
A function is recursive if a statement in fixed•Memory is allocated from heap•It •Space Complexity: The memory usage
the body of the function calls itself. is not necessary to specify the Number through all data items in a data structure
of elements during declaration(i.e., should be less possible.
Recursion is The process of defining
something in terms of itself. For a memory is allocated during run Time).•It
computer language to be Recursive, a occupies more memory.•Deleting an LINEAR SEARCH?
function must be able to call itself. element is possible. A linear search, also known as a
LINKED LIST SINGLY LINKED LIST:-Linked List can be sequential search, is a method of finding
Linked lists and arrays are similar since defined as collection of objects an element within a list. It checks each
they both store collections of data. Array called nodes that are randomly stored in element of the list sequentially until a
is the Most common data structure used the memory.A node contains two fields match is found or the whole list has
to store collections of elements. Arrays i.e. data stored at that particular address been searched.
are Convenient to declare and provide and the pointer which contains the Algorithm:-
the easy syntax to access any element by address of the next node in the •begin with the leftmost element of
its index Number. Once the array is set memory.The last node of the list arr[] and one by one compare x with
up, access to any element is convenient contains pointer to the each element.
and fast TWO WAY LINKED LIST/DOUBLY LINKED •x matches with an element then return
Advantages of linked lists: LIST the index.
•Linked lists are dynamic data A Doubly Linked List (DLL) contains an •if x does not match with any of the
structures. i.e., they can grow or shrink extra pointer, typically called previous
elements then return -1.
during The execution of a program. pointer, together with next pointer and PROGRAM:-
•Linked lists have efficient memory data which are there in singly linked •#include<stdio.h>
utilization. Here, memory is not list.A doubly linked list or a two-way •Int main(){
pre-Allocated. Memory is allocated linked list is a more complex type of •Int a[20],I,x,n;
whenever it is required and it is linked list which contains a pointer to •Printf(“How many elements?”);
de-allocated when it is no longer the next as well as the previous node in •Scanf(“%d”,&n);
needed. •Insertion and Deletions are sequence, Therefore, it contains three •Printf(“Enter array elements:n”);
easier and efficient. parts are data, a pointer to the next •For(i=0;i<n;++i)• Scanf(“%d”,&a[i]);
•Linked lists provide flexibility•In node, and a pointer to the previous •Printf(“nEnter element to search:”);
inserting a data item at a specified node. This would enable us to traverse •Scanf(“%d”,&x);
position and deletion of the data item the list in the backward direction as well •For(i=0;i<n;++i)
From the given position. CIRCULAR LINKED LIST •If(a[i]==x)
•Many complex applications can be Circular linked list is a linked list where •Break;
easily carried out with linked lists. all nodes are connected to form a circle. •If(i<n)
There is no NULL at the end. A circular •Printf(“Element found at index %d”,i);
ARRAY:- linked list can be a singly circular linked •Else
An array is a collection of similar data list or doubly circular linked •Printf(“Element not found”);
elements stored at contiguous memory list.AdvantagesofCircularLinkedLists: •Return 0;}
locations. It is the simplest data 1) Any node can be a starting point. We
structure where each data element can can traverse the whole list by starting
be accessed directly by only using its from any point. 2)useful to
index number. … Rather, we can define implementation of queue
an array which will store the data
elements at contiguous memory
locations. Application of data structures
Data structures are integral part of
programming languages and when you
CHARACTERISTICS OF DS write a program and execute it, you will
be using a data structure in some or the Adding New Node BST:- •If an operand is encountered, add it to
other context. Whether you have used •Create a new BST node and assign Y.
data structures implicitly or explicitly values to it. •If a left parenthesis is encountered,
doesn’t matter. If you have not used any •Insert(node, key)If root == NULL, push it onto STACK.
data structure explicitly in your program, •Return the new node to the calling •if an operator is encountered, then(a)
operating system will use one while function.If root=>data < key Repeatedly pop from STACK and add to Y
executing your program •Call the insert function with each operator (on the top of STACK)
•Stacks are used in applications root=>right and assign the return value which has the same precedence as or
like:UNDO/REDO options in your text in root=>right. higher precedence than operator.(b)
editorStoring function calls in recursion, • Root->right = insert(root=>right,key) Add operator to STACK./*End of If
result that each function returns in • If root=>data > key structure */
recursion is stored in stack and when •Call the insert function with root->left •if a right parenthesis is encountered,
function returns the result if popped and assign the return value in root=>left. then :
from stack ,Storing browser history, •Root=>left = insert(root=>left,key) (a) Repeatedly pop from STACK and add
when you click back and forward •Finally, return the original root pointer to Y each operator (on the top of STACK)
buttons you visit the previous or next to the calling function. until a left parenthesis is encountered.
page that you visited respectively (b) Remove the left parenthesis. [Do not
• Queues:-are used in Scheduling add the left parenthesis to Y]./* end of If
processes like LRU (Least Recently APPLICATIONS OF TREES structure *//* end of Step 2 loop */
Used)In asynchronous programming • Storing naturally hierarchical data: •exit.
For storing your keystrokes as you type Trees are used to store the data in the
on your keyboard hierarchical structure. For example, the MEMORY ALLOCATION IN TWO
• Graphs are used in Network/ request file system. The file system stored on the DIMENSIONAL ARRAY
routing In algorithms like Djikstra’s disc drive, the file and folder are in the 1)using a single pointer:- A simple way
algorithm (whose modified version is form of the naturally hierarchical data is to allocate memory block of size r*c
used in Google ,Social networking sites and stored in the form of trees. and access elements using simple
• Trees:-A tree is also one of the data •Organize data: It is used to organize pointer arithmetic.
structures that represent hierarchical data for efficient insertion, deletion and 2)using an array of pointers:- We can
data. Suppose we want to show the searching. For example, a binary tree has create an array of pointers of size r. Note
employees and their positions in the a logN time for searching an element. that from C99, C language allows
hierarchical form. •Trie: It is a special kind of tree that is variable sized arrays. After creating an
•General tree is a tree in which each used to store the dictionary. It is a fast array of pointers, we can dynamically
node can have many children or nodes and efficient way for dynamic spell allocate memory for every row.
•The subtree of a general tree do not checking. 3)using pointer to a pointer:- We can
hold the ordered property •Heap: It is also a tree data structure create an array of pointers also
•In data structure, a general tree can not implemented using arrays. It is used to dynamically using a double pointer.
be empty implement priority queues. Once we have an array pointers
•B-Tree and B+Tree: B-Tree and B+Tree allocated dynamically, we can
Expression Trees are the tree data structures used to dynamically allocate memory and for
An expression tree is a representation of implement indexing in databases.
every row like method 2. 4) Using
expressions arranged in a tree-like data •Routing table: The tree data structure
is also used to store the data in routing double pointer and one malloc call;
structure. In other words, it is a tree
with leaves as operands of the tables in the routers.
GRAPH
expression and nodes contain the A graph can be defined as group of
operators. Similar to other data vertices and edges that are used to
Θ Notation: The theta notation bounds
structures, data interaction is also connect these vertices. A graph can be
a function from above and below, so it
possible in an expression tree. defines exact asymptotic behavior. A seen as a cyclic tree, where the vertices
Expression trees are mainly used for simple way to get Theta notation of an (Nodes) maintain any complex
analyzing, evaluating and modifying expression is to drop low order terms relationship among them instead of
expressions, especially complex and ignore leading constants having parent child relationship
expressions. • Big O Notation: The Big O notation WEiGHTED GRAPH
defines an upper bound of an algorithm, A WG is a graph with the property that
BINARY SEARCH TREE(BST) it bounds a function only from above. its edges have a number associated with
Binary Search Tree is a node-based For example, consider the case of it.the number is called weight of the
binary tree data structure which has the Insertion Sort. It takes linear time in best edge.The WG can be undirected or
following properties: case and quadratic time in worst case. directed
• The left subtree of a node contains We can safely say that the time DIRECTED GRAPH
only nodes with keys lesser than the complexity of Insertion sort is O(n^2). A DG is a graph with the property that it
node’s key Note that O(n^2) also covers linear time. edges have directions.They are also
• The right subtree of a node contains • Ω Notation: Just as Big O notation called digraph
only nodes with keys greater than the provides an asymptotic upper bound on
node’s key. a function, Ω notation provides an
• The left and right subtree each must asymptotic lower bound. Ω Notation can
also be a binary search tree. be useful when we have lower bound on
time complexity of an algorithm
Postfix notation is also known as Reverse (b) increment rear pointer to point the
Polish Notation or Suffix. next empty space.
Circular Queue is a linear data structure 6. K-D Tree: A space partitioning tree /* A binary tree node has data, pointer to
left child
in which the operations are performed used to organize points in K dimensional
based on FIFO (First In First Out) space. and a pointer to right child */
principle and the last position is
connected back to the first position to 7. Trie : Used to implement dictionaries struct node
make a circle.It is also called 'Ring with prefix lookup.
Buffer' {
8. Suffix Tree : For quick pattern
searching in a fixed text. int data;
maximum height of the tree (H), and make An undirected graph is a set of
Define forest. . the array big enough to hold any binary nodes and a set of links between
tree of this height the nodes. Each node is called
Forest is a collection of disjoint trees.
In other words, we can also say that (or less). We'll need an array of size a vertex, each link is called an edge,
forest is a collection of an acyclic (2**H)-1. Here is the biggest binary tree of
and each edge connects two
graph which is not connected. Here depth 3: root of the
vertices. The order of the two
is a pictorial representation of a connected vertices is unimportant.
tree (A): array position 1
An undirected graph is a finite set of
forest. ∙ root's left child (B): array position 2 vertices together with a finite
∙ root's right child (C): array position 3 set of edges.
∙ ...
2. Binary Search Tree :is a tree that ∙ left child of node in array position K: array
allows fast search, insert, delete on a position 2K
sorted data. It also
∙ right child of node in array position K:
allows finding closest item array position 2K+1