Data Structures & Algorithms Notes

Arrays

Storing data in a linear collection...

int[] arr = {1, 2, 3, 4};

Linked List

Data structures where elements are linked using pointers...

Sorting Algorithms

Bubble Sort, Merge Sort, Quick Sort...

Stacks

A stack is a linear data structure that follows Last In First Out (LIFO) principle...


        class Stack {
          constructor() {
            this.stack = [];
          }
          push(value) {
            this.stack.push(value);
          }
          pop() {
            return this.stack.pop();
          }
        }

Queues

A queue is a linear data structure that follows First In First Out (FIFO) principle...


        class Queue {
          constructor() {
            this.queue = [];
          }
          enqueue(value) {
            this.queue.push(value);
          }
          dequeue() {
            return this.queue.shift();
          }
        }

Binary Search Tree

A binary tree where each node has at most two children, and the left child is less than the parent node...


        class Node {
          constructor(value) {
            this.value = value;
            this.left = null;
            this.right = null;
          }
        }
        function insert(root, value) {
          if (!root) {
            return new Node(value);
          }
          if (value < root.value) {
            root.left = insert(root.left, value);
          } else {
            root.right = insert(root.right, value);
          }
          return root;
        }
Can’t find what you're looking for? Request a topic