Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="row">
  <div class="col">
    This visulization is part of: <a href="https://livefiredev.com/validate-binary-search-tree-explanation-interactive-demo/">LeetCode: Validate Binary Search Tree Explanation + Interactive Demo</a>
  </div>
</div>

<div class="header">
  <h3>Enter In A Tree In The From Of An Array:</h3>
  <br />
  <input type="text" id="tree_input" value="5,1,4,null,null,3,6" placeholder="[5,1,4,null,null,3,6]" class="input">
  <button class="button-30" id="validate_this_tree">Validate This Tree</button>
</div>
<div class="chart" id="basic-example"></div>
<div id="success-result">This is a valid Binary Search Tree!</div>
<div id="failed-result">This is NOT a valid Binary Search Tree!</div>
              
            
!

CSS

              
                body {
  font-family: "Belanosima", sans-serif;
  font-family: "Roboto", sans-serif;
}

.chart {
  /* border: 1px solid #000; */
  padding: 20px;
  margin: 20px;
}

.node {
  border: 2px solid #000;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  text-align: center;
  font-weight: bold;
}

.node-desc {
  top: -9px;
  position: relative;
  left: -47px;
  width: 142px;
  color: #fff;
  background-color: #939393;
  border: 3px solid red;
  border-radius: 10px;
}

.in_valid_node {
  background-color: #f90b31;
  color: white;
}

.valid_node {
  background-color: #a1c9b0;
  color: white;
}

.button_holder {
  text-align: center;
  margin-top: 30px;
}

.input {
  height: 40px;
  border: 1px solid #767676;
  border-radius: 5px;
  padding: 5px;
  margin-bottom: 10px;
  font-size: 19px;
  margin-right: 20px;
}

h3 {
  margin-bottom: 1px;
}

#success-result {
  background-color: #a1c9b0;
  color: #fff;
  padding: 10px;
  border-radius: 5px;
  font-size: 30px;
  margin-left: 10%;
  margin-right: 10%;
  text-align: center;
  display: none;
}

#failed-result {
  background-color: #fc8f8f;
  color: #fff;
  padding: 10px;
  border-radius: 5px;
  font-size: 30px;
  margin-left: 10%;
  margin-right: 10%;
  text-align: center;
  display: none;
}

.shake_me {
  animation: shake 0.5s;
  animation-iteration-count: infinite;
}

@keyframes shake {
  0% {
    transform: translate(1px, 1px) rotate(0deg);
  }
  10% {
    transform: translate(-1px, -2px) rotate(-1deg);
  }
  20% {
    transform: translate(-3px, 0px) rotate(1deg);
  }
  30% {
    transform: translate(3px, 2px) rotate(0deg);
  }
  40% {
    transform: translate(1px, -1px) rotate(1deg);
  }
  50% {
    transform: translate(-1px, 2px) rotate(-1deg);
  }
  60% {
    transform: translate(-3px, 1px) rotate(0deg);
  }
  70% {
    transform: translate(3px, 1px) rotate(-1deg);
  }
  80% {
    transform: translate(-1px, -1px) rotate(1deg);
  }
  90% {
    transform: translate(1px, 2px) rotate(0deg);
  }
  100% {
    transform: translate(1px, -2px) rotate(-1deg);
  }
}

/* CSS */
.button-30 {
  align-items: center;
  appearance: none;
  background-color: #fcfcfd;
  border-radius: 4px;
  border-width: 0;
  box-shadow: rgba(45, 35, 66, 0.4) 0 2px 4px,
    rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #d6d6e7 0 -3px 0 inset;
  box-sizing: border-box;
  color: #36395a;
  cursor: pointer;
  display: inline-flex;
  font-family: "JetBrains Mono", monospace;
  height: 48px;
  justify-content: center;
  line-height: 1;
  list-style: none;
  overflow: hidden;
  padding-left: 16px;
  padding-right: 16px;
  position: relative;
  text-align: left;
  text-decoration: none;
  transition: box-shadow 0.15s, transform 0.15s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  white-space: nowrap;
  will-change: box-shadow, transform;
  font-size: 18px;
}

.button-30:focus {
  box-shadow: #d6d6e7 0 0 0 1.5px inset, rgba(45, 35, 66, 0.4) 0 2px 4px,
    rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #d6d6e7 0 -3px 0 inset;
}

.button-30:hover {
  box-shadow: rgba(45, 35, 66, 0.4) 0 4px 8px,
    rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #d6d6e7 0 -3px 0 inset;
  transform: translateY(-2px);
}

.button-30:active {
  box-shadow: #d6d6e7 0 3px 7px inset;
  transform: translateY(2px);
}

.header {
  text-align: center;
}

              
            
!

JS

              
                class Node {
  constructor(value, leftNode, rightNode) {
    this.value = value;
    this.leftNode = leftNode;
    this.rightNode = rightNode;
    this.min_value_possible = null;
    this.max_value_possible = null;
    this.isValid = null;
    this.setUpInstanceVariables();
  }

  setMinAndMaxValuesPossible(min_value_possible, max_value_possible) {
    this.min_value_possible = min_value_possible.toString();
    this.max_value_possible = max_value_possible.toString();

    this.min_value_possible = this.min_value_possible.replace("Infinity", "∞");
    this.max_value_possible = this.max_value_possible.replace("Infinity", "∞");

    if (this.value > min_value_possible && this.value < max_value_possible) {
      this.isValid = true;
    } else {
      this.isValid = false;
    }
  }

  representAsTreantJsConfig() {
    let config = {
      text: {
        name: this.val
      }
    };
    if (this.min_value_possible != null) {
      config.text.desc = `Min:${this.min_value_possible}, Max:${this.max_value_possible}`;
    }

    if (this.isValid == true) {
      config.HTMLclass = "valid_node";
    } else if (this.isValid == false) {
      config.HTMLclass = "in_valid_node";
    } else {
      config.HTMLclass = "to_check";
    }

    let childrenArray = [];
    if (this.leftNode != null) {
      childrenArray.push(this.leftNode.representAsTreantJsConfig());
    }

    if (this.rightNode != null) {
      childrenArray.push(this.rightNode.representAsTreantJsConfig());
    }

    config.children = childrenArray;
    return config;
  }

  setUpInstanceVariables() {
    this.val = this.value;
    this.left = this.leftNode;
    this.right = this.rightNode;
  }

  value() {
    return this.value;
  }

  leftNode() {
    return this.leftNode;
  }

  rightNode() {
    return this.rightNode;
  }
}

const constructBinaryTree = (treeAsArray, rootIdx) => {
  let value = treeAsArray[rootIdx];
  let leftNode = null;
  let rightNode = null;

  let leftNodeIdx = 2 * rootIdx + 1;
  let rightNodeIdx = 2 * rootIdx + 2;

  if (
    treeAsArray[leftNodeIdx] != null ||
    treeAsArray[leftNodeIdx] != undefined
  ) {
    leftNode = constructBinaryTree(treeAsArray, leftNodeIdx);
  }

  if (
    treeAsArray[rightNodeIdx] != null ||
    treeAsArray[rightNodeIdx] != undefined
  ) {
    rightNode = constructBinaryTree(treeAsArray, rightNodeIdx);
  }

  return new Node(value, leftNode, rightNode);
};

let isThisAValidSubTree = (node, min_value_possible, max_value_possible) => {
  node.setMinAndMaxValuesPossible(min_value_possible, max_value_possible);
  addToTreeConfigs();

  let nodeVal = node.val;
  if (nodeVal <= min_value_possible || nodeVal >= max_value_possible) {
    return false;
  }

  let leftChild = node.left;
  let leftChildMinValue = min_value_possible;
  let leftChildMaxValue = nodeVal;

  if (
    leftChild != null &&
    isThisAValidSubTree(leftChild, leftChildMinValue, leftChildMaxValue) ==
      false
  ) {
    return false;
  }

  let rightChild = node.right;
  let rightChildMinValue = nodeVal;
  let rightChildMaxValue = max_value_possible;

  if (
    rightChild != null &&
    isThisAValidSubTree(rightChild, rightChildMinValue, rightChildMaxValue) ==
      false
  ) {
    return false;
  }

  return true;
};

var isValidBST = function (root) {
  return isThisAValidSubTree(
    root,
    Number.NEGATIVE_INFINITY,
    Number.POSITIVE_INFINITY
  );
};

let treeConfigs = [];

const addToTreeConfigs = () => {
  let config = tree.representAsTreantJsConfig();
  let configCopy = JSON.parse(JSON.stringify(config));
  treeConfigs.push(configCopy);
};

const drawTree = (treeAsTreantJsConfig) => {
  let chart_config = {
    chart: {
      container: "#basic-example",
      connectors: {
        type: "straight"
      },
      siblingSeparation: 200,
      levelSeparation: 100,
      subTeeSeparation: 100
    },
    nodeStructure: treeAsTreantJsConfig
  };

  new Treant(chart_config);
  canDrawTree = false;
};

let indexOfConfigToDraw = undefined;
let setIntervalId = undefined;
let tree = undefined;
const jsConfetti = new JSConfetti();

const constructBinaryTreeOnBasisOfInput = () => {
  treeConfigs = [];
  shownConfetti = false;

  document.getElementById("success-result").style.display = "none";
  document.getElementById("failed-result").style.display = "none";

  clearInterval(setIntervalId);
  indexOfConfigToDraw = 0;
  let treeInput = document.getElementById("tree_input").value;
  let treeAsArray = treeInput.split(",").map((val) => {
    let theVal = val.trim();
    if (isNaN(theVal)) {
      return null;
    } else {
      return parseInt(theVal);
    }
  });
  tree = constructBinaryTree(treeAsArray, 0);
  let resultOfTreeValidity = isValidBST(tree);

  setIntervalId = setInterval(() => {
    if (indexOfConfigToDraw < treeConfigs.length) {
      drawTree(treeConfigs[indexOfConfigToDraw]);
      indexOfConfigToDraw++;
    } else {
      clearInterval(setIntervalId);
      if (resultOfTreeValidity == true) {
        document.getElementById("success-result").style.display = "block";
        jsConfetti.addConfetti({
          emojis: ["🎉", "🎊"],
          emojiSize: 50,
          confettiNumber: 100,
          confettiRadius: 6,
          confettiHeight: 6,
          wind: 0,
          gravity: 0.2,
          rotation: 0,
          rotationDeg: 0,
          colors: ["#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"]
        });
      } else {
        document.getElementById("failed-result").style.display = "block";
        document.getElementById("basic-example").classList.add("shake_me");
        setTimeout(() => {
          document.getElementById("basic-example").classList.remove("shake_me");
        }, 1000);
      }
    }
  }, 1000);
};

document.getElementById("validate_this_tree").addEventListener("click", () => {
  constructBinaryTreeOnBasisOfInput();
});

              
            
!
999px

Console