-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/_Problems_/binary-tree-to-binary-search-tree/binary-tree-to-binary-search-tree.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { binaryTreeToBST, storeInorder } = require('.'); | ||
const BinaryTree = require('../../_DataStructures_/Trees/BinaryTree'); | ||
|
||
describe('Binary tree to binary search tree', () => { | ||
let tree; | ||
|
||
describe('Create Binary Tree', () => { | ||
tree = new BinaryTree([10, 30, 15, 20, null, null, 5]); | ||
}); | ||
|
||
it('Should converted binary tree to binary search tree', () => { | ||
const bTree = binaryTreeToBST(tree); | ||
expect(storeInorder(bTree)).toEqual([5, 10, 15, 20, 30]); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
const Node = require('../../_DataStructures_/Trees/BinaryTree/Node'); | ||
// 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 | ||
// 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) { | ||
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 null; | ||
|
||
const bstNode = new Node(); | ||
// First update the left subtree | ||
const leftChild = arrayToBST(arr, node.leftChild); | ||
if (leftChild) { | ||
bstNode.leftChild = leftChild; | ||
} | ||
|
||
// update the root's data and remove it from sorted array | ||
// eslint-disable-next-line no-param-reassign | ||
bstNode.value = arr.shift(); | ||
|
||
// Finally update the right subtree | ||
const rightChild = arrayToBST(arr, node.rightChild); | ||
if (rightChild) { | ||
bstNode.rightChild = rightChild; | ||
} | ||
|
||
return bstNode; | ||
} | ||
|
||
function binaryTreeToBST(bTree) { | ||
// Tree is empty | ||
if (!bTree.root) return null; | ||
const arr = bTree.preOrder(); | ||
arr.sort((a, b) => a - b); | ||
const bst = arrayToBST(arr, bTree.root); | ||
return bst; | ||
} | ||
|
||
module.exports = { | ||
binaryTreeToBST, | ||
storeInorder, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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