Dsa Reviewer 1
Dsa Reviewer 1
Data Structure
- Programmatic way of storing data so that it can be used efficiently and will allow the most
efficient algorithm to be used.
- The functional definition of a data structure is known as Abstract Data Type (ADT), which is
independent of implementation.
- The way of which the data is organized affects the performance of a program for different
tasks.
- Examples: lists, arrays, stacks, queues, heaps, trees, and graphs.
- a logical description of how we view the data and the operations that are allowed without regard
to how they will be implemented.
• Information hiding – encapsulating the details of the implementation/hiding them from the
user’s view.
Algorithms
• Structure of Algorithm
1. Input Step
2. Assignment Step
3. Decision Step
4. Repetitive Step
5. Output Step
• Properties of Algorithm
o Finiteness – must terminate after a finite number of steps.
o Definiteness – steps must be precisely defined or unambiguously specified.
o Generality – generic enough to solve all the problems of a particular class.
o Effectiveness – operations of the algorithm must be basic enough to put down on pencil and
paper. They should not be too complex to warrant writing another algorithm.
o Input-Output – must have certain initial and precise inputs, and outputs that may be
generated both as its intermediate and final steps.
Searching – process of finding a particular data item from a collection of data items based on
specific criteria. A search typically performed using a search key and it answers either True or False
based on the item is present or not in the list.
Searching Techniques
Sorting Techniques
BubbleSort(Array, n) {
for i = 0 to n-2 {
for j = 0 to n-2 {
• Bubble sort/exchange sort – arranges if Array[j] > Array[j+1] {
values by iterating over the list several swap(Array[j], Array[j+1])
times and in each iteration, the larger value }
gets bubble up to the end of the list. }
}
}
SelectionSort(Array, n) {
• Selection sort - a simple sorting algorithm for i = 0 to n-1 {
that sorts an array by repeatedly finding the min_idx = i
minimum element from the unsorted part for j = i+1 to n {
and moving it to the beginning. It improves if Array[j] < Array[min_idx] {
over bubble sort by making only one min_idx = j
exchange per pass. It maintains two }
subarrays: one sorted and one unsorted. In }
each iteration, the minimum element is swap(Array[min_idx], Array[i])
moved from the unsorted to the sorted }
subarray. }
InsertionSort(Array, n) {
for i = 1 to n {
key = Array[i]
• Insertion Sort - considers the elements one j=i-1
at a time, inserting each in its suitable place while (j >= 0 and Array[j] > key) {
among those already considered (keeping Array[j + 1] = Array[j]
them sorted). Insertion sort is an example of j=j-1
an incremental algorithm. It builds the }
sorted sequence one number at a time. Array[j + 1] = key
}
}
Stack
- a container of objects that are inserted and removed according to the last-in first-out (LIFO)
principle.
- a limited access data structure (elements can be added and removed from the stack only at the
top.
- may be implemented to have a bounded capacity.
o overflow state – stack is full, and no items can be pushed.
o underflow state – stack is empty, and no items can be popped.
• Stack ADT
- works on the last-in first-out (LIFO) principle.
- insertion and deletion take place at one end called TOP.
▪ PUSH – add operation of the stack.
▪ POP – delete operation of the stack.
▪ Stack Overflow – PUSH operation on a full stack.
▪ Stack Underflow – POP operation on an empty stack.
▪ SP is a pointer used to access the top element of the stack.
• Stack Using Arrays
o Preliminaries
▪ Create a one-dimensional array with fixed size (int stack[SIZE])
▪ Define an integer variable 'top' and initialize with '-1'. (int top = -1)
o PUSH o POP
▪ if top == SIZE-1: ▪ if top == -1:
print(“Stack Overflow”) print(“Stack Underflow”)
else: else:
top++ delete stack[top]
stack[top] = data top—
o DISPLAY
▪ if top == -1:
print(“Stack Empty”)
else:
for i in range(-1, top+1):
print(stack[top])
• Stack Applications
o Stack is used by compilers to check for balancing of parentheses, brackets, and braces.
o Stack is used to evaluate a postfix expression.
o Stack is used to convert an infix expression into postfix/prefix form.
o In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
o During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.
o Depth first search uses a stack data structure to find an element from a graph.
Queue
else:
for i in range(FRONT, REAR-1):
print(queue[i])
• Applications of Queue
o Used to schedule jobs to be processed by the CPU.
o When multiple users send print jobs to a printer, each printing job is kept in the printing
queue.
o Breadth first search uses a queue data structure to find an element from a graph.
• Disadvantages of Linear Queue
o Time consuming – linear time to be spent in shifting the elements to the beginning of the
queue.
o Signaling queue full even if the queue is having vacant position.
• DEQUE/DEQUEUE (Double-ended Queue)
- generalizes a queue, for which elements can be added or removed from either the front or
the rear.
- Also known as head-tail linked list.
o DEQUE REPRESENTATIONS ▪ Output-restricted DEQUE (ORD)
▪ Input-restricted DEQUE (IRD) - allows deletion from only one
- allows insertions from only one end.
end. o DEQUE OPERATIONS
▪ Insert element at FRONT. ▪ Remove element at FRONT.
▪ Insert element at REAR. ▪ Remove element at REAR.
Tree
- a non-empty set, one element of which is designated the root of the tree, while the remaining
elements are partitioned into non-empty sets, each of which is a subtree of the root.
- a tree T is a set of needs storing elements such that the nodes have a parent-child relationship
that satisfies the following:
▪ If T is not empty, T has a special tree called the root that has no parent.
▪ Each node v of T different than the root has a unique parent node w; each node with
parent w is a child of w.
• Binary Tree
- each node can have at most two children: a left child and a right child. The topmost node is
the root. The left subtree and right subtree attached to the root can be either empty or
smaller binary trees.
• Tree Terminologies
o Leaf node/external node – a node with no children.
o Internal node – a node which is not a leaf.
o Path - A sequence of nodes n1, n2, . . ., nk, such that ni is the parent of ni + 1 for i = 1, 2,. . ., k - 1.
The length of a path is 1 less than the number of nodes on the path. Thus, there is a path of
length zero from a node to itself.
o Siblings – the children of the same parent.
o Ancestor – If there is a path from node A to node B, then A is called an ancestor of B.
o Descendant - If there is a path from node A to node B, then B is called a descendant of A.
o Subtree – any node of a tree, with all its descendants.
o Level – distance from the root. The root has level 0. The maximum number of nodes at any
level is 2n.
o Height of the tree – maximum level in a tree.
o Height of the node - the length of the longest path from the node to a leaf.
o Depth - another term used to denote the height of the tree.
• Assigning Level Numbers and Numbering Nodes for A Binary Tree
o The nodes of a binary tree can be numbered in a natural way, level by level, left to right.
o The nodes of a complete binary tree can be numbered so that the root is assigned the
number 1, a left child is assigned twice the number assigned its parent, and a right child is
assigned one more than twice the number assigned its parent.
• Properties of Binary Trees
o If h = height of a binary tree, then
▪ 2h – maximum number of leaves
▪ (2h+1 – 1) – maximum number of nodes
o If a binary tree contains m nodes at level L, it contains a most (2*m) nodes at level (L + 1).
o The maximum number of nodes at any level L is 2^L.
o The total number of edges in a full binary tree with n node is n - 1.
• Types of Binary Trees
o Complete Binary Tree
▪ A binary tree in which all levels are fully filled except possibly the last, which is filled
from left to right. It contains the first n nodes when nodes are numbered from 1 to n from
top to bottom, left to right.
o Perfect Binary Tree
▪ A binary tree in which all internal nodes have two children, and all leaves are at same
level.
o Balanced Binary Tree
▪ A binary tree is balanced if height of the tree is O(log n) where n is number of nodes.
▪ A binary tree that for every node, the difference in height between its left and right
subtree is no more than 1. If this condition is true for all nodes, then the tree is balanced.
• Array Representation of Binary Tree
o Nodes are indexed from 0 (root) onwards.
o Indexing is done level by level, from left to right, top to bottom.
o Empty nodes are also indexed.
o Each node with index i is stored as the ith element in an array.
o For a node at index n, its left child is at (2n + 1) and right child
at (2n + 2).
o If a node doesn’t have a child, null is stored at the
corresponding array index.
• Binary Tree Traversals
- visiting each node in the tree exactly once.
o Inorder Traversal o Preorder Traversal
▪ Visit all the nodes in the left subtree. ▪ Visit root node.
▪ Visit the root node. ▪ Visit all the nodes in the left subtree.
▪ Visit all the nodes in the right ▪ Visit all the nodes in the right
subtree. subtree.
o Postorder Traversal
▪ Visit all the nodes in the left subtree.
▪ Visit all the nodes in the right subtree.
▪ Visit the root node.
• Binary Search Tree
- a node-based binary tree data structure.
• Properties of Binary Search Tree
o The left sub-tree of a node with keys less than the node’s key.
o The right sub-tree of a node contains only nodes with keys greater than the node’s key.
o The left and right sub-tree each must also be a binary search tree. There must be no
duplicate nodes.
• Searching a key
o To search a given key in Binary Search Tree, we first compare it with root, if the key is present
at root, we return root. If key is greater than root’s key, we recur for right sub-tree of root
node. Otherwise, we recur for left sub-tree.