Found 8862 Articles for Front End Technology

Creating a Graph in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:36:09

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 Data Structure in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:46:10

1K+ Views

A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges.Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of vertices. Take a look at the following graph −In the above graph, V = {a, b, c, d, e} E = {ab, ac, bd, cd, de}TerminologyMathematical graphs can be represented in the data ... Read More

AVL Tree class in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:50:25

598 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

Inserting a node in a Javascript AVL Tree

karthikeya Boyini
Updated on 15-Jun-2020 11:51:43

165 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 Rotations in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:52:10

242 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

Calculating the balance factor in a Javascript AVL Tree

karthikeya Boyini
Updated on 15-Jun-2020 11:53:40

670 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 tree in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:53:56

217 Views

An AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. A self-balancing tree is a tree that performs some rotation within it's subtrees so that it can be balanced on both left and right side.These trees are particularly helpful in cases where insertions cause a tree to become heavy on one side. Balanced trees keep the lookup times close to O(log(n)) as opposed to an completely unbalanced tree that leans more towards the O(n) side.

Binary Search Tree Class in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:59:47

234 Views

Here is the complete implementation of the BinarySearchTree Class −Exampleclass BinarySearchTree {    constructor() {       // Initialize a root element to null.       this.root = null;    }    insertIter(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;    while (true) {    if (data < currNode.data) {       // Set ... Read More

Removing a node in a Javascript Tree

Nikhilesh Aleti
Updated on 18-Nov-2022 07:04:25

2K+ Views

Tree is a nonlinear data structure which has nodes and edges; where edges act as link between two nodes and nodes hold values in it. Traversal is a procedure where it will retrieve the data in every node of the tree and print them in a specified sequential order. There are three types of tree traversals which are mentioned below − In-order traversal − In-order technique will follow the path (Left → Root → Right). Pre-order traversal − Pre-order technique will follow the path (Root → Left → Right). Post-order traversal − Post-order technique will follow the path (Left ... Read More

Pre-order traversal in a Javascript Tree

Nikhilesh Aleti
Updated on 18-Nov-2022 07:47:20

2K+ Views

Tree is a hierarchical data structure which includes nodes and edges to it. Edges in tree acts as links connecting two nodes. The Preorder tree traversal is a technique where the root node will be traversed first and then it will traverse the left subtree followed by the right subtree. It will represent as ROOT → LEFT → RIGHT Algorithm These are the following steps to perform preorder tree traversal − Traverse the root node. Then, traverse the left subtree. Then, traverse the right subtree. To understand the preorder traversal better, consider the following binary search tree ... Read More

Advertisements