Convert a Binary Tree to a Binary Search Tree using JavaScript Last Updated : 01 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given a Binary Tree, the task is to convert it to a Binary Search Tree. The conversion must be done in such a way that it keeps the original structure of the Binary Tree. Example: To demonstrate the conversion of the Binary Tree to the Binary Search Tree.Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 / \ 2 7ApproachIn this approach we are using Inorder Traversal and Sorting. To convert a binary tree to a BST, define a Node class and implement helper functions: newNode for node creation, printInorder for traversal, storeInorder to store inorder traversal in an array, and arrayToBST to update the tree with sorted values. In binaryTreeToBST , count the nodes, store and sort the inorder traversal, and update the tree. Finally, create a sample tree, convert it, and print its inorder traversal.Example: The example below illustrates the Conversion of a binary tree to a binary search tree using JavaScript. JavaScript class Node { constructor(data) { this.left = null; this.right = null; this.data = data; } } let index = 0; function newNode(data) { let temp = new Node(data); return temp; } function printInorder(node) { if (node == null) return; printInorder(node.left); console.log(node.data + " "); printInorder(node.right); } function arrayToBST(arr, root) { // Base Case if (root == null) return; arrayToBST(arr, root.left); root.data = arr[index]; index++; arrayToBST(arr, root.right); } function storeInorder(node, inorder) { // Base Case if (node == null) return inorder; storeInorder(node.left, inorder); inorder[index] = node.data; index++; storeInorder(node.right, inorder); } function countNodes(root) { if (root == null) return 0; return countNodes(root.left) + countNodes(root.right) + 1; } function binaryTreeToBST(root) { if (root == null) return; let n = countNodes(root); let arr = new Array(n); arr.fill(0); storeInorder(root, arr); arr.sort(function (a, b) { return a - b }); index = 0; arrayToBST(arr, root); } let root = null; root = newNode(10); root.left = newNode(30); root.right = newNode(15); root.left.left = newNode(20); root.right.right = newNode(5); binaryTreeToBST(root); console.log("Following is Inorder Traversal of the converted BST: "); printInorder(root); OutputFollowing is Inorder Traversal of the converted BST: 5 10 15 20 30 Time Complexity: O(n*logn)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Convert a Binary Tree to a Binary Search Tree using JavaScript abhishek9202 Follow Improve Article Tags : JavaScript Web Technologies javaScript Similar Reads Boundary Traversal of Binary Tree using JavaScript The Boundary Traversal of the Binary Tree can be done by traversing the left, right, and leaf parts of the Binary Tree. We will break the Boundary Traversal of Binary Tree using JavaScript in three parts. First, we will traverse the left part, right part, and leaf parts of the Binary Tree and print 4 min read Merge Two Balanced Binary Search Trees using JavaScript We are given two different Balanced Binary Search Trees and we have to merge them to form a single balanced binary search tree. A Balanced Binary Search Tree (BST) is a binary search tree that keeps its height close to the minimum possible height. This is achieved by ensuring that the height differe 4 min read Count the Number of Nodes in a Complete Binary tree using JavaScript We need to count the number of the nodes in the complete binary tree using JavaScript. The complete binary tree is a binary tree in which every level then except possibly the last, is completely filled and all the nodes are as far left as possible. There are several approaches for counting the numbe 4 min read Binary Search Tree in Javascript In this article, we would be implementing the Binary Search Tree data structure in Javascript. A tree is a collection of nodes connected by some edges. A tree is a non linear data structure. A Binary Search tree is a binary tree in which nodes that have lesser value are stored on the left while the 9 min read Inverting a Binary Tree in JavaScript One can Invert a binary tree using JavaScript. Inverting a binary tree, also known as mirroring a binary tree, is the process of swapping the left and right children of all nodes in the tree. Below is the diagram to understand the problem clearlyInverting a binary treeBelow are the approaches to inv 5 min read Check Symmetrical Binary Tree using JavaScript Given a binary tree, our task is to check whether it is Symmetrical Binary tree. In other words, we need to check whether the binary tree is a mirror of itself. Example: Input: 11 / \ 12 12 / \ / \ 13 14 14 13Output: True Input: 11 / \ 12 12 \ \ 13 13Output: FalseBelow are the approaches to check Sy 3 min read How to convert a DOM nodelist to an array using JavaScript ? NodeList objects are the collection of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). Although NodeList is not an actual Array but it is possible to iterate over it with the help of forEach() method. NodeList can also be converted into 2 min read How to insert a node in Binary Search Tree using Iteration You are given a binary search tree (BST) and a value to insert into the tree. Print inorder traversal of the BST after the insertion.Example: Input:To the given BST insert 40 Output: Explanation:The new node 40 is a leaf node. Start searching from the root till a leaf node is hit, i.e while searchin 10 min read Find the Preorder Successor of a Given Node in a Binary Tree using JavaScript The Preorder successor of a given node is a node that occurs after the given node in the preorder traversal of the binary tree. The preorder traversal is a traversal in which the root node is visited first, then the left child, and then the right child. Example: The pre-order successor of a binary t 2 min read Print the nodes having exactly one child in a Binary tree using JavaScript Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return â-1â if no such node exists. Examples: Input: 1 / \ 2 3 / \ 4 5 / 6Output: 3Explanation:There are three nodes in the Binary tree that have at least one child that are 1,2,4 4 min read Like