0% found this document useful (0 votes)
11 views

Data Structures and Algorithms

Uploaded by

Shubham Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Data Structures and Algorithms

Uploaded by

Shubham Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structures and Algorithms

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated efficiently.

A data structure is not only used for organizing the data. It is also used for processing, retrieving, and
storing data.

• Linear data structure: Data structure in which data elements are arranged sequentially or
linearly, where each element is attached to its previous and next adjacent elements, is called a linear
data structure.

Examples of linear data structures are array, stack, queue, linked list, etc.

• Static data structure: Static data structure has a fixed memory size. It is easier to access
the elements in a static data structure.

An example of this data structure is an array.

• Dynamic data structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning the
memory (space) complexity of the code.

Examples of this data structure are queue, stack, etc.

• Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the
elements in a single run only.
Examples of non-linear data structures are trees and graphs.

What is Array?
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together. This makes it easier to calculate the position of each element by
simply adding an offset to a base value, i.e., the memory location of the first element of the array
(generally denoted by the name of the array).

What is String?
Strings are defined as an array of characters. The difference between a character array and a string is
the string is terminated with a special character ‘\0’.

The Cy
How is String represented in Memory?
In C, a string can be referred to either using a character pointer or as a character array. When strings
are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is
an auto variable then the string is stored in the stack segment, if it’s a global or static variable then
stored in the data segment, etc.
What is Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image:

In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.

What is Stack?
Stack is a linear data structure that follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that
is inserted last, comes out first and FILO implies that the element that is inserted first, comes out
last.

There are many real-life examples of a stack. Consider an example of plates stacked over one another
in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has
been placed at the bottommost position remains in the stack for the longest period of time. So, it can
be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
What is Queue Data Structure?
A Queue is defined as a linear data structure that is open at both ends and the operations are
performed in First in First Out (FIFO) order.

We define a queue to be a list in which all additions to the list are made at one end, and all deletions
from the list are made at the other end. The element that is first pushed into the order, the
operation is first performed on that.

FIFO Principle of Queue:


A Queue is like a line waiting to purchase tickets, where the first person in line is the first person
served. (i.e. First come first serve).

The position of the entry in a queue ready to be served, that is, the first entry that will be removed
from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the
position of the last entry in the queue, that is, the one most recently added, is called the rear (or the
tail) of the queue. See the below figure.

Characteristics of Queue:

 Queue can handle multiple data.


 We can access both ends.
 They are fast and flexible.
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the Queue is
implemented using the array. Variables used in this case are

 Queue: the name of the array storing queue elements.


 Front: the index where the first element is stored in the array representing the queue.
 Rear: the index where the last element is stored in an array representing the queue.
Introduction to Tree
A tree data structure is a hierarchical structure that is used to represent and organize data in a way
that is easy to navigate and search. It is a collection of nodes that are connected by edges and has a
hierarchical relationship between the nodes.

The topmost node of the tree is called the root, and the nodes below it are called the child nodes.
Each node can have multiple child nodes, and these child nodes can also have their own child nodes,
forming a recursive structure.

This data structure is a specialized method to organize and store data in the computer to be used
more effectively. It consists of a central node, structural nodes, and sub-nodes, which are connected
via edges. We can also say that tree data structure has roots, branches, and leaves connected with
one another.

Basic Terminologies In Tree Data Structure:


 Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
 Child Node: The node which is the immediate successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes of {B}.
 Root Node: The topmost node of a tree or the node which does not have any parent node is
called the root node. {A} is the root node of the tree. A non-empty tree must contain exactly
one root node and exactly one path from the root to all other nodes of the tree.
 Leaf Node or External Node: The nodes which do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P} are the leaf nodes of the tree.
 Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
 Descendant: Any successor node on the path from the leaf node to that node. {E,I} are the
descendants of the node {B}.
 Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
 Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
 Internal node: A node with at least one child is called Internal Node.
 Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
 Subtree: Any node of the tree along with its descendant.

Representation of Tree Data Structure:


A tree consists of a root, and zero or more subtrees T1, T2, … , Tk such that there is an edge from the
root of the tree to the root of each subtree.

Representation of Tree Data Structure

Representation of a Node in Tree Data Structure:


struct Node
{
int data;
struct Node *first_child;
struct Node *second_child;
struct Node *third_child;
.
.
.
struct Node *nth_child;
};

Example of Tree data structure


Here,

 Node 1 is the root node


 1 is the parent of 2 and 3
 2 and 3 are the siblings
 4, 5, 6, and 7 are the leaf nodes
 1 and 2 are the ancestors of 5

Types of Tree data structures:

 Binary tree: In a binary tree, each node can have a maximum of two children linked to it.
Some common types of binary trees include full binary trees, complete binary trees,
balanced binary trees, and degenerate or pathological binary trees.
 Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most three
child nodes, usually distinguished as “left”, “mid” and “right”.
 N-ary Tree or Generic Tree: Generic trees are a collection of nodes where each node is a data
structure that consists of records and a list of references to its children(duplicate references
are not allowed). Unlike the linked list, each node stores the address of multiple nodes.

Basic Operation Of Tree Data Structure:


 Create – create a tree in the data structure.
 Insert − Inserts data in a tree.
 Search − Searches specific data in a tree to check whether it is present or not.
 Traversal:
 Preorder Traversal – perform Traveling a tree in a pre-order manner in
the data structure.
 In order Traversal – perform Traveling a tree in an in-order manner.
 Post-order Traversal –perform Traveling a tree in a post-order manner.

Why Tree is considered a non-linear data structure?


The data in a tree are not stored in a sequential manner i.e., they are not stored linearly. Instead,
they are arranged on multiple levels or we can say it is a hierarchical structure. For this reason, the
tree is a non-linear data structure.

Properties of Tree Data Structure:


 Number of edges: An edge can be defined as the connection between two nodes. If a tree has N
nodes then it will have (N-1) edges. There is only one path from each node to any other node of
the tree.
 Depth of a node: The depth of a node is defined as the length of the path from the root to that
node. Each edge adds 1 unit of length to the path. So, it can also be defined as the number of
edges in the path from the root of the tree to the node.
 Height of a node: The height of a node can be defined as the length of the longest path from the
node to a leaf node of the tree.
 Height of the Tree: The height of a tree is the length of the longest path from the root of the tree
to a leaf node of the tree.
 Degree of a Node: The total count of subtrees attached to that node is called the degree of the
node. The degree of a leaf node must be 0. The degree of a tree is the maximum degree of a
node among all the nodes in the tree.

Need for Tree Data Structure


1. One reason to use trees might be because you want to store information that naturally forms a
hierarchy. For example, the file system on a computer:
File System

2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and
slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked
Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on the number of nodes as
nodes are linked using pointers.

Application of Tree Data Structure:


 File System: This allows for efficient navigation and organization of files.
 Data Compression: Huffman coding is a popular technique for data compression that involves
constructing a binary tree where the leaves represent characters and their frequency of
occurrence. The resulting tree is used to encode the data in a way that minimizes the amount of
storage required.
 Compiler Design: In compiler design, a syntax tree is used to represent the structure of a
program.
 Database Indexing: B-trees and other tree structures are used in database indexing to efficiently
search for and retrieve data.
Advantages of Tree Data Structure:
 Tree offer Efficient Searching Depending on the type of tree, with average search times of O(log
n) for balanced trees like AVL.
 Trees provide a hierarchical representation of data, making it easy to organize and navigate large
amounts of information.
 The recursive nature of trees makes them easy to traverse and manipulate using recursive
algorithms.

Disadvantages of Tree Data Structure:


 Unbalanced Trees, meaning that the height of the tree is skewed towards one side, which can
lead to inefficient search times.
 Trees demand more memory space requirements than some other data structures like arrays
and linked lists, especially if the tree is very large.
 The implementation and manipulation of trees can be complex and require a good
understanding of the algorithms.

You might also like