binary search tree implementation in java code example

Example 1: java binary tree

public class BinaryTree {

    private int key;

    private BinaryTree left, right;

    /**
     * Simple constructor.
     *
     * @param key
     *     to set as key.
     */
    public BinaryTree(int key) {
        this.key = key;
    }

    /**
     * Extended constructor.
     *
     * @param key
     *     to set as key.
     * @param left
     *     to set as left child.
     * @param right
     *     to set as right child.
     */
    public BinaryTree(int key, BinaryTree left, BinaryTree right) {
        this.key = key;
        setLeft(left);
        setRight(right);
    }

    public int getKey() {
        return key;
    }

    /**
     * @return the left child.
     */
    public BinaryTree getLeft() {
        return left;
    }

    /**
     * @return the right child.
     */
    public BinaryTree getRight() {
        return right;
    }

    public boolean hasLeft() {
        return left != null;
    }

    public boolean hasRight() {
        return right != null;
    }

    //Return a String representation of the BinaryTree using level order traversal
    public String toString(){
        int h = height(this);
        int i;
        String result = "";
        for (i=1; i<=h; i++) {
            result += printGivenLevel(this, i);
        }
        return result;
    }

    //returns the number of nodes in the BinaryTree
    public int size(){
        return size(this);
    }

    public static int size(BinaryTree tree){
        if(tree == null) return 0;
        return 1 + size(tree.getLeft()) + size(tree.getRight());
    }

    public int height(){ return height(this);}

    public static int height(BinaryTree tree){
        if(tree == null) return 0;
        int left = height(tree.getLeft());
        int right = height(tree.getRight());
        return Math.max(left, right) + 1;
    }

    public String printGivenLevel (BinaryTree root ,int level) {
        if (root == null) return "";
        String result = "";
        if (level == 1) {
            result += root.getKey() + " ";
            return result;
        }else if (level > 1) {
            String left = printGivenLevel(root.left, level-1);
            String right = printGivenLevel(root.right, level-1);
            return left + right;
        }else{
            return "";
        }
    }

    /**
     * @param left
     *     to set
     */
    public void setLeft(BinaryTree left) {
        this.left = left;
    }

    /**
     * @param right
     *     to set
     */
    public void setRight(BinaryTree right) {
        this.right = right;
    }
}

Example 2: generic binary search tree java

public class BinaryTree<T extends Comparable<T>> {
	TreeNode<T> root;
	
	public BinaryTree() {
		super();
		this.root = new TreeNode<T>();
	}
 
	public BinaryTree(T o) {
		super();
		// TODO Auto-generated constructor stub
		TreeNode<T> node = new TreeNode<T>(o);
		this.root = node;
	}
	
	public void add(T o) {
		if (root == null) {
			root = new TreeNode<T>(o);
		} else {
			root.insert(o);
		}
	}
	
	public boolean isMember (TreeNode<T> 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<T> node) {
		if (node != null) {
			node.visit();
			preOrderHelper(node.left);
			preOrderHelper(node.right);
		}
	}
	
	public void inOrderHelper(TreeNode<T> node) {
		if (node != null) {
			inOrderHelper(node.left);
			node.visit();
			inOrderHelper(node.right);
		}
	}
	
	public void postOrderHelper(TreeNode<T> 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);
		}
	}
	
}

Tags:

Go Example