Step -1 of 0
1
/************************************************
2
 
3
Hello! This is a playground for binary trees.
4
 
5
Write JavaScript here, then click the "Run" button
6
to step through your code. On the right hand side,
7
you'll see any trees that are in scope.
8
 
9
For it to work, you must build your trees using
10
the provided TreeNode constructor. You can also
11
use the convenience buildTree function. See the
12
code below for usage.
13
 
14
Note: this is an alpha version, and a lot of things
15
don't work. Notably, only one binary tree at a time
16
will be displayed.
17
 
18
The code is on GitHub:
19
http://github.com/kasrak/treevis
20
 
21
************************************************/
22
 
23
function binarySearch(node, value) {
24
  if (!node || node.value == value) {
25
    return node;
26
  } else if (node.value < value) {
27
    return binarySearch(node.right, value);
28
  } else {
29
    return binarySearch(node.left, value);
30
  }
31
}
32
 
33
function addNode(tree, value) {
34
  if (!tree) {
35
    return TreeNode(value);
36
  } else if (tree.value < value) {
37
    tree.right = addNode(tree.right, value);
38
  } else {
39
    tree.left = addNode(tree.left, value);
40
  }
41
 
42
  return tree;
43
}
44
 
45
var root = buildTree([10, [5, [2], [7]],