-
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
5 changed files
with
192 additions
and
0 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
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,84 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import lombok.EqualsAndHashCode; | ||
|
||
public interface Node<T> extends Visitor.Visit<T> { | ||
static <T> Node<T> root(T value) { | ||
return new Default<>(null, value); | ||
} | ||
|
||
T value(); | ||
|
||
Node<T> left(); | ||
|
||
Node<T> left(T value); | ||
|
||
Node<T> right(); | ||
|
||
Node<T> right(T value); | ||
|
||
|
||
Node<T> deleteLeft(T value); | ||
|
||
Node<T> deleteRight(T value); | ||
|
||
@Override | ||
void visit(Visitor<T> visitor, VisitorStrategy<T> strategy); | ||
|
||
@EqualsAndHashCode(of = "value") | ||
final class Default<T> implements Node<T> { | ||
private final T value; | ||
private final Node<T> parent; | ||
private Node<T> left; | ||
private Node<T> right; | ||
|
||
private Default(Node<T> parent, T value) { | ||
this.value = value; | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public T value() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public Node<T> left() { | ||
return left; | ||
} | ||
|
||
@Override | ||
public Node<T> left(T value) { | ||
return left = new Default<>(this, value); | ||
} | ||
|
||
@Override | ||
public Node<T> right() { | ||
return right; | ||
} | ||
|
||
@Override | ||
public Node<T> right(T value) { | ||
return right = new Default<>(this, value); | ||
} | ||
|
||
@Override | ||
public Node<T> deleteLeft(T value) { | ||
Node<T> node = left; | ||
left = null; | ||
return node; | ||
} | ||
|
||
@Override | ||
public Node<T> deleteRight(T value) { | ||
Node<T> node = right; | ||
right = null; | ||
return node; | ||
} | ||
|
||
@Override | ||
public void visit(Visitor<T> visitor, VisitorStrategy<T> strategy) { | ||
strategy.visit(this, visitor, strategy); | ||
} | ||
} | ||
} |
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,36 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import java.util.Comparator; | ||
|
||
public interface Tree<T> extends Visitor.Visit<T> { | ||
|
||
void add(T element); | ||
|
||
@Override | ||
void visit(Visitor<T> visitor, VisitorStrategy<T> strategy); | ||
|
||
abstract class AbstractTree<T> implements Tree<T> { | ||
protected final Comparator<T> comparator; | ||
protected Node<T> root; | ||
|
||
protected AbstractTree(Comparator<T> comparator) { | ||
this.comparator = comparator; | ||
} | ||
|
||
@Override | ||
public void add(T element) { | ||
root = root == null ? Node.root(element) : add(root, element); | ||
} | ||
|
||
abstract Node<T> add(Node<T> root, T element); | ||
|
||
@Override | ||
public void visit(Visitor<T> visitor, VisitorStrategy<T> strategy) { | ||
if (root != null) { | ||
strategy.visit(root, visitor, strategy); | ||
} else { | ||
visitor.empty(); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
vol6/src/main/java/ru/mifi/practice/vol6/tree/Visitor.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,25 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
public interface Visitor<T> { | ||
void visit(Node<T> node); | ||
|
||
void empty(); | ||
|
||
@FunctionalInterface | ||
interface Visit<T> { | ||
void visit(Visitor<T> visitor, VisitorStrategy<T> strategy); | ||
} | ||
|
||
final class Stdout<T> implements Visitor<T> { | ||
|
||
@Override | ||
public void visit(Node<T> node) { | ||
System.out.println(node); | ||
} | ||
|
||
@Override | ||
public void empty() { | ||
System.out.println("empty"); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
@FunctionalInterface | ||
public interface VisitorStrategy<T> { | ||
void visit(Node<T> node, Visitor<T> visitor, VisitorStrategy<T> strategy); | ||
|
||
final class PreOrder<T> implements VisitorStrategy<T> { | ||
@Override | ||
public void visit(Node<T> node, Visitor<T> visitor, VisitorStrategy<T> strategy) { | ||
if (node == null) { | ||
return; | ||
} | ||
visitor.visit(node); | ||
strategy.visit(node.left(), visitor, strategy); | ||
strategy.visit(node.right(), visitor, strategy); | ||
} | ||
} | ||
|
||
final class PostOrder<T> implements VisitorStrategy<T> { | ||
@Override | ||
public void visit(Node<T> node, Visitor<T> visitor, VisitorStrategy<T> strategy) { | ||
if (node == null) { | ||
return; | ||
} | ||
strategy.visit(node.left(), visitor, strategy); | ||
strategy.visit(node.right(), visitor, strategy); | ||
visitor.visit(node); | ||
} | ||
} | ||
|
||
final class InOrder<T> implements VisitorStrategy<T> { | ||
@Override | ||
public void visit(Node<T> node, Visitor<T> visitor, VisitorStrategy<T> strategy) { | ||
if (node == null) { | ||
return; | ||
} | ||
strategy.visit(node.left(), visitor, strategy); | ||
visitor.visit(node); | ||
strategy.visit(node.right(), visitor, strategy); | ||
} | ||
} | ||
} |