File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments