Node: remove{Child,Parent}(): split trees branches apart
removeChild(node): removes the node from children of the current node, making the child node into a new root node. removeParent(): removes the current from from it's parent's children, making itself into a new root node.
This commit is contained in:
parent
b2c3032ec0
commit
9fca56b4c6
2 changed files with 31 additions and 0 deletions
|
@ -162,4 +162,15 @@ public interface Node<T> {
|
|||
*/
|
||||
boolean isNamed();
|
||||
|
||||
/**
|
||||
* Remove the node from the children.
|
||||
*
|
||||
* @param node the node to be removed
|
||||
*/
|
||||
void removeChild(Node<T> node);
|
||||
|
||||
/**
|
||||
* Removes the parent from the node. Makes the node into a new root node.
|
||||
*/
|
||||
void removeParent();
|
||||
}
|
||||
|
|
|
@ -305,6 +305,26 @@ public class NodeItem<T> implements Node<T> {
|
|||
throw new NodeException("Named child not found");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeChild(final Node<T> node) {
|
||||
if (children.remove(node)) {
|
||||
node.removeParent();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeParent() {
|
||||
if (parent != null) {
|
||||
parent.removeChild(this);
|
||||
parent = null;
|
||||
if (nameSupplier == null) {
|
||||
// this is now a root node, so must provide a default name
|
||||
// supplier
|
||||
nameSupplier = n -> null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String drawTree(final int depth) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
|
Loading…
Reference in a new issue