Skip to content

Commit b82bd4f

Browse files
Added Tree DS code in JS
1 parent 7a06f72 commit b82bd4f

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

Tree/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// --- Directions
2+
// 1) Create a node class. The constructor
3+
// should accept an argument that gets assigned
4+
// to the data property and initialize an
5+
// empty array for storing children. The node
6+
// class should have methods 'add' and 'remove'.
7+
// 2) Create a tree class. The tree constructor
8+
// should initialize a 'root' property to null.
9+
// 3) Implement 'traverseBF' and 'traverseDF'
10+
// on the tree class. Each method should accept a
11+
// function that gets called with each element in the tree
12+
13+
class Node {
14+
constructor(data) {
15+
this.data = data;
16+
this.children = [];
17+
}
18+
add(data) {
19+
this.children.push(new Node(data));
20+
}
21+
remove(data) {
22+
this.children = this.children.filter((node) => {
23+
return node.data !== data;
24+
});
25+
}
26+
}
27+
class Tree {
28+
constructor() {
29+
this.root = null;
30+
}
31+
}
32+
traverseBF(fn){
33+
const arr=[this.root];
34+
while(arr.length){
35+
const node = arr.shift();
36+
arr.push(...node.children);
37+
fn(node);
38+
}
39+
}
40+
traverseDF(fn){
41+
const arr =[this.root];
42+
while(arr.length){
43+
const node=arr.shift();
44+
arr.unshift(...node.children)
45+
fn(node);
46+
}
47+
}
48+
module.exports = { Tree, Node };

Tree/test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const T = require('./index');
2+
const Node = T.Node;
3+
const Tree = T.Tree;
4+
5+
describe('Node', () => {
6+
test('Node is a constructor', () => {
7+
expect(typeof Node.prototype.constructor).toEqual('function');
8+
});
9+
10+
test('Node has a data and children properties', () => {
11+
const n = new Node('a');
12+
expect(n.data).toEqual('a');
13+
expect(n.children.length).toEqual(0);
14+
});
15+
16+
test('Node can add children', () => {
17+
const n = new Node('a');
18+
n.add('b');
19+
expect(n.children.length).toEqual(1);
20+
expect(n.children[0].children).toEqual([]);
21+
});
22+
23+
test('Node can remove children', () => {
24+
const n = new Node('a');
25+
n.add('b');
26+
expect(n.children.length).toEqual(1);
27+
n.remove('b');
28+
expect(n.children.length).toEqual(0);
29+
});
30+
});
31+
32+
describe.skip('Tree', () => {
33+
test('starts empty', () => {
34+
const t = new Tree();
35+
expect(t.root).toEqual(null);
36+
});
37+
38+
test('Can traverse bf', () => {
39+
const letters = [];
40+
const t = new Tree();
41+
t.root = new Node('a');
42+
t.root.add('b');
43+
t.root.add('c');
44+
t.root.children[0].add('d');
45+
46+
t.traverseBF(node => {
47+
letters.push(node.data);
48+
});
49+
50+
expect(letters).toEqual(['a', 'b', 'c', 'd']);
51+
});
52+
53+
test('Can traverse DF', () => {
54+
const letters = [];
55+
const t = new Tree();
56+
t.root = new Node('a');
57+
t.root.add('b');
58+
t.root.add('d');
59+
t.root.children[0].add('c');
60+
61+
t.traverseDF(node => {
62+
letters.push(node.data);
63+
});
64+
65+
expect(letters).toEqual(['a', 'b', 'c', 'd']);
66+
});
67+
});

0 commit comments

Comments
 (0)