Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
Last active June 11, 2018 16:41
Show Gist options
  • Select an option

  • Save pinkmomo027/8b89a4163362ae403837708d21a7ded8 to your computer and use it in GitHub Desktop.

Select an option

Save pinkmomo027/8b89a4163362ae403837708d21a7ded8 to your computer and use it in GitHub Desktop.
trying out javascript class
class TNode {
constructor (value) {
this.value = value;
this.left = null;
this.right = null;
}
print (level=0) {
let c = level * 2;
while(c > 0) {
process.stdout.write('- ');
c--;
}
process.stdout.write(`${this.value} \n`);
if (this.left) {
this.left.print(level + 1);
}
if (this.right) {
this.right.print(level + 1);
}
}
}
let root = new TNode(0);
let l1 = new TNode(10);
let r1 = new TNode(100);
root.left = l1;
root.right = r1;
let l2 = new TNode(11);
let r2 = new TNode(12);
l1.left = l2;
l1.right = r2;
l2.left = new TNode(20);
r2.left = new TNode(30);
r2.right = new TNode(31);
let l3 = new TNode(101);
let r3 = new TNode(102);
r1.left = l3;
r1.right = r3;
root.print();
// 0
// - - 10
// - - - - 11
// - - - - - - 20
// - - - - 12
// - - - - - - 30
// - - - - - - 31
// - - 100
// - - - - 101
// - - - - 102
// [Finished in 0.1s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment