-
Notifications
You must be signed in to change notification settings - Fork 269
Implement Binary Tree to Binary Search Tree Conversion - Solution #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
Comment on lines
+21
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a better way to do this is to create a file inside Problems/tree-traversal directory nd have the following:
And re-use the function here. Alternatively, I am going forward to create these as helpers inside DataStructures/Trees/traversal so that they can be re-used |
||
|
||
// Helper function to copy elements from sorted array to make BST while keeping same structure | ||
function arrayToBST(arr, root) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you mention the runtime complexity of this function? |
||
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, | ||
}; |