All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.psjava.ds.tree.BinaryTreeNodeFactory Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
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