-
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
11 changed files
with
320 additions
and
22 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
vol6/src/main/java/ru/mifi/practice/vol6/tree/Diameter.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,46 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import ru.mifi.practice.vol6.tree.paths.LowestCommonAncestorPath; | ||
import ru.mifi.practice.vol6.tree.visitors.Distance; | ||
|
||
import java.util.List; | ||
|
||
public final class Diameter<T> { | ||
|
||
public List<Node<T>> path(Tree<T> tree) { | ||
VisitorStrategy.PreOrder<T> strategy = new VisitorStrategy.PreOrder<>(); | ||
Distance<T> vDistance = new Distance<>(); | ||
tree.visit(vDistance, strategy); | ||
int max = 0; | ||
Node<T> maxNode = null; | ||
for (var entry : vDistance.distances().entrySet()) { | ||
if (max < entry.getValue()) { | ||
max = entry.getValue(); | ||
maxNode = entry.getKey(); | ||
} | ||
} | ||
if (maxNode != null) { | ||
VisitorStrategy.AlreadyVisited<T> alreadyVisited = new VisitorStrategy.AlreadyVisited<>(strategy); | ||
Node<T> start = maxNode; | ||
Node<T> it = maxNode; | ||
int distance = 0; | ||
vDistance.clear(); | ||
while (it != null) { | ||
vDistance.resetLevel(distance); | ||
it.visit(vDistance, alreadyVisited); | ||
++distance; | ||
it = it.parent(); | ||
} | ||
max = 0; | ||
for (var entry : vDistance.distances().entrySet()) { | ||
if (max < entry.getValue()) { | ||
max = entry.getValue(); | ||
maxNode = entry.getKey(); | ||
} | ||
} | ||
Node<T> end = maxNode; | ||
return new LowestCommonAncestorPath<T>().path(tree, start.value(), end.value()); | ||
} | ||
return List.of(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import java.util.List; | ||
|
||
public interface Path<T> { | ||
List<Node<T>> path(Tree<T> tree, T start, T end); | ||
} |
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
24 changes: 24 additions & 0 deletions
24
vol6/src/main/java/ru/mifi/practice/vol6/tree/VisitorStrategy.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
45 changes: 45 additions & 0 deletions
45
vol6/src/main/java/ru/mifi/practice/vol6/tree/paths/LowestCommonAncestorPath.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,45 @@ | ||
package ru.mifi.practice.vol6.tree.paths; | ||
|
||
import ru.mifi.practice.vol6.tree.Node; | ||
import ru.mifi.practice.vol6.tree.Path; | ||
import ru.mifi.practice.vol6.tree.Tree; | ||
import ru.mifi.practice.vol6.tree.VisitorStrategy; | ||
import ru.mifi.practice.vol6.tree.visitors.Distance; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class LowestCommonAncestorPath<T> implements Path<T> { | ||
@Override | ||
public List<Node<T>> path(Tree<T> tree, T start, T end) { | ||
Distance<T> vDistance = new Distance<>(); | ||
tree.visit(vDistance, new VisitorStrategy.PreOrder<>()); | ||
Map<Node<T>, Integer> distances = vDistance.distances(); | ||
var nStart = tree.find(start); | ||
var nStartIt = nStart; | ||
var nEnd = tree.find(end); | ||
var nEndIt = nEnd; | ||
int hStart = distances.get(nStart); | ||
int hEnd = distances.get(nEnd); | ||
while (hStart != hEnd) { | ||
if (hStart > hEnd) { | ||
nStartIt = nStartIt.parent(); | ||
hStart -= 1; | ||
} else { | ||
nEndIt = nEndIt.parent(); | ||
hEnd -= 1; | ||
} | ||
} | ||
while (!nStartIt.equals(nEndIt)) { | ||
nStartIt = nStartIt.parent(); | ||
nEndIt = nEndIt.parent(); | ||
} | ||
var lca = nStartIt; | ||
List<Node<T>> path = lca.path(start); | ||
Collections.reverse(path); | ||
path.remove(path.size() - 1); | ||
path.addAll(lca.path(end)); | ||
return path; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
vol6/src/main/java/ru/mifi/practice/vol6/tree/paths/MergePath.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,32 @@ | ||
package ru.mifi.practice.vol6.tree.paths; | ||
|
||
import ru.mifi.practice.vol6.tree.Node; | ||
import ru.mifi.practice.vol6.tree.Path; | ||
import ru.mifi.practice.vol6.tree.Tree; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public final class MergePath<T> implements Path<T> { | ||
|
||
@Override | ||
public List<Node<T>> path(Tree<T> tree, T start, T end) { | ||
Node<T> it = tree.find(start); | ||
Node<T> nEnd = it.search(end); | ||
if (nEnd != null) { | ||
return it.path(end); | ||
} | ||
while (it != null) { | ||
List<Node<T>> path = it.path(end); | ||
if (!path.isEmpty()) { | ||
List<Node<T>> result = it.path(start); | ||
Collections.reverse(result); | ||
result.remove(result.size() - 1); | ||
result.addAll(path); | ||
return result; | ||
} | ||
it = it.parent(); | ||
} | ||
return List.of(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
vol6/src/main/java/ru/mifi/practice/vol6/tree/visitors/Distance.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,42 @@ | ||
package ru.mifi.practice.vol6.tree.visitors; | ||
|
||
import ru.mifi.practice.vol6.tree.Node; | ||
import ru.mifi.practice.vol6.tree.Visitor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class Distance<T> implements Visitor<T> { | ||
private final AtomicInteger level = new AtomicInteger(0); | ||
private final Map<Node<T>, Integer> distances = new HashMap<>(); | ||
|
||
public void resetLevel(int level) { | ||
this.level.set(level); | ||
} | ||
|
||
public void clear() { | ||
distances.clear(); | ||
resetLevel(0); | ||
} | ||
|
||
public Map<Node<T>, Integer> distances() { | ||
return distances; | ||
} | ||
|
||
@Override | ||
public void enterNode(Node<T> node) { | ||
int incremented = level.getAndIncrement(); | ||
distances.put(node, incremented); | ||
} | ||
|
||
@Override | ||
public void exitNode(Node<T> node) { | ||
level.decrementAndGet(); | ||
} | ||
|
||
@Override | ||
public void empty() { | ||
|
||
} | ||
} |
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
Oops, something went wrong.