Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bee4fa

Browse files
author
Swastikyadav
committed
Create a node class for trees dataStructure
1 parent 805a41c commit 0bee4fa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎DsAlgo-Questions/22-trees.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
1. Create a node class. The constuctor should accept an argument that gets assigned to the data property and initialize an empty array for storing children. The node class should have methods 'add' and 'remove'.
3+
4+
2. Create a tree class. The tree constuctor should initialize a 'root' property to null.
5+
6+
3. Implement 'traverseBFS' and 'traverseDFS' on the trees class.
7+
*/
8+
9+
class Node {
10+
constructor(data) {
11+
this.data = data;
12+
this.children = [];
13+
}
14+
15+
// Add a new node to the children of current node.
16+
add(data) {
17+
this.children.push(new Node(data));
18+
}
19+
20+
// Remove a data from the children of current node.
21+
remove(data) {
22+
const filteredChildren = this.children.filter(
23+
node => node.data !== data
24+
);
25+
26+
this.children = filteredChildren;
27+
}
28+
};
29+
30+
class Tree {};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /