-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiceworks.java
More file actions
32 lines (30 loc) · 793 Bytes
/
spiceworks.java
File metadata and controls
32 lines (30 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Node {
public Node firstChild;
public Node nextSibling;
}
public int countDescendents (Node node) {
int numDescendent;
//Declaring a linkedList for the list of nodes
LinkedList<Node> list = new LinkedList<Node>();
//If node has descendants, push it onto the list.
if(node.firstChild != NULL && node != NULL) {
list.push(node.firstChild);
numDescendent++;
}
//If list ist not empty, iterate through it.
while(!list.isEmpty()) {
//Set current node to first on the list
Node b = list.pop();
//Checking if the firstChild is valid
if(b.firstChild != NULL) {
list.push(b.firstChild);
numDescendent++;
}
//Checking if nextSibling is valid
if(b.nextSibling != NULL) {
list.push(b.nextSibling);
numDescendent++;
}
}
return numDescendent;
}