Data Structure 2
Data Structure 2
Arrays
Definition:
It is a data structure consisting of a collection of elements at contiguous memory locations, each
identified by an index.
Elements 5 1 4 2 6
Indexes 0 1 2 3 4
Key concepts:
• Indexing: Elements can be accessed using their index
• Memory allocation: Contiguous block of memory
Common operations:
Operation Example Time Complexity (worst-case)
Insertion 3 5 1 3 4 2 6 O(n)
x array
3 5 1 4 3 2 6
Deletion x array O(n)
Shifting
5 1 4 2 6
array with 3 deleted
Search 3 5 1 3 4 2 6 O(n)
x array
Application:
• Dynamic Programming—Store intermediate results to avoid redundant calculations, improving
efficiency
Pros:
• Quick access: O(1) time complexity for accessing elements by index
• Predictable memory use: Fixed size makes memory management easy
Cons:
• Fixed size: Specifying size at the time of creation leads to potentially wasted or insufficient space
• Expensive insertion/deletion: Inserting or deleting elements (except at the end) requires shifting
elements, resulting in O(n) time complexity
A linked list
A B C D
Head NULL
Common operations:
Operation Example Time Complexity (worst-case)
Head
A B C D
Insertion old link O(n)
Data Next
E
new node
Head
Deletion O(n)
A B C D NULL
A B C D NULL
Search O(n)
Head Node to be searched is C
Traversal:
• Forward: Only in the forward direction
Application:
• Implementing stacks and queues
Pros:
• Dynamic size: Grow or shrink as needed
• No contiguous memory requirement: Does not require a large block of contiguous memory
Cons:
• Extra memory overhead: Requires additional memory for storing pointers
• Sequential access: Traverse nodes sequentially to access an element
NULL prev data next prev data next prev data next prev data next NULL
CS101 Data Structures
Common operations:
Time Complexity
Operation Example
(worst-case)
Head
prev 0 next
New node
Head
Search O(n)
NULL prev 2 next prev 4 next prev 8 next NULL
Node to be searched is 4
Traversal:
• Forward and backward: Traversal is in both directions (forward and backward)
Application:
• Implementing complex data structures like Fibonacci heaps
Pros:
• Bidirectional traversal: Traverse the list in both directions
• Dynamic Size: Grow or shrink as needed
Cons:
• Extra memory overhead: Requires additional memory to store two pointers per node
• Complex implementation: More complex than singly linked lists
Stacks
Definition:
It’s a collection of elements following the Last In, First Out (LIFO) principle, where the most recently
added element is the first to be removed.
Data4
Data3
Data2
Data1
CS101 Data Structures
Common operations:
• Push: O(1) — Adds an element to the top of the stack
Push
C
C
Push O(1)
B
Pop
C
C
Pop O(1)
B
Top C
Top B O(1)
Application:
• Expression evaluation and syntax parsing (e.g., converting infix expressions to postfix or evaluating
postfix expressions)
Pros:
• Efficient operations: O(1) time complexity for push and pop operations
Cons:
• Sequential access: Elements are accessed in a LIFO order
*Note: It can be implemented using arrays or linked lists.
Queues
Definition:
It’s a collection of elements following the First In, First Out (FIFO) principle, where the first element
added is the first to be removed.
Enqueue
Front/ Head Back/ Tail/ Rear
3 4 5 6 7 8
Dequeue
CS101 Data Structures
Common operations:
Operation Example Time Complexity (worst-case)
8
Enqueue O(1)
3 4 5 6 7 8
Enqueue
3 4 5 6 7 8
Dequeue O(1)
3
Dequeue
Front
Front 3 4 5 6 7 8 O(1)
Application:
• Task management (e.g., printer queue)
Pros:
• Efficient operations: O(1) time complexity for enqueue and dequeue operations
Cons:
• Sequential access: Elements are accessed in a FIFO order
*Note: It can be implemented using arrays or linked lists.
Binary Trees
Definition:
It’s a data structure in which each node has at most two children, referred to as the left and right
children.
Parent
22 10
Types:
1. Full binary tree: Every node has 0 or 2 children.
2 3
4 5
6 7
CS101 Data Structures
2. Complete binary tree: All levels are completely filled except possibly the last level, filled from left to
right.
1
2 3
4 5 6
3. Perfect binary tree: All internal nodes have two children, and all leaves are at the same level.
2 3
4 5 6 7
3. Traversals:
• In-order (LNR): Visit the left subtree, node, and the right subtree.
Initial traversal
from root to left 1
most node
2 3
4 5 6
In-order traversal: 4 2 5 1 3 6
• Pre-order (NLR): Visit the node, left subtree, and right subtree.
2 3
4 5 6
Pre-order traversal: 1 2 4 5 3 6
• Post-order (LRN): Visit the left subtree, right subtree, and node.
Initial traversal
from root to left 1
most node
2 3
4 5 6
Post-order traversal: 4 5 2 6 3 1
CS101 Data Structures
• Level-order: Visit nodes level by level.
12 13
7 14 2
17 23 7 3 8 11
Level-order traversal: 5 12 13 7 14 2 17 23 7 3 8 11
4. Common operations:
• Insertion: O(n)
10 10
11 9 11 9
7 15 8 7 12 15 8
After inserting 12
• Deletion: O(n)
13 13
12 10 9 10
4 19 16 9 4 19 16 12
13
Deleting the 9 10
deepest node
4 19 16
• Search: O(n)
10
11 9 Node to be searched is 9
7 15 8
CS101 Data Structures
Application:
• Hierarchical data representation (e.g., file systems, databases)
Pros:
• Hierarchical structure: Ideal for representing hierarchical data
• Multiple traversals: Provides various traversal methods for different use cases
Cons:
• Inefficient operations: O(n) time complexity for insertion, deletion, and searching
• Memory overhead: Requires memory for storing pointers for each node
3 9
1 5 7 11
Common operations:
• Insertion: O(h) (h = height)
100 100
insert 40
20 500 20 500
10 30 10 30
40
50 50
Replace 12 with 6
and delete 12
25 75 25 75
Delete node 12 12 30 60 6 30 60
6 52 70 12 52 70
Deleted node
CS101 Data Structures
• Search: O(h) (h = height)
100
10 30
Application:
• Implementing associative arrays and priority queues
Pros:
• Efficient searching/sorting: O(h) time complexity for searching, insertion, and deletion
• Maintains order: Keeps elements in a sorted order
Cons:
• Height-dependent: Performance depends on the height of the tree; in the worst case (skewed tree),
it can degrade to O(n)
• Duplicate handling: Typically, duplicates are discarded, which may not be suitable for all use cases