JSFiddle - React, Tailwind, and code Playground
by russau
JavaScript
//http://upload.wikimedia.org/wikipedia/commons/6/67/Sorted_binary_tree.svg
function node(data) {
this.data = data;
this.left = null;
this.right = null;
this.print = function() {
document.write(this.data)
};
}
function child_depth(depth, node) {
if (node == null) return;
document.write(depth)
node.print();
child_depth(depth+1, node.left);
child_depth(depth+1, node.right);
}
function traverse(node) {
if (node == null) return;
traverse(node.left);
node.print();
traverse(node.right);
}
function breadthfirst(node) {
var stack = [];
stack.push(node);
while (stack.length > 0) {
var n = stack.shift();
n.print();
if (n.left != null) stack.push(n.left);
if (n.right != null) stack.push(n.right);
}
}
var root = new node('f');
var b = new node('b');
var g = new node('g');
root.left = b;
root.right = g;
var a = new node('a');
var d = new node('d');
b.left = a;
b.right = d;
var c = new node('c');
var e = new node('e');
d.left = c;
d.right = e;
var i = new node('i');
g.right = i;
var h = new node('h');
i.left = h;
//breadthfirst(root);
child_depth(0, root);