-
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
8 changed files
with
241 additions
and
7 deletions.
There are no files selected for viewing
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,26 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import ru.mifi.practice.vol6.tree.visitors.OnSubTree; | ||
|
||
import java.io.IOException; | ||
import java.util.Comparator; | ||
|
||
public abstract class Main { | ||
public static void main(String[] args) throws IOException { | ||
Tree<Integer> tree = new ParserText<Integer>().parse(Main.class.getResourceAsStream("/standard.tree"), | ||
Integer::parseInt, Comparator.comparing(k -> k)); | ||
System.out.print("PRE : "); | ||
tree.visit(new Visitor.Stdout<>(), new VisitorStrategy.PreOrder<>()); | ||
System.out.println(); | ||
System.out.print("POST: "); | ||
tree.visit(new Visitor.Stdout<>(), new VisitorStrategy.PostOrder<>()); | ||
System.out.println(); | ||
System.out.print("IN : "); | ||
tree.visit(new Visitor.Stdout<>(), new VisitorStrategy.InOrder<>()); | ||
System.out.println(); | ||
OnSubTree<Integer> on = new OnSubTree<>(); | ||
tree.visit(on, new VisitorStrategy.PreOrder<>()); | ||
System.out.println(on); | ||
System.out.println(tree); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
vol6/src/main/java/ru/mifi/practice/vol6/tree/ParserText.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,48 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Comparator; | ||
import java.util.function.Function; | ||
|
||
@SuppressWarnings({"PMD.EmptyControlStatement", "PMD.CompareObjectsWithEquals"}) | ||
public final class ParserText<T> implements Tree.Loader<T> { | ||
|
||
public Tree<T> parse(String text, Function<String, T> value, Comparator<T> comparator) throws IOException { | ||
return parse(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)), value, comparator); | ||
} | ||
|
||
@Override | ||
public Tree<T> parse(InputStream stream, Function<String, T> value, Comparator<T> comparator) throws IOException { | ||
Tree.Standard<T> tree = new Tree.Standard<>(comparator); | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
line = line.trim(); | ||
if (line.isEmpty()) { | ||
continue; | ||
} | ||
String[] parts = line.split(":"); | ||
String owner = parts[0].trim(); | ||
String left = ""; | ||
String right = ""; | ||
String description = parts[1].trim(); | ||
if (description.isEmpty()) { | ||
continue; | ||
} else if (description.charAt(0) == '{' && description.charAt(description.length() - 1) == '}') { | ||
description = description.substring(1, description.length() - 1); | ||
parts = description.split(","); | ||
left = parts[0].trim(); | ||
right = parts[1].trim(); | ||
} | ||
tree.add(value.apply(owner), | ||
left.isEmpty() ? null : value.apply(left), right.isEmpty() ? null : value.apply(right)); | ||
} | ||
} | ||
return tree; | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
vol6/src/main/java/ru/mifi/practice/vol6/tree/visitors/OnSubTree.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,77 @@ | ||
package ru.mifi.practice.vol6.tree.visitors; | ||
|
||
import ru.mifi.practice.vol6.tree.Node; | ||
import ru.mifi.practice.vol6.tree.Visitor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class OnSubTree<T> implements Visitor<T> { | ||
private final Map<Node<T>, Integer> stepsIn = new HashMap<>(); | ||
private final Map<Node<T>, Integer> stepsOut = new HashMap<>(); | ||
private final AtomicInteger steps = new AtomicInteger(); | ||
|
||
void clear() { | ||
stepsIn.clear(); | ||
stepsOut.clear(); | ||
steps.set(0); | ||
} | ||
|
||
@Override | ||
public void enterNode(Node<T> node) { | ||
if (node != null) { | ||
int step = steps.incrementAndGet(); | ||
stepsIn.put(node, step); | ||
} | ||
} | ||
|
||
@Override | ||
public void exitNode(Node<T> node) { | ||
if (node != null) { | ||
stepsOut.put(node, steps.incrementAndGet()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("checked") | ||
public List<Node<T>> times() { | ||
List<Node<T>> result = new ArrayList<>(); | ||
Node[] nodes = new Node[stepsIn.size() * 2 + 1]; | ||
for (var key : stepsIn.keySet()) { | ||
int in = stepsIn.get(key); | ||
int out = stepsOut.get(key); | ||
nodes[in] = key; | ||
nodes[out] = key; | ||
} | ||
for (Node node : nodes) { | ||
if (node == null) { | ||
continue; | ||
} | ||
result.add(node); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
@Override | ||
public void empty() { | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
StringBuilder fl = new StringBuilder(); | ||
StringBuilder sl = new StringBuilder(); | ||
List<Node<T>> times = times(); | ||
for (int i = 0; i < times.size(); i++) { | ||
Node<T> node = times.get(i); | ||
fl.append(String.format("%2d ", i)); | ||
sl.append(String.format("%2s ", node.value())); | ||
} | ||
sb.append(fl).append("\n").append(sl); | ||
return sb.toString(); | ||
} | ||
} |
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,6 @@ | ||
1:{ 2, 3} | ||
3:{ 4, 5} | ||
4:{ 9, } | ||
9:{11, 10} | ||
5:{ 6, 7} | ||
7:{ 8, } |