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

org.psjava.ds.tree.binary.BinaryTreeToString Maven / Gradle / Ivy

The newest version!
package org.psjava.ds.tree.binary;

public class BinaryTreeToString {

	public static  String toString(BinaryTreeNodeWithParent rootOrNull) {
		if (rootOrNull == null)
			return "Empty";
		else
			return toStringRecursively(rootOrNull);
	}

	private static  String toStringRecursively(BinaryTreeNodeWithParent root) {
		String r = root.getData().toString();
		if (root.hasLeft() || root.hasRight()) {
			r += "(";
			if (root.hasLeft())
				r += toStringRecursively(root.getLeft());
			r += ",";
			if (root.hasRight())
				r += toStringRecursively(root.getRight());
			r += ")";
		}
		return r;
	}

	private BinaryTreeToString() {
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy