Skip to content

Commit aab079f

Browse files
committed
adding data structure tree
1 parent 22608c3 commit aab079f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

tree/directions.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# --- Directions
2+
3+
* 1) Create a node class. The constructor
4+
should accept an argument that gets assigned
5+
to the data property and initialize an
6+
empty array for storing children. The node
7+
class should have methods 'add' and 'remove'.
8+
9+
* 2) Create a tree class. The tree constructor
10+
should initialize a 'root' property to null.
11+
12+
* 3) Implement 'traverseBF' and 'traverseDF'
13+
on the tree class. Each method should accept a
14+
function that gets called with each element in the tree
15+
16+

tree/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
14+
class Node {
15+
constructor(data) {
16+
this.data = data;
17+
this.children = [];
18+
}
19+
20+
add(data) {
21+
this.children.push(new Node(data));
22+
}
23+
24+
remove(data) {
25+
this.children = this.children.filter( node => {
26+
return node.data !== data;
27+
});
28+
}
29+
}

0 commit comments

Comments
 (0)