Skip to content

Instantly share code, notes, and snippets.

@nirajrajgor
Created September 20, 2020 17:33
Show Gist options
  • Save nirajrajgor/eb4ef0356c5933d71d6d1cad84852163 to your computer and use it in GitHub Desktop.
Save nirajrajgor/eb4ef0356c5933d71d6d1cad84852163 to your computer and use it in GitHub Desktop.
Tree & its Node using ES6 Classes in javascript.
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
remove(data) {
this.children = this.children.filter(child => child.data !== data);
}
}
class Tree {
constructor() {
this.root = null;
}
}
const t = new Tree();
t.root = new Node('a');
t.root.add('b');
t.root.add('c');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment