Implementing Data Structures from Four Semesters of Computer Science in 5 Hours by Brian Holt
Posted on August 26, 2020 in Algorithms, JavaScript by Matt Jennings
Array List (in Java, NOT JavaScript)
The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
Deleting in an Array List is expensive, but getting is extremely quick.
Example code:
/* ArrayList We are going to approximate an implementation of ArrayList. In JavaScript terms, that means we are going to implement an array using objects. You should not use arrays at all in this exercise, just objects. Make a class (or constructor function; something you can call new on) called ArrayList. ArrayList should have the following properties (in addition to whatever properties you create): length - integer - How many elements in the array push - function - accepts a value and adds to the end of the list pop - function - removes the last value in the list and returns it get - function - accepts an index and returns the value at that position delete - function - accepts an index, removes value from list, collapses, and returns removed value As always, you can change describe to xdescribe to prevent the unit tests from running while you work */ class ArrayList { constructor() { this.length = 0; this.data = {}; } push(value) { this.data[this.length] = value; this.length++; } pop() { const ans = this.data[this.length-1]; delete this.data[this.length-1]; this.length--; return ans; } get(index) { return this.data[index]; } delete(index) { const ans = this.data[index]; this._collapseTo(index); return ans; } _collapseTo(index) { for (let i = index; i < this.length; i++) { this.data[i] = this.data[i+1]; } delete this.data[this.length-1]; this.length--; } serialize() { return this.data; } }
Linked List
A Linked List (or Singly Linked List) is made of a bunch of nodes that point to the next one in the list. Every node in a LinkedLists has two properties, the value of whatever is being store and a pointer to the next node in the list.
Code example:
class LinkedList { constructor() { this.tail = this.head = null; this.length = 0; } push(value) { const node = new Node(value); this.length++; if (!this.head) { this.head = node; } else { this.tail.next = node; } this.tail = node; } pop() { if (!this.head) return null; if (this.head === this.tail) { const node = this.head; this.head = this.tail = null; return node.value; } const penultimate = this._find(null, (value, nodeValue, i, current) => current.next === this.tail ); const ans = penultimate.next.value; penultimate.next = null; this.tail = penultimate; this.length--; return ans; } _find(value, test=this.test) { let current = this.head; let i = 0; while(current) { if (test(value, current.value, i, current)) { return current; } current = current.next; i++; } return null; } get(index) { const node = this._find(index, this.testIndex); if (!node) return null; return node.value; } delete(index) { if (index === 0) { const head = this.head; if (head) { this.head = head.next; } else { this.head = null; } this.length--; return head.value; } const node = this._find(index-1, this.testIndex); const excise = node.next; if (!excise) return null; node.next = excise.next; if (!node.next.next) this.tail = node.next; this.length--; return excise.value; } test(search, nodeValue) { return search === nodeValue; } testIndex(search, __, i) { return search === i; } serialize() { const ans = []; let current = this.head; if (!current) return ans; while (current) { ans.push(current.value); current = current.next; } return ans; } } class Node { constructor(value) { this.value = value; this.next = null; } }
Binary Search Tree
The gist of the Binary Search Tree (BST) is that a node in a BST has zero, one, or two subtrees. Every element in the left subtree is lesser than the value of the node and every node in the right is greater. By being able to depend on this fact, it makes it very simple to add and find new elements. Like LinkedLists, we just have to change pointers when we add new elements.
The advantages are its fast inserting and deleting when the tree is balanced. Searching can take a long time and the shape of the tree can become unbalanced.
Code example:
class Tree { constructor() { this.root = null; } toObject() { return this.root; } add(value) { // no head, create new head if(this.root === null) { this.root = new Node(value); return; } let current = this.root; while(true) { if(current.value > value) { // go left if(current.left) { current = current.left; } else { current.left = new Node(value); return; } } else { // go right if(current.right) { current = current.right; } else { current.right = new Node(value); return; } } } } } class Node { constructor(value, left = null, right = null) { this.value = value; this.left = left; this.right = right; } }