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 400 of 534
Calculating the balance factor in a Javascript AVL Tree
AVL tree checks the height of the left and the right sub-trees and assures that the difference is not more than 1. This difference is called the Balance Factor. For example, in the following trees, the first tree is balanced and the next two trees are not balanced: Balanced (BF ≤ 1) B A C Unbalanced (BF = 2) ...
Read MoreInserting a node in a Javascript AVL Tree
We can learn how to insert a node in an AVL Tree. Insertions in AVL trees follow the same process as BST, but we need to perform one extra step called balance tree during insert whenever we move down the tree. This requires calculating the balance factor and applying appropriate rotation methods based on the tree configuration. The balance factor determines which type of rotation is needed to maintain the AVL property. Understanding AVL Tree Balance An AVL tree maintains balance by ensuring that for every node, the heights of its left and right subtrees differ by ...
Read MoreAVL Tree class in Javascript
An AVL Tree is a self-balancing binary search tree where the heights of left and right subtrees differ by at most one. This implementation provides insertion with automatic rebalancing through rotations. 10 5 15 3 7 Balanced AVL Tree (height difference ...
Read MoreCreating a Graph in Javascript
We'll be creating a graph class that supports weights and both directed and undirected types. This will be implemented using an adjacency list. As we move to more advanced concepts, both weights and directed nature of the graphs will come in handy. An adjacency list is an array A of separate lists. Each element of the array Ai is a list, which contains all the vertices that are adjacent to vertex i. We're defining it using 2 members, nodes and edges. Graph Class Structure Let's set up the graph class by defining our class and some methods ...
Read MoreGraph Traversals in Javascript
Graph traversal (also known as graph search) refers to the process of visiting (checking and/or updating) each vertex in a graph. Such traversals are classified by the order in which the vertices are visited. In JavaScript, the two most common graph traversal algorithms are Breadth-First Search (BFS) and Depth-First Search (DFS). Both algorithms systematically explore graph nodes but use different strategies. Graph Representation First, let's create a simple graph using an adjacency list representation: class Graph { constructor() { this.adjacencyList = {}; ...
Read MoreBreadth-first search traversal in Javascript
BFS visits the neighbor vertices before visiting the child vertices, and a queue is used in the search process. Following is how a BFS works − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue. If no adjacent vertex is found, remove the first vertex from the queue. Repeat Rule 1 and Rule 2 until the queue is empty. How BFS Traversal Works Let us look at an illustration of how BFS Traversal works: ...
Read MoreShortest path algorithms in Javascript
In graph theory, the shortest path problem is finding a path between two vertices in a graph such that the sum of edge weights is minimized. To implement shortest path algorithms, we need to modify our graph structure to support weighted edges. Setting Up Weighted Graphs First, let's modify our graph methods to handle weights: class WeightedGraph { constructor() { this.nodes = []; this.edges = {}; } ...
Read MoreDijkstra's algorithm in Javascript
Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph. We'll use the new addEdge and addDirectedEdge methods to add weights to the edges when creating a graph. Let us look at how this algorithm works − Create a distance collection and set all vertices distances as infinity except the source node. Enqueue the source node in a min-priority queue with priority 0 as the distance is 0. Start a loop till the priority queue is empty and dequeue the ...
Read MorePrim's algorithm in Javascript
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. How Prim's Algorithm Works Let us look at an illustration of how Prim's algorithm works: 1. Choose any arbitrary node ...
Read MoreKruskal's algorithm in Javascript
Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a weighted, undirected graph. It works by systematically selecting the smallest weight edges while avoiding cycles. How Kruskal's Algorithm Works The algorithm follows these steps: Create a set of all edges in the graph Sort all edges by weight in ascending order While the set is not empty and not all vertices are covered: Remove the minimum weight edge from the set Check if this edge forms a cycle. If not, ...
Read More