Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- constructor(data) {
- this.data = data;
- this.children = [];
- }
- add(data) {
- this.children.push(new Node(data));
- }
- remove(data) {
- this.children = this.children.filter(node => node.data !== data);
- }
- }
- class Tree {
- constructor() {
- this.root = null;
- }
- traverseBF(fn) {
- const arr = [this.root];
- while (arr.length) {
- const node = arr.shift();
- arr.push(...node.children);
- fn(node);
- }
- }
- traverseDF(fn) {
- const arr = [this.root];
- while (arr.length) {
- const node = arr.shift();
- arr.unshift(...node.children);
- fn(node);
- }
- }
- }
- // Implementation
- // const node = new Node(1);
- // const tree = new Tree();
- // tree.root = node;
- module.exports = { Tree, Node };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement