Found 10483 Articles for Web Development

Node in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:31:41

217 Views

Each element in a tree is a node. We need to define a node before we proceed to define a binary tree as a tree consists of nodes. We'll create a very simple node definition that has 3 properties, namely: left, right and data.left − This holds the reference to the left child of this node.right − This holds the reference to the right child of this node.data − This holds the reference to the data we want to store in this node.Let us see the code representation of such a structure.Examleclass Node {    constructor(data, left = null, right ... Read More

Binary Search Tree in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 10:54:03

437 Views

A Binary Search tree exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value.We'll mostly focus on such trees in this section on trees.Operations on Binary Search TreesWe'll define the following operations on the Binary Search Tree −Inserting a key into a treeIn-order traversal in a treePre-order traversal in a treePost-order traversal in a treeSearching for values in a treeSearching for minimum value in a treeSearching for maximum value in a treeRemoving a leaf node in a treeRead More

Binary Tree in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 10:54:43

262 Views

Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in the linked list.Here is an illustration of a binary tree with some terms that we've discussed below −Important TermsFollowing are the important terms with respect to the tree.Path − Path refers to the sequence of nodes ... Read More

Tree Data Structure in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 10:55:39

559 Views

The tree represents hierarchical structures like organization hierarchy charts, file systems, etc. To put it more formally, a tree can be defined recursively (locally) as a collection of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a list of references to nodes (the "children"), with the constraints that no reference is duplicated (i.e., each child has exactly one parent).

The HashTable Class in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 10:58:43

167 Views

Here is the complete implementation of the HashTable class. This could of course be improved by using more efficient data structures and collision resolution algorithms.Exampleclass HashTable {    constructor() {       this.container = [];       // Populate the container with empty arrays       // which can be used to add more elements in       // cases of collisions       for (let i = 0; i < 11; i++) {          this.container.push([]);       }    }    display() {       this.container.forEach((value, index) => ... Read More

Joining two hash tables in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:00:35

573 Views

Sometimes we need to combine containers together using a join function and get a new container. We'll write a static join method that takes in 2 HashTables and creates a new HashTable with all the values. For the sake of simplicity, we'll let the values from the second one override the values for the first one if there are any keys present in both of them. Examplestatic join(table1, table2) {    // Check if both args are HashTables    if(!table1 instanceof HashTable || !table2 instanceof HashTable) {       throw new Error("Illegal Arguments")    }    let combo = ... Read More

Loop through a hash table using Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:01:51

2K+ Views

Now let us create a forEach function that'll allow us to loop over all key-value pairs and call a callback on those values. For this, we just need to loop over each chain in the container and call the callback on the key and value pairs.ExampleforEach(callback) {    // For each chain    this.container.forEach(elem => {       // For each element in each chain call callback on KV pair       elem.forEach(({ key, value }) => callback(key, value));    }); }You can test this using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ... Read More

Remove elements from Javascript Hash Table

karthikeya Boyini
Updated on 15-Jun-2020 11:04:30

1K+ Views

To remove elements, we simply need to find them and remove them using a simple splice function call that removes elements in place from an array.Let us look at the implementation of the same − Exampleremove(key) {    let hashCode = this.hash(key);    for (let i = 0; i < this.container[hashCode].length; i++) {       // Find the element in the chain       if (this.container[hashCode][i].key === key) {          this.container[hashCode].splice(i, 1);          return true       }    }    return false; }You can test this using − Examplelet ht = ... Read More

Search Element in an Javascript Hash Table

Sai Subramanyam
Updated on 15-Jun-2020 11:06:23

401 Views

We've kind of already implemented this in our put method. Let us look at it again in isolation.Exampleget(key) {    let hashCode = hash(key);    for(let i = 0; i < this.container[hashCode].length; i ++) {       // Find the element in the chain       if(this.container[hashCode][i].key === key) {          return this.container[hashCode][i];       }    }    return undefined; }You can test it using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ht.put(15, 21); ht.put(32, 34); console.log(ht.get(20)); console.log(ht.get(21)); console.log(ht.get(55)); console.log(ht.get(32));OutputThis will give the output.{ key: 20, ... Read More

Add elements to a hash table using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:08:45

994 Views

When adding elements to a hash table the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolutionNow let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object − Exampleput(key, value) {    let hashCode = hash(key);    for(let i = 0; i < this.container[hashCode].length; i ++) {       // Replace the existing value with the given key       ... Read More

Advertisements