-
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
12 changed files
with
172 additions
and
12 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,71 @@ | ||
package ru.mifi.practice.vol6.tree; | ||
|
||
import ru.mifi.practice.vol6.tree.visitors.Count; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Почему реализация не правильная? | ||
* @param <T> тип значения в вершине дерева | ||
*/ | ||
public final class Center<T> { | ||
public List<Node<T>> center(Tree<T> tree, Function<? super T, ? extends T> copyFunction) { | ||
tree = tree.copy(copyFunction); | ||
List<Node<T>> result = new ArrayList<>(); | ||
Set<Node<T>> leafs = new HashSet<>(); | ||
VisitorStrategy.PreOrder<T> strategy = new VisitorStrategy.PreOrder<>(); | ||
Visitor<T> search = new Visitor<>() { | ||
@Override | ||
public void enterNode(Node<T> node) { | ||
if (node.left() == null && node.right() == null) { | ||
leafs.add(node); | ||
} | ||
} | ||
|
||
@Override | ||
public void exitNode(Node<T> node) { | ||
//None | ||
} | ||
|
||
@Override | ||
public void empty() { | ||
//None | ||
} | ||
}; | ||
Count<T> count = new Count<>(); | ||
while (true) { | ||
count.clear(); | ||
tree.visit(count, strategy); | ||
if (count.count() <= 2) { | ||
break; | ||
} | ||
leafs.clear(); | ||
tree.visit(search, strategy); | ||
for (Node<T> leaf : leafs) { | ||
tree.delete(leaf.value()); | ||
} | ||
} | ||
tree.visit(new Visitor<T>() { | ||
|
||
@Override | ||
public void enterNode(Node<T> node) { | ||
result.add(node); | ||
} | ||
|
||
@Override | ||
public void exitNode(Node<T> node) { | ||
//None | ||
} | ||
|
||
@Override | ||
public void empty() { | ||
//None | ||
} | ||
}, strategy); | ||
return result; | ||
} | ||
} |
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
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
33 changes: 33 additions & 0 deletions
33
vol6/src/main/java/ru/mifi/practice/vol6/tree/visitors/Count.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,33 @@ | ||
package ru.mifi.practice.vol6.tree.visitors; | ||
|
||
import ru.mifi.practice.vol6.tree.Node; | ||
import ru.mifi.practice.vol6.tree.Visitor; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class Count<T> implements Visitor<T> { | ||
private final AtomicInteger count = new AtomicInteger(0); | ||
|
||
public void clear() { | ||
count.set(0); | ||
} | ||
|
||
public int count() { | ||
return count.get(); | ||
} | ||
|
||
@Override | ||
public void enterNode(Node<T> node) { | ||
count.incrementAndGet(); | ||
} | ||
|
||
@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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,6 @@ public void exitNode(Node<T> node) { | |
|
||
@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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ public List<Node<T>> path() { | |
|
||
@Override | ||
public void empty() { | ||
|
||
//None | ||
} | ||
|
||
@Override | ||
|
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 |
---|---|---|
|
@@ -65,7 +65,7 @@ public List<Node<T>> times() { | |
|
||
@Override | ||
public void empty() { | ||
|
||
//None | ||
} | ||
|
||
@Override | ||
|
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 |
---|---|---|
|
@@ -33,6 +33,6 @@ public void exitNode(Node<T> node) { | |
|
||
@Override | ||
public void empty() { | ||
|
||
//None | ||
} | ||
} |