Last active
November 16, 2021 00:15
-
-
Save cagataycali/2abb60f881519963ce10ad610c271481 to your computer and use it in GitHub Desktop.
[JavaScript] Binary search tree find min height
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
| const assert = require('assert'); | |
| // Write function that takes in a non-empty sorted array of distinct integers, | |
| // constructs a BST from the integers, and returns the root of the BST. | |
| function minHeightBst(array) { | |
| // Find the median of array, | |
| return constructMinHeightBST(array, 0, array.length - 1) | |
| } | |
| function constructMinHeightBST(array, startIndex, endIndex) { | |
| if (startIndex > endIndex) { | |
| return null; | |
| } | |
| // Find the median index, | |
| const medianIndex = Math.floor((startIndex + endIndex) / 2); | |
| // Create a tree node, | |
| const bst = new BST(array[medianIndex]); | |
| // Go left by 1 | |
| bst.left = constructMinHeightBST(array, startIndex, medianIndex - 1); | |
| bst.right = constructMinHeightBST(array, medianIndex + 1, endIndex); | |
| return bst; | |
| } | |
| class BST { | |
| constructor(value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| insert(value) { | |
| // Move to the left branch direction, | |
| if (value < this.value) { | |
| // If the left branch is null, create a BST node and attach. | |
| if (this.left === null) { | |
| this.left = new BST(value); | |
| } else { | |
| // Recursively call for left branch direction, | |
| this.left.insert(value); | |
| } | |
| } else { | |
| // Move to the right branch direction, | |
| if (this.right === null) { | |
| this.right = new BST(value); | |
| } else { | |
| this.right.insert(value); | |
| } | |
| } | |
| } | |
| } | |
| // The BST must be balanced to achieve the minimum height. | |
| // - Pick the median integer from array, | |
| // - Divide the array into two slice by that index, | |
| // - left slice divide from median, make the median our left root, | |
| // - rigth slice divide from median, make the median our right root, | |
| // - create insertion list by that, | |
| // - insert that list as BST nodes in correct places. | |
| const array = [1, 2, 5, 7, 10, 13, 14, 15, 22]; | |
| // const bst = new BST(5) | |
| // bst.insert(2) | |
| // console.log(JSON.stringify(bst, null, 2)); | |
| minHeightBst(array) |
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
| function minHeightBst(array) { | |
| return constructMinHeightBst(array, 0, array.length - 1); | |
| } | |
| function constructMinHeightBst(array, startIndex, endIndex) { | |
| if (startIndex > endIndex) return null; | |
| const medianIndex = Math.floor((startIndex + endIndex) / 2); | |
| const bst = new BST(array[medianIndex]); | |
| bst.left = constructMinHeightBst(array, startIndex, medianIndex - 1); | |
| bst.right = constructMinHeightBst(array, medianIndex + 1, endIndex); | |
| return bst; | |
| } | |
| function BST(value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| const array = [1, 2, 5, 7, 10, 13, 14, 15, 22]; | |
| console.log(JSON.stringify(minHeightBst(array), null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment