Skip to content

Instantly share code, notes, and snippets.

@dcorns
Last active August 29, 2015 13:57
Show Gist options
  • Save dcorns/9558962 to your computer and use it in GitHub Desktop.
Save dcorns/9558962 to your computer and use it in GitHub Desktop.
Recursion method for converting a sorted array to a binary tree
var arrayToBST = function(ary){
//make return object here for simple manipulation
bt = {};
//encapsulate start functions within validation
//validate input: is array
if(Array.isArray(ary)){
//validate input: numberts only
if(!(testNumbersOnly(ary))){
console.log('Array input invalid, only numbers please.');
return;
}
//validate input: numbers sorted
if(!(testSorted(ary))){
console.log('I only convert, I don\'t sort!');
return;
}
//all tests passed++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//if three or less numbers, skip recursion
if(ary.length < 4){
return makeSubTree(ary);
}
else{
//greater than three numbers, run recursion
var baseIdx = Math.floor(ary.length/2);
var base = ary[baseIdx];
makeTree(ary,base,{});
return bt;
}
}
else{
console.log('Input needs to be an array');
return;
}
function makeBtNode(root,left,right){
console.log('BT NODE: ' + 'root: ' + root+','+'left: '+left+',right: ' + right);
return {'root':root,'left':left,'right':right};
}
function makeTree(ary,base,obj){
var baseIdx = ary.indexOf(base);
//test for first run
if(!(bt.hasOwnProperty('root'))){
//plant tree top
bt = makeBtNode(base, null, null);
console.log("Tree Top: "+bt);
}
//graft in branches
if((baseIdx -1) % 2 == 0 && (!(obj.hasOwnProperty('root')))){
//an even number indicates an odd number of elements. create root,null,null for bottom
obj = makeBtNode(ary[0],null,null);
ary.splice(0,1);
}
else{ //if even number at start or one pass already occured
if(obj.hasOwnProperty('root')){
var branch = makeBtNode(ary[0],null,ary[1]);
branch.left = obj;
obj = branch;
}
else{
obj = makeBtNode(ary[0],null,ary[1]);
}
ary.splice(0,2);
}
if(ary.length < 2){
//complete right side and end recursion
bt.right = obj;
return;
}
else{
if(ary.indexOf(base) < 1){
//complete left side and start the right
bt.left = obj;
obj = {};
base = ary.shift();
ary.push(base);
}
}
makeTree(ary,base,obj);
}
//input test functions++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function testNumbersOnly(ary){
var count=0;
while(count < ary.length){
if(!(typeof ary[count] == 'number')){
return false;
}
count++;
}
return true;
}
function testSorted(ary){
var count=1;
while(count < ary.length){
if(ary[count-1] > ary[count]){
return false;
}
count++;
}
return true;
}
//function for submissions of 3 values or less
function makeSubTree(list){
switch (list.length){
case 1:
return makeBtNode(list[0],null,null);
break;
case 2:
var left = makeBtNode(list[0],null,null);
return makeBtNode(list[1],left,null);
break;
case 3:
var left = makeBtNode(list[0],null,null);
var right = makeBtNode(list[2],null,null);
return makeBtNode(list[1],left,right);
break;
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>recursion fun</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script src="rangeSearch.js"></script>
</head>
<body>
<div id='app'>
arrayToBST([5,8,23,38,46,50,55,64,78,89,95,99])
</br>
rangeSearchBST({ root: 3, left: { root: 2, left: { root: 1, left: null, right: null }, right: null }, right: { root: 4, left: null, right: null } },40,80)
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment