From 2ba8925c78c28da36aa7d0185255e4c65ff061b3 Mon Sep 17 00:00:00 2001 From: manish Date: 2019年10月23日 00:50:15 +0530 Subject: [PATCH 1/2] Implement Binary Tree to Binary Search Tree Conversion - Solution --- .../binary-tree-to-binary-search-tree/Node.js | 7 ++ .../binary-tree-to-binary-search-tree.test.js | 19 ++++++ .../index.js | 65 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/_Problems_/binary-tree-to-binary-search-tree/Node.js create mode 100644 src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js create mode 100644 src/_Problems_/binary-tree-to-binary-search-tree/index.js diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/Node.js b/src/_Problems_/binary-tree-to-binary-search-tree/Node.js new file mode 100644 index 00000000..2b515b2a --- /dev/null +++ b/src/_Problems_/binary-tree-to-binary-search-tree/Node.js @@ -0,0 +1,7 @@ +module.exports = class Node { + constructor(value) { + this.value = value; + this.leftChild = null; // will be a node + this.rightChild = null; // will be a node + } +}; diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js new file mode 100644 index 00000000..9e6a9fe8 --- /dev/null +++ b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js @@ -0,0 +1,19 @@ +const { binaryTreeToBST, storeInorder } = require('.'); +const Node = require('./Node'); + +describe('Binary tree to binary search tree', () => { + let tree; + + describe('Create Binary Tree', () => { + tree = new Node(10); + tree.leftChild = new Node(30); + tree.leftChild.leftChild = new Node(20); + tree.rightChild = new Node(15); + tree.rightChild.rightChild = new Node(5); + }); + + it('Should converted binary tree to binary search tree', () => { + binaryTreeToBST(tree); + expect(storeInorder(tree)).toEqual([5, 10, 15, 20, 30]); + }); +}); diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/index.js b/src/_Problems_/binary-tree-to-binary-search-tree/index.js new file mode 100644 index 00000000..d1c1ae17 --- /dev/null +++ b/src/_Problems_/binary-tree-to-binary-search-tree/index.js @@ -0,0 +1,65 @@ +/** + * Given a Binary Tree, convert it to a Binary Search Tree. + * The conversion must be done in such a way that keeps the original structure of Binary Tree. + * Example 1 +Input: + 10 + / \ + 2 7 + / \ + 8 4 +Output: + 8 + / \ + 4 10 + / \ + 2 7 + */ + +// Helper function to store inorder traversal of a binary tree +function storeInorder(root) { + /** left - root - right */ + if (root === null) return []; + + // First store the left subtree + let arr = []; + const left = storeInorder(root.leftChild); + arr = [...left, ...arr]; + + // Append root's data + arr = [...arr, root.value]; + + // Store right subtree + const right = storeInorder(root.rightChild); + arr = [...arr, ...right]; + return arr; +} + +// Helper function to copy elements from sorted array to make BST while keeping same structure +function arrayToBST(arr, root) { + const node = root; + // Base case + if (!node) return; + + // First update the left subtree + arrayToBST(arr, node.leftChild); + + // update the root's data and remove it from sorted array + node.value = arr.shift(); + + // Finally update the right subtree + arrayToBST(arr, node.rightChild); +} + +function binaryTreeToBST(root) { + // Tree is empty + if (!root) return; + const arr = storeInorder(root); + arr.sort((a, b) => a - b); + arrayToBST(arr, root); +} + +module.exports = { + binaryTreeToBST, + storeInorder, +}; From 7578fb1ca0f6ab99e6ed5b4719a1c1312953d7c8 Mon Sep 17 00:00:00 2001 From: manish Date: 2019年10月26日 16:39:11 +0530 Subject: [PATCH 2/2] Binary Tree to BST - Return null value - Using Node Class already existed - Mentioned Runtime complexity of function - Return BST root node - Used BinaryTree implementation to create the tree. --- .../Trees/BinaryTree/index.js | 4 +-- .../binary-tree-to-binary-search-tree/Node.js | 7 ----- .../binary-tree-to-binary-search-tree.test.js | 12 +++----- .../index.js | 29 ++++++++++++++----- 4 files changed, 27 insertions(+), 25 deletions(-) delete mode 100644 src/_Problems_/binary-tree-to-binary-search-tree/Node.js diff --git a/src/_DataStructures_/Trees/BinaryTree/index.js b/src/_DataStructures_/Trees/BinaryTree/index.js index f30999ad..7cb032c4 100644 --- a/src/_DataStructures_/Trees/BinaryTree/index.js +++ b/src/_DataStructures_/Trees/BinaryTree/index.js @@ -3,14 +3,14 @@ const Node = require('./Node'); class BinaryTree { constructor(arr) { if (!Array.isArray(arr) || !arr.length) { - throw new Error('Invalid argument to create a Binary Tre'); + throw new Error('Invalid argument to create a Binary Tree'); } this.root = this.createBinaryTree((this.root = null), arr, 0); } // eslint-disable-next-line class-methods-use-this createBinaryTree(root, arr, i) { - if (i < arr.length) { + if (i < arr.length && arr[i]) { // eslint-disable-next-line no-param-reassign root = new Node(arr[i]); // eslint-disable-next-line no-param-reassign diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/Node.js b/src/_Problems_/binary-tree-to-binary-search-tree/Node.js deleted file mode 100644 index 2b515b2a..00000000 --- a/src/_Problems_/binary-tree-to-binary-search-tree/Node.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = class Node { - constructor(value) { - this.value = value; - this.leftChild = null; // will be a node - this.rightChild = null; // will be a node - } -}; diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js index 9e6a9fe8..95913a7e 100644 --- a/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js +++ b/src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js @@ -1,19 +1,15 @@ const { binaryTreeToBST, storeInorder } = require('.'); -const Node = require('./Node'); +const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree'); describe('Binary tree to binary search tree', () => { let tree; describe('Create Binary Tree', () => { - tree = new Node(10); - tree.leftChild = new Node(30); - tree.leftChild.leftChild = new Node(20); - tree.rightChild = new Node(15); - tree.rightChild.rightChild = new Node(5); + tree = new BinaryTree([10, 30, 15, 20, null, null, 5]); }); it('Should converted binary tree to binary search tree', () => { - binaryTreeToBST(tree); - expect(storeInorder(tree)).toEqual([5, 10, 15, 20, 30]); + const bTree = binaryTreeToBST(tree); + expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]); }); }); diff --git a/src/_Problems_/binary-tree-to-binary-search-tree/index.js b/src/_Problems_/binary-tree-to-binary-search-tree/index.js index d1c1ae17..1a56c5bd 100644 --- a/src/_Problems_/binary-tree-to-binary-search-tree/index.js +++ b/src/_Problems_/binary-tree-to-binary-search-tree/index.js @@ -16,6 +16,7 @@ Output: 2 7 */ +const Node = require('../../_DataStructures_/Trees/BinaryTree/Node'); // Helper function to store inorder traversal of a binary tree function storeInorder(root) { /** left - root - right */ @@ -36,27 +37,39 @@ function storeInorder(root) { } // Helper function to copy elements from sorted array to make BST while keeping same structure +// Runtime complexity iof this function is O(n) where n is number of nodes, as we are each node of tree one time. function arrayToBST(arr, root) { const node = root; // Base case - if (!node) return; + if (!node) return null; + const bstNode = new Node(); // First update the left subtree - arrayToBST(arr, node.leftChild); + const leftChild = arrayToBST(arr, node.leftChild); + if (leftChild) { + bstNode.leftChild = leftChild; + } // update the root's data and remove it from sorted array - node.value = arr.shift(); + // eslint-disable-next-line no-param-reassign + bstNode.value = arr.shift(); // Finally update the right subtree - arrayToBST(arr, node.rightChild); + const rightChild = arrayToBST(arr, node.rightChild); + if (rightChild) { + bstNode.rightChild = rightChild; + } + + return bstNode; } -function binaryTreeToBST(root) { +function binaryTreeToBST(bTree) { // Tree is empty - if (!root) return; - const arr = storeInorder(root); + if (!bTree.root) return null; + const arr = bTree.preOrder(); arr.sort((a, b) => a - b); - arrayToBST(arr, root); + const bst = arrayToBST(arr, bTree.root); + return bst; } module.exports = {

AltStyle によって変換されたページ (->オリジナル) /