Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Binary Search Trees</h1>
<i>(Full CREATE, READ, UPDATE, DELETE operations)</i></br></br>
<i>Constraints:</i></br>
<i>1. Left nodes always <= to every right node</i></br>
<i>2. Duplicate entries (values) allowed</i></br>

<h2>Remove Node</h2>
<h5>Begin</h5>
<div id="remove_init"></div>
<h5>Removing Node 7</h5>
<div id="remove_end"></div>

<h2>Count Height</h2>
<h5>Begin</h5>
<div id="count_begin"></div>
<h5>Count Height</h5>
<div id="count"></div>

<h2>Get Node and Validate</h2>
<div id="get_one"></div>

<h2>Get Node and Validate</h2>
<div id="get_two"></div>

<h2>Insert Node</h2>
<h5>Begin</h5>
<div id="add_init"></div>
<h5>Insert Node 3 (No 8)</h5>
<div id="add_end"></div>

<h2>Create Trees From Array</h2>

<h5>Begin</h5>
<div>[1,2,3,4,5,6,7,8,9]</div>
<h5>Create End</h5>
<div id="create_one"></div>
<h5>Count Height</h5>
<div id="count_one"></div>

<h5>Begin</h5>
<div>[5,4,3,2,6,1,7,9,8,0,10,11]</div>
<h5>Create End</h5>
<div id="create_two"></div>
<h5>Count Height</h5>
<div id="count_two"></div>

<h5>Begin</h5>
<div>[99,10,1]</div>
<h5>Create End</h5>
<div id="create_three"></div>
<h5>Count Height</h5>
<div id="count_three"></div>

<h5>Begin</h5>
<div>[77,82]</div>
<h5>Create End</h5>
<div id="create_four"></div>
<h5>Count Height</h5>
<div id="count_four"></div>

<h5>Begin</h5>
<div>[1]</div>
<h5>Create End</h5>
<div id="create_five"></div>
<h5>Count Height</h5>
<div id="count_five"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const print = (txt, context) => {
  const el = document.getElementById(context);
  const n = document.createElement("div");
  el.appendChild(n);
  n.innerHTML = JSON.stringify(txt);
  console.log(txt);
};

/**
 * Basic BST functions.
 */

const Node = function(data, left, right) {
  this.data = data;
  this.left = left;
  this.right = right;
};

const resolveNode = (node, context) => {
  if (node) {
    let left = "null",
      right = "null";
    if (node.left) {
      resolveNode(node.left, context);
      left = node.left.data;
    }
    if (node.right) {
      resolveNode(node.right, context);
      right = node.right.data;
    }
    print(
      `tree: {node: ${
        node.data
      } left: ${left} right: ${right}} isBst: ${isValid(node)}`,
      context
    );
  }
};

/**
 * BST definitions:
 *
 * 1. Parent nodes always > child nodes (disabled) - strong definition - essentially, a heap.
 * 2. Left nodes always < right nodes (enabled) - by itself, weak definition for BST.
 */

const isValid = node => {
  if (node) {
    return !(node.right && node.left && node.left.data > node.right.data);
  }
};

const countHeight = (node, height = 0, context) => {
  height++;
  if (!node.left && node.right) {
    print(
      `Max branch {data: ${node.data}, left: ${node.left}, right: ${node.right.data}} - height: ${height}`,
      context
    );
  }
  if (!node.right && node.left) {
    print(
      `Max branch {data: ${node.data}, left: ${node.left.data}, right: ${node.right}} - height: ${height}`,
      context
    );
  }
  if (node.left) countHeight(node.left, height, context);
  if (node.right) countHeight(node.right, height, context);
  if (!node.left && !node.right) {
    print(
      `Max branch {data: ${node.data}, left: ${node.left}, right: ${node.right}} - height: ${height}`,
      context
    );
    return height;
  }
};

const toArray = (tree, context) => {
  let result = [];
  const innerArray = (tree, context) => {
    if (result.indexOf(tree.data) == -1) result.push(tree.data);
    if (tree.left) innerArray(tree.left, context);
    if (tree.right) innerArray(tree.right, context);
  };
  innerArray(tree, context);
  return result;
};

/**
 * CRUD.
 */

const get = (value, tree, context) => {
  if (tree.left) {
    if (value === tree.left.data) resolveNode(tree.left, context);
    else get(value, tree.left);
  }
  if (tree.right) {
    if (value === tree.right.data) resolveNode(tree.right, context);
    else get(value, tree.right);
  }
};

//modifies tree - use modified tree thereafter
const remove = (value, tree) => {
  if (tree.left) {
    if (value === tree.left.data) tree.left = null;
    else remove(value, tree.left);
  }
  if (tree.right) {
    if (value === tree.right.data) tree.right = null;
    else remove(value, tree.right);
  }
};

const buildTree = (arr, context) => {
  const customSort = (a, b) => a - b;
  arr.sort(customSort);
  if (arr.length === 1) {
    let tree = new Node(arr[0], null, null);
    resolveNode(tree, context);
    return tree;
  }
  if (arr.length === 2) {
    let tree = new Node(arr[1], new Node(arr[0], null, null), null);
    resolveNode(tree, context);
    return tree;
  }
  if (arr.length === 3) {
    let tree = new Node(
      arr[1],
      new Node(arr[0], null, null),
      new Node(arr[2], null, null)
    );
    resolveNode(tree, context);
    return tree;
  }
  if (arr.length > 3) {
    const inner = arr => {
      const left = arr.shift();
      const data = arr.shift();
      const right = arr.shift();
      let node = new Node(
        data,
        left instanceof Node ? left : new Node(left, null, null),
        new Node(right, null, null)
      );
      arr.unshift(node);
      if (arr.length === 1) return node;
      if (arr.length === 2) return new Node(arr[1], arr[0], null);
      if (arr.length === 3)
        return new Node(arr[1], arr[0], new Node(arr[2], null, null));
      if (arr.length > 3) return inner(arr);
    };
    let tree = inner(arr);
    resolveNode(tree, context);
    return tree;
  }
};

const insert = (node, tree, context) => {
  let nodeArray = toArray(node, context);
  let treeArray = toArray(tree, context);
  buildTree(nodeArray.concat(treeArray), context);
};

/**
 * Tests.
 */

// remove a node and all its subnodes
const rightOne = new Node(7, new Node(6, null, null), new Node(9, null, null));
const leftOne = new Node(3, new Node(2, null, null), new Node(4, null, null));
const testOne = new Node(5, leftOne, rightOne);
resolveNode(testOne, "remove_init");
remove(7, testOne);
resolveNode(testOne, "remove_end");

// get and verify if node is BST
const rightTwo = new Node(4, new Node(6, null, null), new Node(7, null, null));
const leftTwo = new Node(3, new Node(1, null, null), new Node(2, null, null));
const testTwo = new Node(5, leftTwo, rightTwo);
get(4, testTwo, "get_one");

// count height
const rightFive = new Node(3, new Node(4, null, null), new Node(5, null, null));
const leftFive = new Node(1, new Node(1, null, null), new Node(2, null, null));
const testFive = new Node(6, leftFive, rightFive);
resolveNode(testFive, "count_begin");
countHeight(testFive, 0, "count");

// get and verify if node is BST
const rightThree = new Node(
  7,
  new Node(6, null, null),
  new Node(9, null, null)
);
const leftThree = new Node(3, new Node(2, null, null), new Node(4, null, null));
const testThree = new Node(5, leftOne, rightOne);
get(7, testThree, "get_two");

// create Trees
const arr_one = [1, 2, 3, 4, 5, 6, 7, 8, 9];
countHeight(buildTree(arr_one, "create_one"), 0, "count_one");
const arr_two = [5, 4, 3, 2, 6, 1, 7, 9, 8, 0, 10, 11];
countHeight(buildTree(arr_two, "create_two"), 0, "count_two");
const arr_three = [99, 10, 1];
countHeight(buildTree(arr_three, "create_three"), 0, "count_three");
const arr_four = [77, 82];
countHeight(buildTree(arr_four, "create_four"), 0, "count_four");
const arr_five = [1];
countHeight(buildTree(arr_five, "create_five"), 0, "count_five");

// insert Node
const rightFour = new Node(7, new Node(6, null, null), new Node(9, null, null));
const testFour = new Node(5, null, rightFour);
resolveNode(testFour, "add_init");
const leftFour = new Node(3, new Node(2, null, null), new Node(4, null, null));
insert(leftFour, testFour, "add_end");

              
            
!
999px

Console