Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 399 of 534
Search Element in an Javascript Hash Table
JavaScript hash tables use a get method to search for elements by their key. The method calculates the hash code and searches through the chain at that index to find the matching key-value pair. The get() Method Implementation The search operation involves computing the hash code for the given key and then iterating through the chain at that bucket to find the exact match: get(key) { let hashCode = hash(key); for(let i = 0; i < this.container[hashCode].length; i ++) { ...
Read MoreRemove elements from Javascript Hash Table
To remove elements from a JavaScript hash table, we need to locate the element using its key and remove it from the underlying storage structure. In hash tables that use chaining for collision resolution, this involves searching through the chain at the computed hash index. Let us look at the implementation of the remove method: Remove Method Implementation remove(key) { let hashCode = this.hash(key); for (let i = 0; i < this.container[hashCode].length; i++) { // Find the element in ...
Read MoreJoining two hash tables in Javascript
Sometimes we need to combine hash tables together using a join function to create a new merged hash table. We'll write a static join method that takes 2 HashTables and creates a new HashTable with all the values. For simplicity, values from the second hash table will override values from the first one if there are duplicate keys. Syntax static join(table1, table2) { // Implementation logic return newHashTable; } Implementation Here's the complete implementation of the join method: class HashTable { ...
Read MoreThe HashTable Class in Javascript
A HashTable (Hash Map) is a data structure that stores key-value pairs using a hash function to compute array indices. This implementation uses chaining to handle collisions, where multiple values can be stored at the same index using arrays. Complete HashTable Implementation class 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 = ...
Read MoreTree Data Structure in Javascript
The tree data structure represents hierarchical relationships like organization charts, file systems, and DOM elements. A tree consists of nodes connected in a parent-child relationship, where each node has a value and references to its children, with no duplicate references. Tree Terminology Understanding key tree terms is essential: Root: The top node with no parent Parent: A node that has children Child: A node connected to a parent above it Leaf: A node with no children Height: Maximum depth from root to any leaf Depth: Number of edges from root to a specific node ...
Read MoreNode in Javascript
In JavaScript tree data structures, a node is the fundamental building block that contains data and references to other connected nodes. For binary trees, each node has three essential properties: the data it stores and references to its left and right child nodes. Node Structure A typical tree node contains the following components: data − Stores the actual value or information in the node left − Reference to the left child node right − Reference to the right child node Creating a Node Class ...
Read MoreCreating a BinaryTree using Javascript
A Binary Search Tree (BST) is a hierarchical data structure where each node has at most two children. Let's learn how to create and represent a binary search tree in JavaScript by building a complete BST class with essential operations. Basic Structure We'll start by creating the BinarySearchTree class and defining a Node class for individual tree elements. class BinarySearchTree { constructor() { // Initialize a root element to null this.root = null; ...
Read MoreInserting a key into a tree in Javascript
In a Binary Search Tree (BST), inserting a new key follows a simple rule: smaller values go to the left subtree, and larger values go to the right subtree. The first insertion creates the root node, while subsequent insertions traverse the tree to find the correct position. How BST Insertion Works The insertion process starts at the root and compares the new value with each node. If the value is smaller, we move left; if larger, we move right. When we reach a null position (leaf node), we insert the new node there. Binary ...
Read MoreSearching for values in an Javascript Binary Search Tree
We're going to use the property of a BST to look up elements in it. In a Binary Search Tree, values smaller than the current node are in the left subtree, and values larger are in the right subtree, making search operations efficient. Iterative Search Implementation The iterative approach uses a loop to traverse the tree: searchIter(data) { let currNode = this.root; while (currNode !== null) { if (currNode.data === data) { ...
Read MoreIn-order traversal in Javascript Tree
The tree is a data structure that is comprised of nodes and edges. These entities are interconnected to each other to form a tree structure. Traversing a data structure means visiting each and every node in that structure and bringing out a sequence of values from that. There are three types of traversals namely, in-order (L→ Root→R ), pre-order(Root→L→R) and, post-order(L→ R → Root) In-order Traversal In the In-order Traversal, the left subtree is visited first, followed by the Root node and finally the right subtree. A binary tree will provide sorted key values in ascending order ...
Read More