Activity 10: Data Structure in Typescript
Arrays Definition:
An array is a collection of elements stored in a contiguous memory location. It is used to store multiple values in a single variable.
Key Features:
Ordered collection
Indexed access to elements
Use Cases:
- Arrays are used when you need to store and manipulate lists of items, such as numbers, strings, or objects.
Time Complexity:
Access: O(1)
Insertion (at the end): O(1)
Deletion (from the end): O(1)
Insertion/Deletion (middle): O(n)
Example Code:
let numbers: number[] = [1, 2, 3]; numbers.push(4); // Adding an element numbers.splice(1, 1); // Removing an element console.log(numbers[0]); // Accessing an element
Tuples Definition:
Tuples are arrays with a fixed number of elements of specific types.
Key Features:
Fixed length
Can hold multiple data types
Use Cases:
- Used when you need to group different data types.
Example Code:
let user: [number, string] = [1, "John"]; console.log(user[0]); // Accessing elements
ArrayList (Dynamic Arrays) Definition:
Dynamic arrays are arrays that can resize automatically when the capacity is exceeded.
Key Features:
Grows automatically
Provides array-like access
Use Cases:
- Used when the number of elements is not fixed.
Example Code:
let list: number[] = [1, 2, 3]; list.push(4); // Resizing automatically console.log(list);
Stack Definition:
A stack is a linear data structure that follows the LIFO (Last In First Out) principle.
Key Features:
- Operations: push (add), pop (remove), peek (view top)
Use Cases:
- Used in undo mechanisms, backtracking algorithms, etc.
Time Complexity:
Push/Pop: O(1)
Search: O(n)
Example Code:
class Stack { private items: T[] = []; push(item: T) { this.items.push(item); } pop() { return this.items.pop(); } }
const stack = new Stack(); stack.push(10); console.log(stack.pop());
Queue Definition:
A queue is a linear structure that follows the FIFO (First In First Out) principle
Key Features:
- Operations: enqueue (add), dequeue (remove)
Use Cases:
- Used in task scheduling, buffering.
Time Complexity:
Enqueue/Dequeue: O(1)
Example Code:
class Queue { private items: T[] = []; enqueue(item: T) { this.items.push(item); } dequeue() { return this.items.shift(); } }
const queue = new Queue(); queue.enqueue(10); console.log(queue.dequeue());
LinkedList Definition:
A linked list is a sequence of nodes where each node points to the next.
Key Features:
Dynamic size
Fast insertion/deletion
Use Cases:
- Used in real-time applications requiring quick updates.
Time Complexity:
Access: O(n)
Insertion/Deletion: O(1)
Example Code:
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } }
class LinkedList { head: Node | null = null; add(value: number) { /* Add logic here */ } }
7. HashMap (Map/Object) Definition:
A key-value pair data structure used for fast data retrieval.
Key Features:
No duplicate keys
Fast access
Use Cases:
Used for database indexing, caching.
Time Complexity:
Access: O(1)
Insertion/Deletion: O(1)
Example Code:
let map = new Map<string, number>(); map.set("one", 1); console.log(map.get("one"));
Set Definition:
A Set is a collection of unique elements.
Key Features:
No duplicates
Fast access
Use Cases:
- Used for filtering duplicates.
Example Code:
let set = new Set(); set.add(1); set.delete(1); console.log(set.has(1));
Tree (Binary Search Tree) Definition:
A tree structure where each node has at most two children, used for sorted data.
Key Features:
Hierarchical structure.
Fast search, insert, delete.
Use Cases:
- Used in searching, sorting algorithms.
Example Code:
class TreeNode { value: number; left: TreeNode | null = null; right: TreeNode | null = null; constructor(value: number) { this.value = value; } }
References
TypeScript for JavaScript Programmers:
https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
Data Structures in TypeScript: https://medium.com/@ilimalbayrak/mastering-typescript-exploring-data-structures-and-algorithms-part-i-47d58e6195ba
GitHub Repository: https://github.com/microsoft/TypeScript/