JavaScript Program to Find Maximum Width of Binary Tree
Last Updated :
27 Feb, 2024
The maximum width of a given binary tree can be calculated by finding the maximum of all the level widths. The maximum width of a Binary tree is the maximum number of nodes present at any level. The maximum width of the Binary tree will include only the present node and not include null nodes.
The maximum width of a Binary Tree
In this example, we are going to see what is the maximum width of a binary tree. The maximum width of a binary tree is the maximum number of nodes present at any level. The maximum number of nodes present is returned as the maximum width of a binary tree.
Maximum Width- The maximum width at level 0 is 1.
- The maximum width at level 1 is 2.
- The maximum width at level 2 is 3.
- The maximum width at level 3 is 3.
As the maximum width of the binary tree is 3 at level 2 and level 3. So the maximum width of a binary tree is 3.
There are several ways to find the maximum width of a Binary tree in JavaScript which are as follows:
Level order Traversal using a queue
The level order traversal of the binary tree can be used to find the maximum width of the binary tree using the level order traversal of a binary tree. Perform the level order traversal using a queue. We calculate the maximum width of a binary tree. Keep the count of nodes at each level stored in the queue which will return the maximum width of a binary tree. Return the maximum width of a binary tree.
Example: In this queue is used to traverse each level of the binary tree using the queue data structure. At each level, we update the maximum width of a binary tree. Lastly, we return the maximum width of the binary tree stored in the variable after completing the whole traversal of a binary tree.
JavaScript
class Node {
constructor(key) {
this.data = key;
this.left = null;
this.right = null;
}
}
function maxWidth(root) {
if (root === null) {
return 0;
}
let maxWidth = 0;
let queue = [];
queue.push(root);
while (queue.length > 0) {
let count = queue.length;
maxWidth = Math.max(maxWidth, count);
while (count > 0) {
let node = queue.shift();
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
count--;
}
}
return maxWidth;
}
// Example usage:
// Constructing a sample binary tree
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(8);
root.right.right.left = new Node(6);
root.right.right.right = new Node(7);
console.log("Maximum width of the binary tree:", maxWidth(root));
OutputMaximum width of the binary tree: 3
Using Breadth-First Search
This is a simple approach to calculate the maximum width of a binary tree. We traverse the tree using the breadth-first search technique. We store the maximum width at each level and update it if a greater width is found than previously stored. This way the maximum width of the binary tree is returned. Declare a maximum variable that will store the maximum width of the binary tree and update the value of a maximum variable as traversing each level of the binary tree is done using the BFS technique and in the last return the maximum width of the Binary tree is stored in a maximum variable.
Example: In the given below example BFS at each level is performed and the maximum width of the binary tree is updated at each level. After the traversal of the binary tree is completed the maximum width of the binary tree is returned.
JavaScript
class Node {
constructor(key) {
this.data = key;
this.left = null;
this.right = null;
}
}
function maxWidth(root) {
if (root === null) return 0;
let max = 0;
let levelNodes = [root];
while (levelNodes.length) {
let count = levelNodes.length;
max = Math.max(max, count);
let nextLevelNodes = [];
for (let i = 0; i < count; i++) {
let node = levelNodes[i];
if (node.left) {
nextLevelNodes.push(node.left);
}
if (node.right) {
nextLevelNodes.push(node.right);
}
}
levelNodes = nextLevelNodes;
}
return max;
}
// Constructing a sample binary tree
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(8);
root.right.right.left = new Node(6);
root.right.right.right = new Node(7);
console.log("Maximum width of the binary tree:", maxWidth(root));
OutputMaximum width of the binary tree: 3
Similar Reads
Maximum Depth or Height Of a Binary Tree with python Binary trees are hierarchical data structures that have widespread applications in computer science, from databases to graphics. One essential property of a binary tree is its depth or height. In this article, we'll discuss how to compute the maximum depth (or height) of a binary tree using Python.
5 min read
Find the maximum path sum between two leaves of a binary tree using JavaScript Given the root of a binary tree, return the maximum path sum of any non-empty path. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to p
6 min read
JavaScript Program for Rightmost and Leftmost Node of a Binary Tree A binary tree is a fundamental data structure consisting of nodes, each with at most two children: a left child and a right child. The topmost node is called the root. Binary trees support operations such as insertion, deletion, and various traversal methods like in-order, pre-order, and post-order
3 min read
Java Program for the Preorder Tree Traversal in Binary Tree Preorder traversal is the method used to traverse the tree data structure. In the preorder traversal, the nodes are visited in the order: of root, left subtree, and right subtree. It is called as "preorder" because the root node is visited before its children. This traversal technique is widely used
2 min read
Find the maximum element of every subtree of a Binary Tree Given a Binary Tree, find the maximum element of every subtree of it. Examples :Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : [4, 5, 5, 7, 6, 7, 7] Explanation: The maximum element of the subtree rooted at node 4 is 4. The maximum element of the subtree rooted at node 2 is 5. The maximum element of the
15+ min read
Java Program to Construct a Binary Search Tree Binary Search Tree (BST) is the widely used data structure in computer science, primarily known for the efficient search, insertion, and deletion operations. It is the type of binary tree where each node has at most two children, referred to as the left child and the right child. Binary Search Tree
6 min read
AVL Tree program in Java AVL tree stands for Adelson-Velsky and Landis tree. An AVL tree is a type of self-balancing binary search tree. In an AVL tree, the height of two child subtrees of any of the nodes differs by no more than one, ensuring that the tree remains balanced. This property helps in maintaining the tree's hei
5 min read
Java Program to Perform the Postorder Tree Traversal The Binary Tree can consist of nodes where each node contains the value and the references to its left and right children. The structure is organized hierarchically with a single root node at the top and each node having at Most Two Children. In this article, we will learn to perform Postorder Tree
3 min read
What is Full Binary Tree? A full binary tree is a type of binary tree in which every node has either zero or two children. Full Binary Tree exampleProperties of Full Binary Tree: Let i be the number of internal nodes, n be the total number of nodes, l be the number of leaf nodes and h be the total number of levels The number
2 min read
Implementing a Binary Tree in Java A binary tree is a hierarchical data structure composed of the nodes. Each node contains the value and references to its left child node and right child node, which are also binary trees that are possibly null. The structure resembles the tree with the nodes branching out from a central root, where
5 min read