org.psjava.ds.tree.BinaryTreeNodeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of psjava Show documentation
Show all versions of psjava Show documentation
Problem Solving Library for Java
package org.psjava.ds.tree;
import org.psjava.util.AssertStatus;
public class BinaryTreeNodeFactory {
public static BinaryTreeNode create(final T initData) {
return new BinaryTreeNode() {
T data = initData;
BinaryTreeNode left = null, right = null; // null if no child.
@Override
public T getData() {
return data;
}
@Override
public void setData(T data) {
this.data = data;
}
@Override
public boolean hasLeft() {
return left != null;
}
@Override
public boolean hasRight() {
return right != null;
}
@Override
public BinaryTreeNode getLeft() {
AssertStatus.assertTrue(hasLeft());
return left;
}
@Override
public BinaryTreeNode getRight() {
AssertStatus.assertTrue(hasRight());
return right;
}
@Override
public void putLeft(BinaryTreeNode child) {
left = child;
}
@Override
public void putRight(BinaryTreeNode child) {
right = child;
}
@Override
public void removeLeft() {
AssertStatus.assertTrue(hasLeft());
left = null;
}
@Override
public void removeRight() {
AssertStatus.assertTrue(hasRight());
right = null;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy