Skip to content

Commit

Permalink
Деревья(Центроид)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Dec 6, 2024
1 parent 8e1d02c commit 8074568
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
62 changes: 62 additions & 0 deletions vol6/src/main/java/ru/mifi/practice/vol6/tree/Centroid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.mifi.practice.vol6.tree;

import ru.mifi.practice.vol6.tree.visitors.Count;

@SuppressWarnings("PMD.SingularField")
public final class Centroid<T> implements Visitor<T> {
private Node<T> centroid = null;
private int vertexes = 0;

private static <T> int nodeCount(Node<T> node, Count<T> count, VisitorStrategy.PreOrder<T> strategy) {
count.clear();
if (node == null) {
return 0;
}
node.visit(count, strategy);
return count.count();
}

private static <T> int nodeUpper(Node<T> node, Count<T> count, VisitorStrategy.PreOrder<T> strategy) {
count.clear();
VisitorStrategy.AlreadyVisited<T> alreadyVisited = new VisitorStrategy.AlreadyVisited<>(strategy);
alreadyVisited.put(node);
Node<T> it = node.parent();
while (it != null) {
it.visit(count, alreadyVisited);
it = it.parent();
}
return count.count();
}

public Node<T> centroid(Tree<T> tree) {
centroid = null;
VisitorStrategy.PreOrder<T> strategy = new VisitorStrategy.PreOrder<>();
Count<T> count = new Count<>();
tree.visit(count, strategy);
vertexes = count.count();
tree.visit(this, strategy);
return centroid;
}

@Override
public void enterNode(Node<T> node) {
VisitorStrategy.PreOrder<T> strategy = new VisitorStrategy.PreOrder<>();
Count<T> count = new Count<>();
var upper = nodeUpper(node, count, strategy);
var left = nodeCount(node.left(), count, strategy);
var right = nodeCount(node.right(), count, strategy);
if (left <= vertexes / 2 && right <= vertexes / 2 && upper <= vertexes / 2) {
centroid = node;
}
}

@Override
public void exitNode(Node<T> node) {
//None
}

@Override
public void empty() {
//None
}
}
4 changes: 4 additions & 0 deletions vol6/src/main/java/ru/mifi/practice/vol6/tree/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ public static void main(String[] args) throws IOException {
List<Node<Integer>> centered = center.center(tree, integer -> integer);
System.out.print("CENTR: ");
System.out.println(centered);
Centroid<Integer> centroid = new Centroid<>();
var c = centroid.centroid(tree);
System.out.print("CEOID: ");
System.out.println(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public void visit(Node<T> node, Visitor<T> visitor, VisitorStrategy<T> strategy)
}
visited.add(node);
visitor.enterNode(node);
this.strategy.visit(node.left(), visitor, strategy);
this.strategy.visit(node.right(), visitor, strategy);
strategy.visit(node.left(), visitor, this.strategy);
strategy.visit(node.right(), visitor, this.strategy);
visitor.exitNode(node);
}

public void put(Node<T> node) {
visited.add(node);
}
}

final class PreOrder<T> implements VisitorStrategy<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public void exitNode(Node<T> node) {
public void empty() {
//None
}

@Override
public String toString() {
return String.valueOf(count.intValue());
}
}

0 comments on commit 8074568

Please sign in to comment.