View on GitHub

Notes

reference notes

Stack

A stack is a data structure that consists of Nodes. Each Node references the next Node in the stack, but does not reference its previous.

img

Common terminology for a stack is

Stacks follow these concepts:

Push O(1)

Pushing a Node onto a stack will always be an O(1) operation. This is because it takes the same amount of time no matter how many Nodes (n) you have in the stack.

When adding a Node, you push it into the stack by assigning it as the new top, with its next property equal to the original top.

Pop O(1)

Popping a Node off a stack is the action of removing a Node from the top. When conducting a pop, the top Node will be re-assigned to the Node that lives below and the top Node is returned to the user.

Typically, you would check isEmpty before conducting a pop. If isEmpty returns true, an exception is raised because you are attempting to pop an empty stack.

Peek O(1)

When conducting a peek, you will only be inspecting the top Node of the stack.

Typically, you would check isEmpty before conducting a peek. If isEmpty returns true, an exception is raised because you are attempting to peek an empty stack.

IsEmpty O(1)

Here is the pseudocode for isEmpty

ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean

return top = NULL

Stack Implementation

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor() {
    this.top = null;
  }

  push(value) {
    const node = new Node(value);
    node.next = this.top;
    this.top = node;
  }

  pop() {
    if (!this.top) {
      throw new Error('Stack is empty');
    }
    const temp = this.top;
    this.top = this.top.next;
    temp.next = null;
    return temp.value;
  }

  peek() {
    if (!this.top) {
      throw new Error('Stack is empty');
    }
    return this.top.value;
  }

  isEmpty() {
    return !this.top;
  }
}

Queue

A queue is a data structure that consists of Nodes. Each Node references the next Node in the queue, but does not reference its previous.

img

Common terminology for a queue is

Queues follow these concepts:

Enqueue O(1)

When you add an item to a queue, you use the enqueue action. This is done by assigning the rear to the new Node and referencing the previous rear to the new rear.

Dequeue O(1)

When you remove an item from a queue, you use the dequeue action. This is done by re-assigning the front to the next Node in the queue.

Typically, you would check isEmpty before conducting a dequeue. If isEmpty returns true, an exception is raised because you are attempting to dequeue an empty queue.

Peek O(1)

When conducting a peek, you will only be inspecting the front Node of the queue.

Typically, you would check isEmpty before conducting a peek. If isEmpty returns true, an exception is raised because you are attempting to peek an empty queue.

IsEmpty O(1)

Here is the pseudocode for isEmpty

ALGORITHM isEmpty()
// INPUT <-- none
// OUTPUT <-- boolean

return front = NULL

Queue Implementation

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
  }

  enqueue(value) {
    const node = new Node(value);
    if (!this.front) {
      this.front = node;
      this.rear = node;
      return;
    }
    this.rear.next = node;
    this.rear = node;
  }

  dequeue() {
    if (!this.front) {
      throw new Error('Queue is empty');
    }
    const temp = this.front;
    this.front = this.front.next;
    temp.next = null;
    return temp.value;
  }

  peek() {
    if (!this.front) {
      throw new Error('Queue is empty');
    }
    return this.front.value;
  }

  isEmpty() {
    return !this.front;
  }
}

Trees

A tree is a data structure that consists of nodes in a parent/child relationship.

img

Common Terminology

Traversals