Created
March 19, 2014 18:59
-
-
Save ionox0/9648813 to your computer and use it in GitHub Desktop.
converts a sorted array to a binary tree
This file contains 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 arrayToBST(list){ | |
function buildTree(list, min, max){ | |
if (min == max){ | |
return { | |
value: list[max], | |
left: null, | |
right: null | |
}; | |
} | |
console.log('asdf'); | |
var middle = Math.floor((min+max)/2); | |
var left = buildTree(list, min, middle-1); | |
var right = buildTree(list, middle+1, max); | |
return { | |
value: list[middle], | |
left: left, | |
right: right | |
}; | |
} | |
return buildTree(list, 0, list.length-1); | |
} | |
var test = [1,2,3,4,5,6,7]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment