Activity 10: Data Structure in Typescript
Arrays
In TypeScript, arrays offer a strong and adaptable means of storing, managing, and gaining access to data collections. TypeScript arrays provide robust type-checking for additional security and ease of collection management with built-in methods for adding, removing, and iterating over elements.
Tuple
Tuples are a specific kind of array in which both the total number of entries and their kinds are predetermined and clearly stated.
Differences Between Tuples and Arrays
Multiple Types: Each element in a tuple can be of a different type, whereas arrays usually hold elements of the same type (e.g.,
number[]
for an array of numbers).Fixed Length: The number of elements in a tuple is fixed, whereas arrays can have any number of elements.
Array List (Dynamic Arrays):
You can simulate "dynamic array" behavior by continuously adding or removing elements, and the array will resize accordingly. You can also use various methods to manipulate these arrays efficiently.
Key Concepts for Dynamic Arrays in TypeScript:
Resizing happens automatically: When you add or remove elements from an array, the array dynamically resizes in the background.
Type enforcement: TypeScript enforces that the array elements adhere to the specified type.
Manipulation: You can manipulate arrays dynamically using built-in array methods like
push()
,pop()
,splice()
, etc.
Example of Creating and Managing Dynamic Arrays
Stack:
A stack is a LIFO data structure that allows you to push, pop, and peek elements, with constant time complexity for these operations.
Implementing a Stack in TypeScript
Queue:
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This indicates that the first element put to the queue is the first one to be removed. It's similar to waiting in a physical line, where the person who arrives first is connected first.
Queue Implementation Using an Array
LinkedList:
Linked lists are a versatile data structure with singly and doubly linked variants. Singly linked lists allow traversal in one direction, while doubly linked lists support traversal in both directions. Implementing linked lists in TypeScript involves defining node classes and managing pointers to add, remove, and traverse nodes efficiently.
Singly Linked List
Doubly Linked List
HashMap (or Object/Map):
The Map object in TypeScript provides a straightforward and powerful way to create a key-value pair data structure. Unlike objects, Map allows for any type of key (e.g., objects, functions) and maintains the order of insertion.
Using an Object
Set:
Set is a collection of unique elements, meaning that each element can only appear once in the set. Sets are useful for storing distinct values and performing operations like checking for membership, adding, and removing elements.
Tree:
A binary tree is a tree data structure where each node has at most two children, often referred to as the left and right child.
Define the Node Class
Define the Binary Search Tree Class
for further reading and understanding, you may visit these sites:
Handbook - Basic Types. (n.d.). https://www.typescriptlang.org/docs/handbook/basic-types.html
Irovich, G. (2021, December 13). Typescript Data structures: stack and queue. DEV Community. https://dev.to/glebirovich/typescript-data-structures-stack-and-queue-hld
Maglione, & Maglione. (2024, January 10). Article: “How to implement a data structure in Typescript.” Sandro Maglione. https://www.sandromaglione.com/articles/how-to-implement-a-data-structure-in-typescript
Wikipedia contributors. (2024, September 9). TypeScript. Wikipedia. https://en.wikipedia.org/wiki/TypeScript