Javascript Articles

Page 476 of 534

Complete Graph Class in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 286 Views

Functions which have been commented out in this code. You can switch to those as well. We've also moved the Queue, Stack, and PriorityQueue classes in different modules that can be imported using either import statements or using require calls. Here is the complete implementation of the Graph class − Exampleconst Queue = require("./Queue"); const Stack = require("./Stack"); const PriorityQueue = require("./PriorityQueue"); class Graph {    constructor() {       this.edges = {};       this.nodes = [];    }    addNode(node) {       this.nodes.push(node);       this.edges[node] = [];    }   ...

Read More

Calculating the balance factor in a Javascript AVL Tree

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 849 Views

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 −In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the difference is 2. In the third tree, the right subtree of A has height 2 and the left is missing, so it is 0, and the difference is 2 again. AVL tree ...

Read More

AVL Rotations in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 472 Views

To balance itself, an AVL tree may perform the following four kinds of rotations −Left rotationRight rotationLeft-Right rotationRight-Left rotationThe first two rotations are single rotations and the next two rotations are double rotations. To have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's understand them one by one.Left RotationIf a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then we perform a single left rotation −In our example, node A has become unbalanced as a node is inserted in the right subtree of ...

Read More

Inserting a node in a Javascript AVL Tree

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 289 Views

We can learn how we can insert a node in an AVL Tree. Insertions in AVL trees are the same as BST, we just need to perform one extra step called balance tree during insert whenever we move down the tree.This requires calculating the balance factor which we already saw before. And according to the configurations, we need to call appropriate rotation methods. These are pretty intuitive with the help of the above explanation.We again create a class method and a helper function for recursive calls − Exampleinsert(data) {    let node = new this.Node(data);    // Check if the tree ...

Read More

AVL Tree class in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 848 Views

Here is the complete implementation of the AVL Tree Class −Exampleclass AVLTree {    constructor() {       // Initialize a root element to null.       this.root = null;    }    getBalanceFactor(root) {       return this.getHeight(root.left) - this.getHeight(root.right);    }    getHeight(root) {       let height = 0;       if (root === null || typeof root == "undefined") {          height = -1;       } else {          height = Math.max(this.getHeight(root.left), this.getHeight(root.right)) + 1;       }     ...

Read More

Creating a Graph in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 1K+ Views

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.Let's set up the graph class by defining our class and some methods that we'll use to add nodes and edges ...

Read More

Graph Traversals in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 266 Views

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.

Read More

Breadth-first search traversal in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 4K+ Views

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.Let us look at an illustration of how BFS Traversal works:StepTraversalDescription1Initialize the queue.2We start by visiting S (starting node) and mark it as visited.3We then see an unvisited adjacent node from S. In this example, we have three ...

Read More

Node in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 248 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

Creating a BinaryTree using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 312 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.

Read More
Showing 4751–4760 of 5,338 articles
« Prev 1 474 475 476 477 478 534 Next »
Advertisements