<repo_name>fangweb/bst-lib<file_sep>/README.md
## About
A binary search tree implementation. 
<file_sep>/index.js
function Node(v, l, r) {
  return {
    value: v,
    left: l,
    right: r,
  }
}

function initTree(v) {
  return Node(v, Node(), Node());
}

function insert(Tree, NewNode) { 
  if (Tree.value === undefined) {
    Tree.value = NewNode.value;
    Tree.left = Node();
    Tree.right = Node();
    return;
  } else if (Tree.value > NewNode.value) {
    insert(Tree.left, NewNode);
  } else if (Tree.value < NewNode.value) {
    insert(Tree.right, NewNode);
  } else { 
    // equal ? 
  }
  
  return Tree;
}

function deleteNode(Tree, value) {
  if (Tree.value === value) {
    Tree.value = Tree.left.value;
    Tree.left = Tree.left.left;
    return;
  }
  
  if (Tree.value > value) {
    deleteNode(Tree.left, value);
  } else {
    deleteNode(Tree.right, value);  
  }
}

function getSubtree(Tree, value) {
  if (Tree.value === value) return Tree;
  if (Tree.value > value) {
    if (Tree.left.value === undefined) return null; 
    return getSubtree(Tree.left, value);
  } else {
    if (Tree.right.value === undefined) return null; 
    return getSubtree(Tree.right, value);
  }
}

function prettyPrint(Tree) {
  return JSON.stringify(Tree, null, 4);
}

exports.Node = Node;
exports.initTree = initTree;
exports.insert = insert;
exports.deleteNode = deleteNode;
exports.getSubtree = getSubtree;
exports.prettyPrint = prettyPrint;
