Data Structure
Data Structure
Prerequisites:
- Basics of programming language
- Basics knowledge of classes and object
Data structure: - is a data organization, management, and storage formation that enables
efficient access and modification.
Data structure: - is a systematic way of organizing a collection of data.
- Access
- Insertion
- Deletion
- Search
-------------------------------------------------------------------------
Primitive Data Structures: -
→ integer
→ float
→ character
→ pointers
Non-Primitive Data Structures: -
→ Linear Data Structures (Linear list):
→ Array
→ Linked List
→ Stack
→ Queue
→ Linear Data Structures (Linear list):
→ Trees
→ Graphs
Complexity
→ Print stack[top]
→ Array-Based Implementation of the ADT stack
Array
→ Fixed Access Time: O (1)
→ Fixed Size
→ Sequential
→ Bad removal
→ Bad Insertion
→ Space Wastage
Linked List
→ Dynamic Size
→ Sequential Space Does Not Require
→ Good Removal
→ Good Insertion
→ No Space Wastage
→ Random Access Is Not Allowed
- Every Node in the list has an item and the pointer saves an address for the next index,
but the last node has an item and NULL.
Find Predecessor:
1. The node has x a right subtree → maximum number in right subtree.
predecessor (20): 15
2. The node x doesn’t have the left subtree, and it is the left child → number less than
x
predecessor (55): 50
2. The node x doesn’t have the left subtree, and it is the right child → Choose parent
predecessor (65): 60
AVL Trees
Search = O (log n)
Balanced factor: | h (Left sub-Tree) – h (Right sub-Tree) | <= 1
Sorting
- Ascending Order: - Descending Order:
→ Max
→ Min
→ Search
→ Find duplicate elements
Selection Sort
- Simple sorting algorithm.
- In-place algorithm.
- Time Complexity:
- Average case: O (n2)
- Worst Case: O (n2)
- Space Complexity: O (1)
- We call the “Selection Sort” when the size of the Array is small.
60 40 50 30 10 20 | min element = 10 | swap(60, 10)
10 | 40 50 30 60 20 | min element = 20 | swap(40, 20)
10 20 | 50 30 60 40 | min element = 30 | swap(50, 30)
10 20 30 | 50 60 40 | min element = 40 | swap(50, 40)
10 20 30 40 | 60 50 | min element = 50 | swap(60, 50)
10 20 30 40 50 | 60 → Sorted
Bubble Sort
| | | | | 100 60 20 10 30 90 | if(100 > 60)? swap | pass 1
| | | | 60 | 100 20 10 30 90 | if(100 > 20)? swap | pass 1
| | | | 60 20 | 100 10 30 90 | if(100 > 10)? swap | pass 1
| | | | 60 20 10 | 100 30 90 | if(100 > 30)? swap | pass 1
| | | | 60 20 10 30 | 100 90 | if(100 > 90)? swap | pass 1
| | | | 60 20 10 30 90 | 100 | pass 1
80 | 90 60 30 50 70 40 | 90 > 80
80 90 | 60 30 50 70 40 | 60 < 80
60 80 90 | 30 50 70 40 | 60 < 80
30 60 80 90 | 50 70 40 | 30 < 60
30 60 80 90 | 50 70 40 | 30 < 50 < 60
30 50 60 80 90 | 70 40 | 60 < 70 < 80
30 50 60 70 80 90 | 40 | 30 < 40 < 60
30 40 50 60 70 80 90 -> Sorted
Marge Sort
(Divide – and - Conquer)
- Time Complexity: - Space Complexity: O (n)
- Best-case: O (n log2 n)
- Average case: O (n log2 n)
- Worst Case: O (n log2 n)
Quick Sort
(Divide – and - Conquer)
- Time Complexity: - Space Complexity: O (1)
- Best-case: O (n log2 n)
- Average case: O (n log2 n)
- Worst Case: O (n log2 n)
Heap Sort
(Complete Binary Tree)
- Time Complexity: - Space Complexity: O (1)
- Best-case: O (n log2 n)
- Average case: O (n log2 n)
- Worst Case: O (n log2 n)
Linear Search
- Time Complexity: - Space Complexity: O (1)
- Best-case: O (1)
- Average case: O (n)
- Worst Case: O (n)
Binary Search
(Divide – and - Conquer)
- Time Complexity: - Space Complexity: O (1)
- Best-case: O (1)
- Average case: O (Log n)
- Worst Case: O (Log n)