Skip to content

Commit 7578fb1

Browse files
committed
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.
1 parent 2ba8925 commit 7578fb1

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/_DataStructures_/Trees/BinaryTree/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const Node = require('./Node');
33
class BinaryTree {
44
constructor(arr) {
55
if (!Array.isArray(arr) || !arr.length) {
6-
throw new Error('Invalid argument to create a Binary Tre');
6+
throw new Error('Invalid argument to create a Binary Tree');
77
}
88
this.root = this.createBinaryTree((this.root = null), arr, 0);
99
}
1010

1111
// eslint-disable-next-line class-methods-use-this
1212
createBinaryTree(root, arr, i) {
13-
if (i < arr.length) {
13+
if (i < arr.length && arr[i]) {
1414
// eslint-disable-next-line no-param-reassign
1515
root = new Node(arr[i]);
1616
// eslint-disable-next-line no-param-reassign

src/_Problems_/binary-tree-to-binary-search-tree/Node.js

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
const { binaryTreeToBST, storeInorder } = require('.');
2-
const Node = require('./Node');
2+
const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree');
33

44
describe('Binary tree to binary search tree', () => {
55
let tree;
66

77
describe('Create Binary Tree', () => {
8-
tree = new Node(10);
9-
tree.leftChild = new Node(30);
10-
tree.leftChild.leftChild = new Node(20);
11-
tree.rightChild = new Node(15);
12-
tree.rightChild.rightChild = new Node(5);
8+
tree = new BinaryTree([10, 30, 15, 20, null, null, 5]);
139
});
1410

1511
it('Should converted binary tree to binary search tree', () => {
16-
binaryTreeToBST(tree);
17-
expect(storeInorder(tree)).toEqual([5, 10, 15, 20, 30]);
12+
const bTree = binaryTreeToBST(tree);
13+
expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]);
1814
});
1915
});

src/_Problems_/binary-tree-to-binary-search-tree/index.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Output:
1616
2 7
1717
*/
1818

19+
const Node = require('../../_DataStructures_/Trees/BinaryTree/Node');
1920
// Helper function to store inorder traversal of a binary tree
2021
function storeInorder(root) {
2122
/** left - root - right */
@@ -36,27 +37,39 @@ function storeInorder(root) {
3637
}
3738

3839
// Helper function to copy elements from sorted array to make BST while keeping same structure
40+
// Runtime complexity iof this function is O(n) where n is number of nodes, as we are each node of tree one time.
3941
function arrayToBST(arr, root) {
4042
const node = root;
4143
// Base case
42-
if (!node) return;
44+
if (!node) return null;
4345

46+
const bstNode = new Node();
4447
// First update the left subtree
45-
arrayToBST(arr, node.leftChild);
48+
const leftChild = arrayToBST(arr, node.leftChild);
49+
if (leftChild) {
50+
bstNode.leftChild = leftChild;
51+
}
4652

4753
// update the root's data and remove it from sorted array
48-
node.value = arr.shift();
54+
// eslint-disable-next-line no-param-reassign
55+
bstNode.value = arr.shift();
4956

5057
// Finally update the right subtree
51-
arrayToBST(arr, node.rightChild);
58+
const rightChild = arrayToBST(arr, node.rightChild);
59+
if (rightChild) {
60+
bstNode.rightChild = rightChild;
61+
}
62+
63+
return bstNode;
5264
}
5365

54-
function binaryTreeToBST(root) {
66+
function binaryTreeToBST(bTree) {
5567
// Tree is empty
56-
if (!root) return;
57-
const arr = storeInorder(root);
68+
if (!bTree.root) return null;
69+
const arr = bTree.preOrder();
5870
arr.sort((a, b) => a - b);
59-
arrayToBST(arr, root);
71+
const bst = arrayToBST(arr, bTree.root);
72+
return bst;
6073
}
6174

6275
module.exports = {

0 commit comments

Comments
 (0)