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

ADS Notes Unit1

Uploaded by

viksonio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

ADS Notes Unit1

Uploaded by

viksonio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

UNIT – I:

Introduction to Algorithm Analysis, Space and Time Complexity analysis, Asymptotic


Notations.
AVL Trees – Creation, Insertion, Deletion operations and Applications B-Trees –
Creation, Insertion, Deletion operations and Applications

Chap 1

Classification of data structure:

Primitive: basic fundamental datatypes

Primitive Data Structures

1. Primitive Data Structures are the data structures consisting of the numbers
and the characters that come in-built into programs.
2. These data structures can be manipulated or operated directly by machine-
level instructions.
3. Basic data types like Integer, Float, Character, and Boolean come under the
Primitive Data Structures.
4. These data types are also called Simple data types, as they contain
characters that can't be divided further

Non-Primitive Data Structures


1. Non-Primitive Data Structures are those data structures derived from
Primitive Data Structures.
2. These data structures can't be manipulated or operated directly by machine-
level instructions.
3. The focus of these data structures is on forming a set of data elements that is
either homogeneous (same data type) or heterogeneous (different data
types).
4. Based on the structure and arrangement of data, we can divide these data
structures into two sub-categories -

a. Linear Data Structures

b. Non-Linear Data Structures

Linear Data Structures

A data structure that preserves a linear connection among its data elements is
known as a Linear Data Structure. The arrangement of the data is done linearly,
where each element consists of the successors and predecessors except the first
and the last data element. However, it is not necessarily true in the case of memory,
as the arrangement may not be sequential.

Based on memory allocation, the Linear Data Structures are further classified into
two types:

1. Static Data Structures: The data structures having a fixed size are known as
Static Data Structures. The memory for these data structures is allocated at
the compiler time, and their size cannot be changed by the user after being
compiled; however, the data stored in them can be altered.
The Array is the best example of the Static Data Structure as they have a
fixed size, and its data can be modified later.
2. Dynamic Data Structures: The data structures having a dynamic size are
known as Dynamic Data Structures. The memory of these data structures is
allocated at the run time, and their size varies during the run time of the code.
Moreover, the user can change the size as well as the data elements stored in
these data structures at the run time of the code.
Linked Lists, Stacks, and Queues are common examples of dynamic data
structures

The following is the list of Linear Data Structures that we generally use:
1. Arrays

An Array is a data structure used to collect multiple data elements of the same data
type into one variable. Instead of storing multiple values of the same data types in
separate variable names, we could store all of them together into one variable. This
statement doesn't imply that we will have to unite all the values of the same data
type in any program into one array of that data type. But there will often be times
when some specific variables of the same data types are all related to one another in
a way appropriate for an array.

An Array is a list of elements where each element has a unique place in the list. The
data elements of the array share the same variable name; however, each carries a
different index number called a subscript. We can access any data element from the
list with the help of its location in the list. Thus, the key feature of the arrays to
understand is that the data is stored in contiguous memory locations, making it
possible for the users to traverse through the data elements of the array using their
respective indexes.

Figure 3. An Array

Arrays can be classified into different types:

a. One-Dimensional Array: An Array with only one row of data elements is


known as a One-Dimensional Array. It is stored in ascending storage location.

b. Two-Dimensional Array: An Array consisting of multiple rows and columns of


data elements is called a Two-Dimensional Array. It is also known as a Matrix.

c. Multidimensional Array: We can define Multidimensional Array as an Array


of Arrays. Multidimensional Arrays are not bounded to two indices or two
dimensions as they can include as many indices are per the need.

Some Applications of Array:

a. We can store a list of data elements belonging to the same data type.

b. Array acts as an auxiliary storage for other data structures.

c. The array also helps store data elements of a binary tree of the fixed count.
d. Array also acts as a storage of matrices.

2. Linked Lists

A Linked List is another example of a linear data structure used to store a collection
of data elements dynamically. Data elements in this data structure are represented
by the Nodes, connected using links or pointers. Each node contains two fields, the
information field consists of the actual data, and the pointer field consists of the
address of the subsequent nodes in the list. The pointer of the last node of the linked
list consists of a null pointer, as it points to nothing. Unlike the Arrays, the user can
dynamically adjust the size of a Linked List as per the requirements.

Figure 4. A Linked List

Linked Lists can be classified into different types:

a. Singly Linked List: A Singly Linked List is the most common type of Linked
List. Each node has data and a pointer field containing an address to the next
node.

b. Doubly Linked List: A Doubly Linked List consists of an information field and
two pointer fields. The information field contains the data. The first pointer field
contains an address of the previous node, whereas another pointer field
contains a reference to the next node. Thus, we can go in both directions
(backward as well as forward).

c. Circular Linked List: The Circular Linked List is similar to the Singly Linked
List. The only key difference is that the last node contains the address of the
first node, forming a circular loop in the Circular Linked List.

Some Applications of Linked Lists:

a. The Linked Lists help us implement stacks, queues, binary trees, and graphs
of predefined size.
b. We can also implement Operating System's function for dynamic memory
management.

c. Linked Lists also allow polynomial implementation for mathematical


operations.

d. We can use Circular Linked List to implement Operating Systems or


application functions that Round Robin execution of tasks.

e. Circular Linked List is also helpful in a Slide Show where a user requires to go
back to the first slide after the last slide is presented.

f. Doubly Linked List is utilized to implement forward and backward buttons in a


browser to move forward and backward in the opened pages of a website.

3. Stacks

A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out) principle
that allows operations like insertion and deletion from one end of the Stack, i.e., Top.
Stacks can be implemented with the help of contiguous memory, an Array, and non-
contiguous memory, a Linked List. Real-life examples of Stacks are piles of books, a
deck of cards, piles of money, and many more.
Figure 5. A Real-life Example of Stack

The above figure represents the real-life example of a Stack where the operations
are performed from one end only, like the insertion and removal of new books from
the top of the Stack. It implies that the insertion and deletion in the Stack can be
done only from the top of the Stack. We can access only the Stack's tops at any
given time.

The primary operations in the Stack are as follows:

a. Push: Operation to insert a new element in the Stack is termed as Push


Operation.

b. Pop: Operation to remove or delete elements from the Stack is termed as


Pop Operation.

Figure 6. A Stack

Some Applications of Stacks:

a. The Stack is used as a Temporary Storage Structure for recursive operations.

b. Stack is also utilized as Auxiliary Storage Structure for function calls, nested
operations, and deferred/postponed functions.

c. We can manage function calls using Stacks.


d. Stacks are also utilized to evaluate the arithmetic expressions in different
programming languages.

e. Stacks are also helpful in converting infix expressions to postfix expressions.

f. Stacks allow us to check the expression's syntax in the programming


environment.

g. We can match parenthesis using Stacks.

h. Stacks can be used to reverse a String.

i. Stacks are helpful in solving problems based on backtracking.

j. We can use Stacks in depth-first search in graph and tree traversal.

k. Stacks are also used in Operating System functions.

l. Stacks are also used in UNDO and REDO functions in an edit.

4. Queues

A Queue is a linear data structure similar to a Stack with some limitations on the
insertion and deletion of the elements. The insertion of an element in a Queue is
done at one end, and the removal is done at another or opposite end. Thus, we can
conclude that the Queue data structure follows FIFO (First In, First Out) principle to
manipulate the data elements. Implementation of Queues can be done using Arrays,
Linked Lists, or Stacks. Some real-life examples of Queues are a line at the ticket
counter, an escalator, a car wash, and many more.
Figure 7. A Real-life Example of Queue

The above image is a real-life illustration of a movie ticket counter that can help us
understand the Queue where the customer who comes first is always served first.
The customer arriving last will undoubtedly be served last. Both ends of the Queue
are open and can execute different operations. Another example is a food court line
where the customer is inserted from the rear end while the customer is removed at
the front end after providing the service they asked for.

The following are the primary operations of the Queue:

a. Enqueue: The insertion or Addition of some data elements to the Queue is


called Enqueue. The element insertion is always done with the help of the
rear pointer.

b. Dequeue: Deleting or removing data elements from the Queue is termed


Dequeue. The deletion of the element is always done with the help of the front
pointer.

Figure 8. A Queue

Some Applications of Queues:

a. Queues are generally used in the breadth search operation in Graphs.

b. Queues are also used in Job Scheduler Operations of Operating Systems,


like a keyboard buffer queue to store the keys pressed by users and a print
buffer queue to store the documents printed by the printer.

c. Queues are responsible for CPU scheduling, Job scheduling, and Disk
Scheduling.

d. Priority Queues are utilized in file-downloading operations in a browser.


e. Queues are also used to transfer data between peripheral devices and the
CPU.

f. Queues are also responsible for handling interrupts generated by the User
Applications for the CPU.

Non-Linear Data Structures

Non-Linear Data Structures are data structures where the data elements are not
arranged in sequential order. Here, the insertion and removal of data are not feasible
in a linear manner. There exists a hierarchical relationship between the individual
data items.

Types of Non-Linear Data Structures

The following is the list of Non-Linear Data Structures that we generally use:

1. Trees

A Tree is a Non-Linear Data Structure and a hierarchy containing a collection of


nodes such that each node of the tree stores a value and a list of references to other
nodes (the "children").

The Tree data structure is a specialized method to arrange and collect data in the
computer to be utilized more effectively. It contains a central node, structural nodes,
and sub-nodes connected via edges. We can also say that the tree data structure
consists of roots, branches, and leaves connected.
Figure 9. A Tree

Trees can be classified into different types:

a. Binary Tree: A Tree data structure where each parent node can have at most
two children is termed a Binary Tree.

b. Binary Search Tree: A Binary Search Tree is a Tree data structure where we
can easily maintain a sorted list of numbers.

c. AVL Tree: An AVL Tree is a self-balancing Binary Search Tree where each
node maintains extra information known as a Balance Factor whose value is
either -1, 0, or +1.

d. B-Tree: A B-Tree is a special type of self-balancing Binary Search Tree where


each node consists of multiple keys and can have more than two children.

Some Applications of Trees:

a. Trees implement hierarchical structures in computer systems like directories


and file systems.

b. Trees are also used to implement the navigation structure of a website.

c. We can generate code like Huffman's code using Trees.

d. Trees are also helpful in decision-making in Gaming applications.

e. Trees are responsible for implementing priority queues for priority-based OS


scheduling functions.

f. Trees are also responsible for parsing expressions and statements in the
compilers of different programming languages.

g. We can use Trees to store data keys for indexing for Database Management
System (DBMS).

h. Spanning Trees allows us to route decisions in Computer and


Communications Networks.
i. Trees are also used in the path-finding algorithm implemented in Artificial
Intelligence (AI), Robotics, and Video Games Applications.

2. Graphs

A Graph is another example of a Non-Linear Data Structure comprising a finite


number of nodes or vertices and the edges connecting them. The Graphs are utilized
to address problems of the real world in which it denotes the problem area as a
network such as social networks, circuit networks, and telephone networks. For
instance, the nodes or vertices of a Graph can represent a single user in a telephone
network, while the edges represent the link between them via telephone.

The Graph data structure, G is considered a mathematical structure comprised of a


set of vertices, V and a set of edges, E as shown below:

G = (V,E)

Figure 10. A Graph

The above figure represents a Graph having seven vertices A, B, C, D, E, F, G, and


ten edges [A, B], [A, C], [B, C], [B, D], [B, E], [C, D], [D, E], [D, F], [E, F], and [E, G].

Depending upon the position of the vertices and edges, the Graphs can be
classified into different types:

a. Null Graph: A Graph with an empty set of edges is termed a Null Graph.
b. Trivial Graph: A Graph having only one vertex is termed a Trivial Graph.

c. Simple Graph: A Graph with neither self-loops nor multiple edges is known
as a Simple Graph.

d. Multi Graph: A Graph is said to be Multi if it consists of multiple edges but no


self-loops.

e. Pseudo Graph: A Graph with self-loops and multiple edges is termed a


Pseudo Graph.

f. Non-Directed Graph: A Graph consisting of non-directed edges is known as


a Non-Directed Graph.

g. Directed Graph: A Graph consisting of the directed edges between the


vertices is known as a Directed Graph.

h. Connected Graph: A Graph with at least a single path between every pair of
vertices is termed a Connected Graph.

i. Disconnected Graph: A Graph where there does not exist any path between
at least one pair of vertices is termed a Disconnected Graph.

j. Regular Graph: A Graph where all vertices have the same degree is termed
a Regular Graph.

k. Complete Graph: A Graph in which all vertices have an edge between every
pair of vertices is known as a Complete Graph.

l. Cycle Graph: A Graph is said to be a Cycle if it has at least three vertices and
edges that form a cycle.

m. Cyclic Graph: A Graph is said to be Cyclic if and only if at least one cycle
exists.

n. Acyclic Graph: A Graph having zero cycles is termed an Acyclic Graph.

o. Finite Graph: A Graph with a finite number of vertices and edges is known as
a Finite Graph.
p. Infinite Graph: A Graph with an infinite number of vertices and edges is
known as an Infinite Graph.

q. Bipartite Graph: A Graph where the vertices can be divided into independent
sets A and B, and all the vertices of set A should only be connected to the
vertices present in set B with some edges is termed a Bipartite Graph.

r. Planar Graph: A Graph is said to be a Planar if we can draw it in a single


plane with two edges intersecting each other.

s. Euler Graph: A Graph is said to be Euler if and only if all the vertices are
even degrees.

t. Hamiltonian Graph: A Connected Graph consisting of a Hamiltonian circuit is


known as a Hamiltonian Graph.

Some Applications of Graphs:

a. Graphs help us represent routes and networks in transportation, travel, and


communication applications.

b. Graphs are used to display routes in GPS.

c. Graphs also help us represent the interconnections in social networks and


other network-based applications.

d. Graphs are utilized in mapping applications.

e. Graphs are responsible for the representation of user preference in e-


commerce applications.

f. Graphs are also used in Utility networks in order to identify the problems
posed to local or municipal corporations.

g. Graphs also help to manage the utilization and availability of resources in an


organization.

h. Graphs are also used to make document link maps of the websites in order to
display the connectivity between the pages through hyperlinks.

i. Graphs are also used in robotic motions and neural networks.


Basic Operations of Data Structures

In the following section, we will discuss the different types of operations that we can
perform to manipulate data in every data structure:

1. Traversal: Traversing a data structure means accessing each data element


exactly once so it can be administered. For example, traversing is required
while printing the names of all the employees in a department.
2. Search: Search is another data structure operation which means to find the
location of one or more data elements that meet certain constraints. Such a
data element may or may not be present in the given set of data elements.
For example, we can use the search operation to find the names of all the
employees who have the experience of more than 5 years.
3. Insertion: Insertion means inserting or adding new data elements to the
collection. For example, we can use the insertion operation to add the details
of a new employee the company has recently hired.
4. Deletion: Deletion means to remove or delete a specific data element from
the given list of data elements. For example, we can use the deleting
operation to delete the name of an employee who has left the job.
5. Sorting: Sorting means to arrange the data elements in either Ascending or
Descending order depending on the type of application. For example, we can
use the sorting operation to arrange the names of employees in a department
in alphabetical order or estimate the top three performers of the month by
arranging the performance of the employees in descending order and
extracting the details of the top three.
6. Merge: Merge means to combine data elements of two sorted lists in order to
form a single list of sorted data elements.
7. Create: Create is an operation used to reserve memory for the data elements
of the program. We can perform this operation using a declaration statement.
The creation of data structure can take place either during the following:

a. Compile-time

b. Run-time
For example, the malloc() function is used in C Language to create
data structure.
8. Selection: Selection means selecting a particular data from the available
data. We can select any particular data by specifying conditions inside the
loop.
9. Update: The Update operation allows us to update or modify the data in the
data structure. We can also update any particular data by specifying some
conditions inside the loop, like the Selection operation.
10. Splitting: The Splitting operation allows us to divide data into various
subparts decreasing the overall process completion time.

--------------------------Revision of Data Structures complete


----------------------------------------------------------

Time Complexity

Understanding Time Complexity with Simple Examples


A lot of students get confused while understanding the concept of time complexity,
but in this article, we will explain it with a very simple example.
Q. Imagine a classroom of 100 students in which you gave your pen to one
person. You have to find that pen without knowing to whom you gave it.
Here are some ways to find the pen and what the O order is.
 O(n2): You go and ask the first person in the class if he has the pen. Also, you
ask this person about the other 99 people in the classroom if they have that
pen and so on,
This is what we call O(n2).
 O(n): Going and asking each student individually is O(N).
 O(log n): Now I divide the class into two groups, then ask: “Is it on the left
side, or the right side of the classroom?” Then I take that group and divide it
into two and ask again, and so on. Repeat the process till you are left with one
student who has your pen. This is what you mean by O(log n).
I might need to do:
 The O(n2) searches if only one student knows on which student the pen
is hidden.
 The O(n) if one student had the pen and only they knew it.
 The O(log n) search if all the students knew, but would only tell me if I
guessed the right side.
The above O -> is called Big – Oh which is an asymptotic notation. There are
other asymptotic notations like theta and Omega.

To travel from House A to House B, above ways of transport is possible, but we have
challenges of cost , speed. We don’t prefer to go in rocket because it’s the fastest,
because of cost. Similarly, we don’t prefer bicycle to travel because it is cheap, as
the speed gets compromised there.
Same way algorithm needs to work on these parameters for their algorithm to be the
best, average or worst algorithm. 1. Big-O Notation
We define an algorithm’s worst-case time complexity by using the Big-O notation,
which determines the set of functions grows slower than or at the same rate as the
expression. Furthermore, it explains the maximum amount of time an algorithm
requires to consider all input values.
2. Omega Notation
It defines the best case of an algorithm’s time complexity, the Omega notation
defines whether the set of functions will grow faster or at the same rate as the
expression. Furthermore, it explains the minimum amount of time an algorithm
requires to consider all input values.
3. Theta Notation
It defines the average case of an algorithm’s time complexity, the Theta notation
defines when the set of functions lies in
both O(expression) and Omega(expression), then Theta notation is used. This is
how we define a time complexity average case for an algorithm.
Some of Searching & Sorting algorithms and their time complexities:
Space complexity
Space complexity refers to the total amount of memory space used by an
algorithm/program, including the space of input values for execution.
Method for Calculating Space and Time Complexity
Methods for Calculating Time Complexity
To calculate time complexity, you must consider each line of code in the program.
Consider the multiplication function as an example. Now, calculate the time
complexity of the multiply function:

1. mul <- 1
2. i <- 1
3. While i <= n do
4. mul = mul * 1
5. i=i+1
6. End while

Let T(n) be a function of the algorithm's time complexity. Lines 1 and 2 have a time
complexity of O. (1). Line 3 represents a loop. As a result, you must repeat lines 4
and 5 (n -1) times. As a result, the time complexity of lines 4 and 5 is O. (n).
Finally, adding the time complexity of all the lines yields the overall time complexity
of the multiple function fT(n) = O(n).
The iterative method gets its name because it calculates an iterative algorithm's time
complexity by parsing it line by line and adding the complexity.
Aside from the iterative method, several other concepts are used in various cases.
The recursive process, for example, is an excellent way to calculate time complexity
for recurrent solutions that use recursive trees or substitutions. The master's theorem
is another popular method for calculating time complexity.
Methods for Calculating Space Complexity
With an example, you will go over how to calculate space complexity in this section.
Here is an example of computing the multiplication of array elements:

1. int mul, i
2. While i < = n do
3. mul <- mul * array[i]
4. i <- i + 1
5. end while
6. return mul

Let S(n) denote the algorithm's space complexity. In most systems, an integer
occupies 4 bytes of memory. As a result, the number of allocated bytes would be the
space complexity.
Line 1 allocates memory space for two integers, resulting in S(n) = 4 bytes multiplied
by 2 = 8 bytes. Line 2 represents a loop. Lines 3 and 4 assign a value to an already
existing variable. As a result, there is no need to set aside any space. The return
statement in line 6 will allocate one more memory case. As a result, S(n)= 4 times 2
+ 4 = 12 bytes.
Because the array is used in the algorithm to allocate n cases of integers, the final
space complexity will be fS(n) = n + 12 = O (n).
As you progress through this tutorial, you will see some differences between space
and time complexity.

Difference Between Time Complexity and Space Complexity


Below is a table showing the differences between time complexity and space
complexity.
Aspect Time Complexity Space Complexity
Definition Measures the execution Measures the memory usage of
time of an algorithm an algorithm relative to input
relative to input size. size.
Amount of time an
Amount of memory (RAM) an
Focus algorithm takes to
algorithm requires for execution.
complete.
Primary Efficiency in terms of memory
Efficiency in terms of time.
Concern usage.
Number of operations or Memory allocation for variables,
Analysis
computational steps data structures, and function
Approach
relative to input size. calls relative to input size.
Larger inputs may require more
Input Size Larger inputs usually lead memory for storing data
Impact to longer execution times. structures or managing
recursion.
Reducing the number of Minimizing the amount of
Optimization
computational operations memory needed for data
Goal
or steps. storage and processing.

Finding an element using


Typical Creating a 2D array of size n x
binary search (O(log n)
Example n (O(n²) space complexity).
time complexity).

What is Time Complexity?


Time complexity is a concept in computer science that describes the amount of
time an algorithm takes to run as a function of the length of the input. It's a crucial
aspect of algorithm analysis as it helps understand how efficiently an algorithm
performs, particularly as the size of the input data increases.

Key Points About Time Complexity


1. Measure of Efficiency: Time complexity provides a way to quantify the efficiency
of an algorithm in terms of time. It's particularly important for understanding the
scalability of an algorithm.
2. Big O Notation: Time complexity is often expressed using Big O notation, which
provides an upper bound on the time requirements of an algorithm in the worst-case
scenario. For example, O(n) denotes linear time complexity, meaning the time
required grows linearly with the size of the input.
3. Types of Time Complexity:
 Constant Time (O(1)): The execution time remains constant regardless of the
input size.
 Logarithmic Time (O(log n)): The execution time increases logarithmically
with an increase in input size. Binary search is a classic example.
 Linear Time (O(n)): The execution time increases linearly with the input size.
For instance, finding an item in an unsorted list.
 Quadratic Time (O(n²)): The time increases quadratically with the input size.
This is common in algorithms with nested loops over the input data.
 Exponential Time (O(2n)): The execution time doubles with each addition to
the input data set. This is typical of algorithms that solve problems by
computing all possible combinations.
4. Worst, Average, and Best Case: Time complexity can refer to the worst-case
(usually represented), average-case, or best-case scenario for an algorithm's
running time. The worst-case time complexity is the most commonly used because it
guarantees the maximum time taken for any input.
5. Impact on Real-world Applications: In practical scenarios, the time complexity
of an algorithm can significantly impact its usability. For large input sizes, an
algorithm with a lower time complexity will generally perform better than one with a
higher time complexity.

What is Space Complexity?


Space complexity is a term in computer science used to describe the amount of
memory space required by an algorithm to run as a function of the length of the
input. It is an important metric for understanding how efficient an algorithm is in
terms of memory usage, especially in environments where memory resources are
limited.

Key Points About Space Complexity

1. Memory Usage Measurement: Space complexity measures the total amount of


memory or storage space an algorithm needs to complete. This includes both the
space taken up by the input data and any additional space used by the algorithm
for variables, data structures, and function calls.
2. Big O Notation: Like time complexity, space complexity is often expressed using
Big O notation. This notation provides an upper bound on the space requirements of
an algorithm in the worst-case scenario. For example, O(n) indicates that the space
required grows linearly with the input size.
3. Types of Space Complexity:
 Constant Space (O(1)): The algorithm uses a fixed amount of memory space
regardless of the input size. For example, an algorithm that swaps two
numbers.
 Linear Space (O(n)): The memory required grows linearly with the input size.
An example is creating a list of 'n' elements.
 Quadratic Space (O(n²)): The space requirement grows quadratically with
the input size, commonly seen in algorithms that create two-dimensional
arrays based on the input size.
4. Components of Space Complexity:
 Static Part: The fixed space required by the algorithm (for code, constants,
and simple variables).
 Dynamic Part: The variable space required by the algorithm during its
execution, including space for dynamic data structures, stack space for
recursion, and so on.
Impact on Algorithm Design: Space complexity is a crucial consideration
when designing algorithms, particularly for systems with limited memory
resources like embedded systems or mobile devices.
5. Trade-off with Time Complexity: Sometimes, there's a trade-off between
space and time complexity. For example, using extra space for caching
(space complexity) can reduce the time it takes to retrieve data (time
complexity).

Similarities Between Time Complexity and Space Complexity


Below is a table showcasing the similarities between time complexity and space
complexity
Similarities Description
Assessment of Algorithm Both are used to gauge how efficient an algorithm
Efficiency is.
Both are analyzed in relation to the size of the
Reliance on Input Size
input data.
Utilization of Big O Both are commonly expressed using Big O
Notation notation.
Influence on Algorithm Both are crucial factors in designing and
Design optimizing algorithms.
Both indicate how well an algorithm scales with
Indicators of Scalability
larger input sizes.
Critical in Resource Both are key in managing computational
Management resources effectively.

Conclusion
Thus, while time complexity and space complexity share similarities in their role in
evaluating algorithm efficiency and scalability, they fundamentally differ in the
specific resources they measure and their implications on algorithm design and
performance.
------------------------------------------------------------------------------------------------------------
Binary Search Tree

A Binary Search Tree is a data structure used in computer science for organizing
and storing data in a sorted manner. Each node in a Binary Search Tree has at most
two children, a left child and a right child, with the left child containing values less
than the parent node and the right child containing values greater than the parent
node. This hierarchical structure allows for efficient searching, insertion,
and deletion operations on the data stored in the tree.
Binary Search Tree

AVL Trees
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees for any node cannot be more
than one.

The difference between the heights of the left subtree and the right subtree for any
node is known as the balance factor of the node.

The AVL tree is named after its inventors, Georgy Adelson-Velsky and Evgenii
Landis, who published it in their 1962 paper “An algorithm for the organization of
information”.

AVL Tree Rotations

AVL tree rotation is a fundamental operation used in self-balancing binary search


trees, specifically in AVL trees. Due to any operations like insertion or deletion, if any
node of an AVL tree becomes unbalanced, specific tree rotations are performed to
restore the balance.

The tree rotations involve rearranging the tree structure without changing the order
of elements. The positions of the nodes of a subtree are interchanged. There are
four types of AVL rotations:
1. Left Rotation (LL Rotation)

In a left rotation, a node's right child becomes the new root, while the original node
becomes the left child of the new root. The new root's left child becomes the original
node's right child.

2. Right Rotation (RR Rotation)

In a right rotation, a node's left child becomes the new root, while the original node
becomes the right child of the new root. The new root's right child becomes the
original node's left child.
3. Left-Right Rotation (LR Rotation)

An LR rotation is a combination of a left rotation followed by a right rotation. It is


performed when the left subtree of a node is unbalanced to the right, and the right
subtree of the left child of that node is unbalanced to the left.

4. Right-Left Rotation (RL Rotation)

An RL rotation is a combination of a right rotation followed by a left rotation. It is


performed when the right subtree of a node is unbalanced to the left, and the left
subtree of the right child of that node is unbalanced to the right.
Standard Operations on AVL Trees in Data Structures

1. Insertion: A newNode is always inserted as a leaf node with a balance factor


equal to 0. After each insertion, the ancestors of the newly inserted node are
examined because the insertion only affects their heights, potentially inducing
an imbalance. This process of traversing the ancestors to find the unbalanced
node is called retracing.

Algorithm for Insertion in an AVL Tree

Step 1: START

Step 2: Insert the node using BST insertion logic.

Step 3: Calculate and check the balance factor of each node.

Step 4: If the balance factor follows the AVL criterion, go to step 6.

Step 5: Else, perform tree rotations according to the insertion done. Once the tree is
balanced go to step 6.

Step 6: END

Example of AVL Trees:


The above tree is AVL because the differences between the heights of left and right
subtrees for every node are less than or equal to 1.

Example of a Tree that is NOT an AVL Tree:

The above tree is not AVL because the differences between the heights of the left
and right subtrees for 8 and 12 are greater than 1.

AVL Tree Insertion


AVL insertion Example :

Insert 9

Insert 15

Insert 20
Imbalance at node 15. H(LST)-H(RST) = 0-2 =-2
Which is not in range {-1,0,1}

Insert 8

Insert 7
Imbalance at node 8

Insert :13
Imbalance at 15 because of 8
With LR Rotation 15-8-9 .. left heavy swap & RR

Insert 10
AVL Deletion
AVL Deletion Example:
Delete 88

Delete 99
Left heavy -RR rotation
Delete 22
1. Time Complexity:
Operations Best Case Average Case Worst Case

Insertion O (log n) O (log n) O (log n)

Deletion O (log n) O (log n) O (log n)

Traversal O (n) O (n) O (n)

Search O (log n) O (log n) O (log n)

2. Space Complexity: Space complexity is the same as that of Binary Search


Trees i.e., O(n) as AVL Trees don't modify the data itself.
Applications of AVL Trees

1. It is used to index huge records in a database and also search in that


efficiently.
2. For all types of in-memory collections, including sets and dictionaries, AVL
Trees are used.
3. Database applications, where insertions and deletions are less common but
frequent data lookups are necessary.
4. Software that needs optimized search.
5. It is applied in corporate areas and storyline games.
Advantages of AVL Trees

1. AVL trees are capable of self-balancing.


2. It cannot be skewed.
3. Compared to Red-Black Trees, it offers faster lookups.
4. Superior searching efficiency compared to other trees, such as the binary
tree.
5. Height is limited to log(n), where n is the number of nodes in the tree as a
whole.
Disadvantages of AVL Trees

1. Implementing it is challenging.
2. Certain procedures have high constant factors.
3. Compared to Red-Black trees, AVL trees are less common.
4. As additional rotations are conducted, AVL trees offer complex insertion and
removal procedures because of their rather rigid balance.
5. Requires to process more for balancing.
-----------------------------------------------------------------------------------------------------
M-way search Tree

multiway trees-also referred to as multi-ary trees or generic trees-are a fundamental


type of data structure. They offer a flexible approach to describe hierarchical
structures and are used in a variety of contexts, including file systems, databases,
and parser trees.
m-way tree Example

Insert 14,
Insert 32

Insert 11, 6, 7, 4, 3 [assume increasing order possible]

Insert 88. 93, 54,37


Insert 89,62,64

Insert
Insert 75, 35, 36:
Deletion of M-way tree

B Tree
A B-tree is a self-balancing tree where all the leaf nodes are at the same level which
allows for efficient searching, insertion and deletion of records.

Because of all the leaf nodes being on the same level, the access time of data is
fixed regardless of the size of the data set.

Characteristics of B-Tree?

B-trees have several important characteristics that make them useful for storing and
retrieving large amounts of data efficiently. Some of the key characteristics of B-trees
are:

 Balanced: B-trees are balanced, meaning that all leaf nodes are at the same
level. This ensures that the time required to access data in the tree remains
constant, regardless of the size of the data set.
 Self-balancing: B-trees are self-balancing, which means that as new data is
inserted or old data is deleted, the tree automatically adjusts to maintain its
balance.
 Multiple keys per node: B-trees allow multiple keys to be stored in each node.
This allows for efficient use of memory and reduces the height of the tree,
which in turn reduces the number of disk accesses required to retrieve data.
 Ordered: B-trees maintain the order of the keys, which makes searching and
range queries efficient.
 Efficient for large data sets: B-trees are particularly useful for storing and
retrieving large amounts of data, as they minimize the number of disk
accesses required to find a particular piece of data.
Application of B-Tree:

B-trees are commonly used in applications where large amounts of data need to be
stored and retrieved efficiently. Some of the specific applications of B-trees include:

 Databases: B-trees are widely used in databases to store indexes that allow
for efficient searching and retrieval of data.
 File systems: B-trees are used in file systems to organize and store files
efficiently.
 Operating systems: B-trees are used in operating systems to manage
memory efficiently.
 Network routers: B-trees are used in network routers to efficiently route
packets through the network.
 DNS servers: B-trees are used in Domain Name System (DNS) servers to
store and retrieve information about domain names.
 Compiler symbol tables: B-trees are used in compilers to store symbol tables
that allow for efficient compilation of code.
Advantages of B-Tree:

B-trees have several advantages over other data structures for storing and retrieving
large amounts of data. Some of the key advantages of B-trees include:

 Sequential Traversing: As the keys are kept in sorted order, the tree can be
traversed sequentially.
 Minimize disk reads: It is a hierarchical structure and thus minimizes disk
reads.
 Partially full blocks: The B-tree has partially full blocks which speed up
insertion and deletion.
Disadvantages of B-Tree:

 Complexity: B-trees can be complex to implement and can require a


significant amount of programming effort to create and maintain.
 Overhead: B-trees can have significant overhead, both in terms of memory
usage and processing time. This is because B-trees require additional
metadata to maintain the tree structure and balance.
 Not optimal for small data sets: B-trees are most effective for storing and
retrieving large amounts of data. For small data sets, other data structures
may be more efficient.
 Limited branching factor: The branching factor of a B-tree determines the
number of child nodes that each node can have. B-trees typically have a fixed
branching factor, which can limit their performance for certain types of data.

B Tree Algorithm
Insert 12
Insert 23

Insert 6
But the node is full , so take the median of 6,12,23
12 is the median , so push it up and make left keys as LST and right keys are RST

Insert 8

Insert 15

Insert 19
19 is the median , push it up to prev node, space is there . make 15 as LST, 23 as
RST

Insert 45:

Insert 1:

Split at median 6, but upper node is also full


6,12,19
So 12 is the median , move further up
Inseert 4:

Place is there in 1 ,
So 4 fits

Insert 7:
Place is there in 8, so place 7

Insert 5
Left of 6, we have 1&4
So 1,4,5
Split node at 4.
Final Answer

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
B- Tree Deletion Example:

Delete 32,14,15, 56, 34, 41

Delete 32
Simply delete 32 because it is the leaf node and case 1.1 , so delete the key

Delete 14

1.2.1 -> largest left sibling is 12, but if its borrowed, the keys < min keys, so cant use
it
1.2.2-> smallest right sibling is 22, and after its borrowed , the keys are more than
minimum.. so we can use it.
Now replace the “22” with “21” in parent and shift 21 to the child node
Delete 15

Merge left sibling with the parent and delete the target node.. link is removed at left
of 13.

But here 22 is a single node –less than min keys, so we further merge
The root node can have 67 (as a single node)

Delete 56:

Left subtree largest no is 55, replaced with 56 and 55 brought down and deleted
Delete 34

LST borrow has less than min keys, so lets borrow from RST =the smallest 41

Delete 41
Both sides less than min keys, so we merge both with parent
23,31,41,43,44 and delete 41

For testing the height case:


Height =2 is made to height reduction

Height =1
-----------------------------------------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy