Skip to content

Commit

Permalink
Дерево
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Dec 5, 2024
1 parent 756d838 commit 0a1f9e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions vol6/src/main/java/ru/mifi/practice/vol6/tree/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.mifi.practice.vol6.tree;

import ru.mifi.practice.vol6.tree.visitors.EulerPath;
import ru.mifi.practice.vol6.tree.visitors.OnSubTree;

import java.io.IOException;
Expand All @@ -9,18 +10,23 @@ 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 : ");
System.out.print("PRE : ");
tree.visit(new Visitor.Stdout<>(), new VisitorStrategy.PreOrder<>());
System.out.println();
System.out.print("POST: ");
System.out.print("POST : ");
tree.visit(new Visitor.Stdout<>(), new VisitorStrategy.PostOrder<>());
System.out.println();
System.out.print("IN : ");
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("TIMED: ");
System.out.println(on);
EulerPath<Integer> euler = new EulerPath<>();
System.out.print("EULER: ");
tree.visit(euler, new VisitorStrategy.PreOrder<>());
System.out.println(euler);
System.out.println(tree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.List;

public final class EulerPath<T> implements Visitor<T> {
private final List<Node<T>> nodes = new ArrayList<>();

void clear() {
nodes.clear();
}

@Override
public void enterNode(Node<T> node) {
if (node != null) {
nodes.add(node);
}
}

@Override
public void exitNode(Node<T> node) {
if (node != null) {
nodes.add(node);
}
}

@SuppressWarnings("checked")
public List<Node<T>> path() {
return nodes;
}


@Override
public void empty() {

}

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

0 comments on commit 0a1f9e6

Please sign in to comment.