Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Data Structure

The document discusses different data structures and algorithms. It covers topics like primitive and non-primitive data structures, complexity analysis, stacks, queues, linked lists, trees, binary trees, sorting algorithms, and searching algorithms.

Uploaded by

anas hany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Structure

The document discusses different data structures and algorithms. It covers topics like primitive and non-primitive data structures, complexity analysis, stacks, queues, linked lists, trees, binary trees, sorting algorithms, and searching algorithms.

Uploaded by

anas hany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Date 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

How much time and space does it take?

Best Case → Omega Notation. Average Case → Theta Notation.


Worst Case → Big O Notation.
For (int i = 1; i <= n; i++) → O (n)
n * (n + 1) / 2 → O (1)
Stack using array “FIFO”
Last in first Out (LIFO)
D

→ Print stack[top]
→ Array-Based Implementation of the ADT stack

Stack using pointers “Linked Stack”


- A Pointer-Based Implementation of the ADT Stack (linked stack)
Queue using array “FIFO”

Queue using pointer “Linked Queue”


Linked List
- Array is the opposite of Linked List

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.

Doubly Linked List


Tree Date Structure
- Binary Tree
→ Binary Search Tree
→ AVL tree or height-balanced binary tree

- Binary Space Partition


- GGM Tree
- Syntax Tree
- Huffman Coding Tree
Tree
Type of Tree:
- Binary tree
- B tree
- Heaps
- Trees
- Multiway tree
- Application specific tree
Binary Tree

Full “Strictly” Binary Tree


1- if every node has zero or two children.

Complete Binary Tree


1- All levels are filled except the last level.
2- All nodes as left as possible in last level.

Perfect Binary Tree “All levels in completely filled”


1- Every node has two children
2- All leaves are at the same level
Balanced Binary Tree
1- The height of tree = O (log n)
2- | h (Left sub-Tree) – h (Right sub-Tree) | <= 1

A degenerate “or pathological” Tree


1- Every Parent node has only one child either left or right.
2- Such tree is performance-wise same as linked list.

Max no. of nodes at level → 2L


Max no. of nodes in a binary tree → 2h+1 -1

Binary Tree Traversal


Breadth-first traversal
- Level-order
Depth-first traversal
- Pre-order: root left right +AB
- In-order: left root right A+B
- Post-order: left right root AB+
Binary Search Tree
Find Successor:
1. The node has x a right subtree → minimum number in left subtree.
Successor (50): 55
2. The node x doesn’t have the right subtree, and it is the right child → number
bigger than x
Successor (24): 50
3. The node x doesn’t have the right subtree, and it is the left child → choose parent
Successor (10): 13

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

| | | | 60 20 10 30 90 | 100 | if(60 > 20)? swap | pass 2


| | | 20 | 60 10 30 90 | 100 | if(60 > 10)? swap | pass 2
| | | 20 10 | 60 30 90 | 100 | if(60 > 30)? swap | pass 2
| | | 20 10 30 | 60 90 | 100 | if(60 > 90)? No swap| pass 2
| | | 20 10 30 60 | 90 | 100 | pass 2

| | | 20 10 30 60 | 90 | 100 | if(20 > 10)? swap | pass 3


| | 10 | 20 30 60 | 90 | 100 | if(20 > 30)? No swap| pass 3
| | 10 20 | 30 60 | 90 | 100 | if(30 > 30)? No swap| pass 3
| | 10 20 30 | 60 | 90 | 100 | pass 3

| | 10 20 30 | 60 | 90 | 100 | if(10 > 20)? No swap | pass 4


| 10 | 20 30 | 60 | 90 | 100 | if(20 > 20)? No swap | pass 4
| 10 20 | 30 | 60 | 90 | 100 | pass 4

| 10 20 | 30 | 60 | 90 | 100 | if(10 > 20)? No swap | pass 5


10 | 20 | 30 | 60 | 90 | 100 | pass 5
- Time Complexity:
- Best case: O (n)
- Average & Worst Case: O (n2)
- Space Complexity: O (1)
Insertion Sort
- Simple sorting algorithm.
- In-place algorithm.
- Works the way we sort playing cards in our hands.
- Time Complexity: - Space Complexity: O (1)
- Best-case: O (n)
- Average case: O (n2)
- Worst Case: O (n2)
- We call the “Insertion Sort” when the size of the Array is small.

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)

You might also like