DS 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Data Structure and Algorithms

By - Astha Tripathi
What is data structure
• Data structures are an integral part of
computers used for the arrangement of data in
memory. They are essential and responsible for
organizing, processing, accessing, and storing
data efficiently.

 Type of DS
Cont..
• 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.
 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
Cont..
• 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.
Stack
• A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.

• It contains only one pointer top pointer pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from the stack. In other words, a stack can be defined as a
container in which insertion and deletion can be done from the one end known as the
top of the stack.
Working
Cont..
• push(): When we insert an element in a stack then the operation is known as a push. If the stack is full
then the overflow condition occurs.
• pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is
empty means that no element exists in the stack, this state is known as an underflow state.
• isEmpty(): It determines whether the stack is empty or not.
• isFull(): It determines whether the stack is full or not.
• Before inserting an element in a stack, we check whether the stack is full.
• If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
• When the new element is pushed in a stack, first, the value of the top gets incremented, i.e.,
top=top+1, and the element will be placed at the new position of the top.
• Before deleting the element from the stack, we check whether the stack is empty.
• If we try to delete the element from the empty stack, then the underflow condition occurs.
• Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.
Addition operation
It is the process of inserting(adding) a new data item I into the stack. If the stack is full and we try to add
a new item into the stack makes the stack over flow. Algorithm is given below
• Step 1 : If top = N.
• Then print “OVERFLOW” and return
• Step 2 : Set top =top+1
• Step 3 : Set Stack[top]= item
• Step 4 : stop
Deletion operation
• Step 1 : If top = Null. Then print “UNDERFLOW” and return
• Step 2 : Set item=Stack[top]
• Step 3 : Set top = top -1
• Step 4: stop
What is Queue?
• 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 which is first pushed into the order, the operation
is first performed on that.
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to
implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −

• Step 1 − Check if the queue is full.

• Step 2 − If the queue is full, produce overflow error and exit.

• Step 3 − If the queue is not full, increment rear pointer to point the next empty space.

• Step 4 − Add data element to the queue location, where the rear is pointing.

• Step 5 − return success.


Algorithm for enqueue operation
• procedure enqueue(data)
• if queue is full
• return overflow
• endif
• rear ← rear + 1
• queue[rear] ← data
• return true
• end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing and
remove the data after access. The following steps are taken to perform dequeue operation −

• Step 1 − Check if the queue is empty.

• Step 2 − If the queue is empty, produce underflow error and exit.

• Step 3 − If the queue is not empty, access the data where front is pointing.

• Step 4 − Increment front pointer to point to the next available data element.

• Step 5 − Return success


Algorithm for dequeue operation
procedure dequeue
• if queue is empty
• return underflow
• end if

• data = queue[front]
• front ← front + 1
• return true

• end procedur
What is a Tree data structure?
• A tree is non-linear and a hierarchical data structure consisting of a collection of
nodes such that each node of the tree stores a value and a list of references to other
nodes (the “children”).

 Root node
 Parent node
 Child node
 Leaf node
 Sibling
 Subtree
Properties of a Tree
• 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.
What are binary trees?
If you have worked on normal trees before or
even know about their basics, you would know
that there are no restrictions when it comes to
the number of children that different nodes are
allowed to have in these trees. Binary trees are a
little different in this sense.
Every parent or node in binary trees can
have a maximum of only two children. It can
be 0 child or 1 child or 2 child maximum.
Binary Search Tree
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
Question
Insert these nodes in a BST

8, 3, 10, 6, 14, 4, 7, 13
Solution
Traversal of binary search tree
• (a) Inorder (Left, Root, Right)
• (b) Preorder (Root, Left, Right)
• (c) Postorder (Left, Right, Root)
Example

You might also like