generic binary search tree java code example
Example: generic binary search tree java
public class BinaryTree> {
TreeNode root;
public BinaryTree() {
super();
this.root = new TreeNode();
}
public BinaryTree(T o) {
super();
// TODO Auto-generated constructor stub
TreeNode node = new TreeNode(o);
this.root = node;
}
public void add(T o) {
if (root == null) {
root = new TreeNode(o);
} else {
root.insert(o);
}
}
public boolean isMember (TreeNode node, T o) {
if(node == null) return false;
if (node.element.compareTo(o) == 0) {
return true;
} else if (node.element.compareTo(o) < 0) {
isMember(node.right, o);
} else {
isMember(node.left, o);
}
return false;
}
public void preOrderHelper (TreeNode node) {
if (node != null) {
node.visit();
preOrderHelper(node.left);
preOrderHelper(node.right);
}
}
public void inOrderHelper(TreeNode node) {
if (node != null) {
inOrderHelper(node.left);
node.visit();
inOrderHelper(node.right);
}
}
public void postOrderHelper(TreeNode node) {
if (node != null) {
postOrderHelper(node.left);
postOrderHelper(node.right);
node.visit();
}
}
public void preOrder() {
if (root != null) {
preOrderHelper(root);
}
}
public void inOrder() {
if (root != null) {
inOrderHelper(root);
}
}
public void postOrder() {
if (root != null) {
postOrderHelper(root);
}
}
}