Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Front End Technology Articles - Page 597 of 860
533 Views
In this article, we will explain how to find the minimum and maximum values in a binary search tree (BST), with implementation in JavaScript. A binary search tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. So the leftmost node will have the minimum value, and the rightmost node will have the maximum value. Find Minimum and Maximum in a BST Given root node of a binary search tree, ... Read More
545 Views
We're going to use the property of a BST to look up elements in it. Let us look at first an iterative implementation of search − ExamplesearchIter(data) { let currNode = this.root; while (currNode !== null) { if (currNode.data === data) { // Found the element! return true; } else if (data < currNode.data) { // Go Left as data is smaller than parent currNode = currNode.left; } else { ... Read More
719 Views
The first insertion in a newly created binary tree creates a node at the root. Further insertions will be inserted according to the binary search tree property of left children being smaller than parent and right ones being greater than parents.Let us look at how we can implement this algorithm in code −ExampleinsertIter(data) { let node = new this.Node(data); // Check if the tree is empty if (this.root === null) { // Insert as the first element this.root = node; return; } let currNode = this.root; ... Read More
291 Views
Let us understand how we're going to create and represent a binary search tree in Javascript. We'll first need to create the class BinarySearchTree and define a property Node on it. Exampleclass BinarySearchTree { constructor() { // Initialize a root element to null. this.root = null; } } BinarySearchTree.prototype.Node = class { constructor(data, left = null, right = null) { this.data = data; this.left = left; this.right = right; } };We've simply created a class representation of our BST class. We'll fill this class in as we proceed to learn functions that we'll add to this structure.
223 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
453 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
271 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
583 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).
176 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
612 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