Find the Mode in a Binary Search Tree using JavaScript
Last Updated :
26 Apr, 2024
One can find the mode in a binary search tree using JavaScript. The mode in the binary search tree will be the value of the node that occurs with the highest frequency among all the nodes in the tree. If there is more than one mode present in the binary search tree then return all modes present in the array.
There are several approaches to calculate mode in binary search tree using JavaScript which are as follows:
Using Inorder Traversal & Hashmap
Create a function and initialize three variables maxFreq to track the maximum frequency encountered, freqMap to store the frequency of each value encountered and modes array to store the mode(s) found. Perform an inorder traversal of BST and update the frequency map and maxFreq as we traverse. Iterate through the frequency map and If the frequency of a value equals maxFreq, push that value into the modes array. Return the modes array.
Example: To demonstrate Finding the mode in a binary search tree using Inorder Traversal & Hashmap
JavaScript
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function findMode(root) {
if (!root) return [];
let maxFreq = 0;
const freqMap = {};
const modes = [];
function inorder(node) {
if (!node) return;
inorder(node.left);
freqMap[node.val] = (freqMap[node.val] || 0) + 1;
maxFreq = Math
.max(maxFreq, freqMap[node.val]);
inorder(node.right);
}
inorder(root);
for (let key in freqMap) {
if (freqMap[key] === maxFreq) {
modes.push(parseInt(key));
}
}
return modes;
}
const root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(2);
console.log(findMode(root));
Time Complexity: O(n + m)
Space Complexity: O(max(n, m))
Using Morris Traversal
Initializes variables - current pointing to the root, maxCount set to 0 for tracking maximum frequency, and modes as an empty array . It uses prev to track the previous node visited and count to monitor the frequency of the current value. While traversing, updates count and checks mode occurrences for null left child; otherwise, finds inorder predecessor for left child, adjusts pointers, and continues traversal. Returns the modes array after traversal completion.
Example: To demonstrate Finding the mode in a binary search tree using Morris Traversal
JavaScript
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function findModeMorris(root) {
let current = root;
let maxCount = 0;
let modes = [];
let prev = null;
let count = 0;
while (current !== null) {
if (current.left === null) {
// Process current node
if (prev !== null
&&
prev.value === current.value) {
count++;
} else {
count = 1;
}
if (count > maxCount) {
maxCount = count;
modes = [current.value];
} else if (count === maxCount) {
modes.push(current.value);
}
prev = current;
current = current.right;
} else {
let predecessor = current.left;
while (
predecessor.right !== null
&&
predecessor.right !== current) {
predecessor = predecessor.right;
}
if (predecessor.right === null) {
predecessor.right = current;
current = current.left;
} else {
predecessor.right = null;
// Process current node
if (prev !== null && prev.value === current.value) {
count++;
} else {
count = 1;
}
if (count > maxCount) {
maxCount = count;
modes = [current.value];
} else if (count === maxCount) {
modes.push(current.value);
}
prev = current;
current = current.right;
}
}
}
return modes;
}
const root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(2);
console.log(findModeMorris(root));
Time Complexity : O(n)
Space Complexity : O(1)
Similar Reads
Insert a Node in Binary tree using JavaScript A binary tree is a tree data structure where each node has at most two children, referred to as the left child and the right child. The topmost node is called the root. Nodes can have values or data, and the left child's value is less than the parent's value, while the right child's value is greater
3 min read
LCA in Binary Tree using JavaScript The lowest common ancestor between two nodes n1 and n2 in a binary tree is defined as the lowest node in a Tree that has both n1 and n2 as descendants. If either n1 or n2 is not present in the tree, or if they are not connected through a common ancestor, the LCA of the binary tree is NULL. Below is
4 min read
Find the Subtree with the Maximum Average in a Binary Tree using JavaScript Given a binary tree, our task is to find the subtree with the maximum average in JavaScript. The average of a subtree is defined as the sum of all node values within the subtree divided by the number of nodes it contains. Example: Input: 5 / \ 6 7 / \ 3 4Output: 7ExplanationAverage of values of subt
8 min read
Search a Given Key in BST using JavaScript Given a binary search tree and a key, determine if the key is present in the tree. If the key is present, return the node containing the key otherwise, return null. Example: Input : {15,20,10,8,12,16,25}, Key : 20Output : 20 found 15 / \ 10 20 / \ / \ 8 12 16 25Table of Content Recursive ApproachIte
3 min read
Kth Largest/Smallest Element in Binary Search Tree using JavaScript A binary search tree is a type of binary tree in which each node has two children: left child & right child. In a Binary search tree (BST) all nodes in the left subtree have values less than the node value and all nodes in the right subtree have values greater than the node value. Different appr
4 min read
Kth Ancestor in Binary Tree using JavaScript Given a binary tree and a node in this tree, our task is to find the k-th ancestor of the specified node in JavaScript. If no such ancestor exists, or when k exceeds the depth of the node, the function should return null. Example: Input: Node = 5, k = 2Output: 1ApproachFirst, calculate the depth of
2 min read