Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#61 Adding some documentation + improving some behaviors #71

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 202 additions & 50 deletions ml2wf/src/main/java/com/ml2wf/constraints/tree/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,65 +30,215 @@
*/
public class BinaryTree<T> {

/**
* Root of the tree.
*/
private T root;
private BinaryTree<T> rightChild;
/**
* Left child of the tree.
*/
private BinaryTree<T> leftChild;
/**
* Right child of the tree.
*/
private BinaryTree<T> rightChild;
/**
* Blocking state of the tree.
*/
private boolean isBlocked;

/**
* {@code BinaryTree}'s full constructor.
*
* @param root root of the tree
* @param leftChild left child of the tree
* @param rightChild right child og the tree
*/
public BinaryTree(T root, BinaryTree<T> leftChild, BinaryTree<T> rightChild) {
this.root = root;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.isBlocked = false;
}

/**
* {@code BinaryTree}'s partial constructor.
*
* <p>
*
* Initializes a new {@code BinaryTree} with {@code null} children.
*
* @param root root of the tree
*/
public BinaryTree(T root) {
this(root, null, null);
}

/**
* {@code BinaryTree}'s empty constructor.
*
* <p>
*
* Initializes a new {@code BinaryTree} with {@code null} root and {@code null}
* children.
*/
public BinaryTree() {
this(null);
}

/**
* Returns the tree's root.
*
* @return the tree's root
*/
public T getRoot() {
return this.root;
}

public void setRoot(T node) {
this.root = node;
/**
* Sets the tree's root.
*
* @param root the new root
*/
public void setRoot(T root) {
this.root = root;
}

public BinaryTree<T> getRightChild() {
return this.rightChild;
/**
* Returns the tree's left child.
*
* @return the tree's left child
*/
public BinaryTree<T> getLeftChild() {
return this.leftChild;
}

public void setRightChild(T rightChild) {
/**
* Sets the tree's left child.
*
* @param leftChild the new tree's left child
*/
public void setLeftChild(T leftChild) {
if (this.isBlocked) {
return; // TODO: raise exception
}
this.rightChild = new BinaryTree<>(rightChild);
this.leftChild = new BinaryTree<>(leftChild);
}

public BinaryTree<T> getLeftChild() {
return this.leftChild;
/**
* Returns the tree's right child.
*
* @return the tree's right child
*/
public BinaryTree<T> getRightChild() {
return this.rightChild;
}

public void setLeftChild(T leftChild) {
/**
* Sets the tree's right child.
*
* @param rightChild the new tree's right child
*/
public void setRightChild(T rightChild) {
if (this.isBlocked) {
return; // TODO: raise exception
}
this.leftChild = new BinaryTree<>(leftChild);
this.rightChild = new BinaryTree<>(rightChild);
}

/**
* Returns whether the current tree is blocked or not.
*
* @return whether the current tree is blocked or not
*/
public boolean isBlocked() {
return this.isBlocked;
}

/**
* Blocks or unblocks the current tree depending on the {@code block} value.
*
* <p>
*
* <b>Note</b> that this method only block the current tree and not its
* children.
*
* @param block the new blocking state
*/
public void setBlocked(boolean block) {
// TODO: check logic (compare to block)
// blocking node but not his children ?
this.isBlocked = block;
}

public BinaryTree<T> addRightChild(BinaryTree<T> child) {
/**
* Blocks of unblocks the current tree.
*
* <p>
*
* <b>Note</b> that blocking the current tree recursively block its children.
*
* @param block the new blocking state
*
* @since 1.0
*/
public void block(boolean block) {
this.isBlocked = block;
this.blockLeftChild(block);
this.blockRightChild(block);
}

/**
* Blocks of unblocks the tree's left child.
*
* <p>
*
* If left child is {@code null} and {@code block} is true, then an empty
* {@code BinaryTree} left child is instantiated and blocked.
*
* @param block the new blocking state
*
* @since 1.0
*/
public void blockLeftChild(boolean block) {
if (this.leftChild == null) {
if (!block) {
return;
} else {
this.leftChild = new BinaryTree<>(); // TODO: reuse creating methods
}
}
this.leftChild.setBlocked(block);
}

/**
* Blocks of unblocks the tree's right child.
*
* <p>
*
* If left child is {@code null} and {@code block} is true, then an empty
* {@code BinaryTree} right child is instantiated and blocked.
*
* @param block the new blocking state
*
* @since 1.0
*/
public void blockRightChild(boolean block) {
if (this.rightChild == null) {
if (!block) {
return;
} else {
this.rightChild = new BinaryTree<>(); // TODO: reuse creating methods
}
}
this.rightChild.setBlocked(block);
}

/**
* TODO: behave differently than {@link #addRightChild(BinaryTree)}.
* TODO: improve this behavior
*/
public BinaryTree<T> addLeftChild(BinaryTree<T> child) {
if (this.isBlocked) {
return null; // TODO: raise exception
}
Expand All @@ -99,26 +249,23 @@ public BinaryTree<T> addRightChild(BinaryTree<T> child) {
this.rightChild = child.getRightChild();
return this;
} else {
if (this.rightChild != null) {
return this.rightChild.addRightChild(child);
if (this.leftChild != null) {
return this.rightChild.addLeftChild(child);
} else {
this.rightChild = child;
return this.rightChild;
this.leftChild = child;
return this.leftChild;
}
}
}

public BinaryTree<T> addRightChild(T child) {
public BinaryTree<T> addLeftChild(T child) {
if (this.isBlocked) {
return null; // TODO: raise exception
}
return this.addRightChild(new BinaryTree<>(child));
return this.addLeftChild(new BinaryTree<>(child));
}

/**
* TODO: behave differently than {@link #addRightChild(BinaryTree)}.
*/
public BinaryTree<T> addLeftChild(BinaryTree<T> child) {
public BinaryTree<T> addRightChild(BinaryTree<T> child) {
if (this.isBlocked) {
return null; // TODO: raise exception
}
Expand All @@ -129,22 +276,48 @@ public BinaryTree<T> addLeftChild(BinaryTree<T> child) {
this.rightChild = child.getRightChild();
return this;
} else {
if (this.leftChild != null) {
return this.rightChild.addLeftChild(child);
if (this.rightChild != null) {
return this.rightChild.addRightChild(child);
} else {
this.leftChild = child;
return this.leftChild;
this.rightChild = child;
return this.rightChild;
}
}
}

public BinaryTree<T> addLeftChild(T child) {
public BinaryTree<T> addRightChild(T child) {
if (this.isBlocked) {
return null; // TODO: raise exception
}
return this.addLeftChild(new BinaryTree<>(child));
return this.addRightChild(new BinaryTree<>(child));
}

/**
* Inserts the given {@code tree} when it is possible.
*
* <p>
*
* Four different behaviors can be observed,
*
* <p>
*
* <ul>
* <li>if the tree is empty, then the current tree is replaced by the given
* {@code tree}</li>
* <li>else if the left child is empty, then the current left child becomes the
* given {@code tree}</li>
* <li>else if the right child is empty, then the current right child becomes
* the given {@code tree}</li>
* <li>else recursively inserts</li>
* </ul>
*
* <p>
*
* <b>Note</b> that an insertion can fail if the tree is blocked.
*
* @param tree tree to insert
* @return whether the insertion succeed or not
*/
public boolean insertWhenPossible(BinaryTree<T> tree) {
if (this.isBlocked) {
return false;
Expand All @@ -170,26 +343,6 @@ public boolean insertWhenPossible(BinaryTree<T> tree) {
}
}

public void block(boolean block) {
this.isBlocked = block;
this.blockLeftChild(block);
this.blockRightChild(block);
}

public void blockLeftChild(boolean block) {
if (this.leftChild == null) {
this.leftChild = new BinaryTree<>(); // TODO: reuse creating methods
}
this.leftChild.setBlocked(block);
}

public void blockRightChild(boolean block) {
if (this.rightChild == null) {
this.rightChild = new BinaryTree<>(); // TODO: reuse creating methods
}
this.rightChild.setBlocked(block);
}

/**
* Returns whether the current tree has children or not.
*
Expand Down Expand Up @@ -255,7 +408,7 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
// TODO: reduce
// TODO: replace by a simpler method
if (this == obj) {
return true;
}
Expand Down Expand Up @@ -292,7 +445,6 @@ public boolean equals(Object obj) {

@Override
public String toString() {
// TODO: to check
StringBuilder builder = new StringBuilder();
builder.append(" ");
if (this.hasLeftChild()) {
Expand Down