-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
vol6/src/main/java/ru/mifi/practice/vol6/tree/Centroid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters