JSFiddle - React, Tailwind, and code Playground
JavaScript
function Node (data,left,right) {
this.data = data;
this.show = show;
}
function show() {
console.log(this.data);
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
}
function insert(data) {
var n = new Node(data,null,null);
if(this.root == null) { //
this.root = n;
} else {
var currNode = this.root,parent;
while(true) {
parent = currNode;
if(data < currNode.data) {
currNode = currNode.left;
if(currNode == null) {
parent.left = n;
break;
}
} else {
currNode = currNode.right;
if(currNode == null) {
parent.right = n;
break;
}
}
}
}
}
function inOrder(node) {
if(!(node == null)) {
inOrder(node.left);
node.show();
inOrder(node.right);
}
}
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
console.log("InOrder traversal: ");
inOrder(nums.root);