Skip to content

Commit

Permalink
Ancestors function for the GSS
Browse files Browse the repository at this point in the history
  • Loading branch information
IOANNVOLZHSKIY committed Jan 21, 2024
1 parent 1734df3 commit a9a1231
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
23 changes: 23 additions & 0 deletions lab5/src/classes/GSSNode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ class _GSSNode<T> {
//return this.prev.values.toSet();
return this.prev.values.toSet().map((e) => e as GSSNode<T>).toSet();
}

Set<_GSSNode<T>> ancestors(int k) {
Set<_GSSNode<T>> result = {};
_getAncestorsRecursive(this, k, result);
result.remove(this);
return result;
}

void _getAncestorsRecursive(_GSSNode<T> currentNode, int k, Set<_GSSNode<T>> result) {
if (k < 0) {
return;
}

result.add(currentNode);

if (currentNode.level == 0) {
return;
}

for (final prevNode in currentNode.prev.values) {
_getAncestorsRecursive(prevNode, k - 1, result);
}
}
}

class GSSNode<T> extends _GSSNode<T> {
Expand Down
16 changes: 5 additions & 11 deletions lab5/src/tests/GSS_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void main() {

// {7,4,1,0}
nodes[4] = stack.push(4, nodes[1]);
nodes[7] = stack.push(
7, nodes[4]); // 7 isn't duplicated, as it ends up in the same layer
nodes[7] = stack.push(7, nodes[4]); // 7 isn't duplicated, as it ends up in the same layer

// {7,5,2,0}
nodes[2] = stack.push(2, nodes[0]);
Expand All @@ -31,17 +30,12 @@ void main() {
nodes[6] = stack.push(6, nodes[2]);
nodes[8] = stack.push(8, nodes[6]);

// Получаем все предыдущие узлы для node3
List<GSSNode<int>> allPreviousNodes =
stack.getPreviousNodesFromNode(nodes[8]!);
var result = nodes[7]?.ancestors(1);
print(result);

// Выводим информацию о всех предыдущих узлах
print("Предыдущие узлы для nodes:");
for (final prevNode in allPreviousNodes) {
print("| ${prevNode.toString()} |");
for (final ancestor in result!) {
print(ancestor.value);
}

//stack.GSStoDot();
});

test('has the right degrees: prev', () {
Expand Down

0 comments on commit a9a1231

Please sign in to comment.