Created
September 20, 2020 17:33
-
-
Save nirajrajgor/eb4ef0356c5933d71d6d1cad84852163 to your computer and use it in GitHub Desktop.
Tree & its Node using ES6 Classes in javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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