package binary_search_tree;

import java.util.NoSuchElementException;
import binary_search_tree.Node;		// My implementation of Node class present in package binary_search_tree

/*
	This class implements Binary Search Tree. Following algorithms have been implemented: 

						1. Insertion
						2. Deletion
						3. In Order Traversal
						4. Pre Order Traversal
						5. Post Order Traversal
						6. Level Order Traversal
						7. Search for an element

	Note: Duplicate Keys are not allowed.					
*/

class Bst{

	private Node root = null; 		// Root of the tree

	// Constructs bst with default values
	Bst(){
		// Do Nothing.	
	}


	// Inserts a new node with specified value at specifiec node n.
	// Time Complexity: 
	//					Average Case: O(log N)
	//					Worst Case: O(N)		
	// Returns Root of sub tree at which the new node is inserted

	protected Node insert(Node n, int value){

		// If n is null, allocate a new node with specified value at n
		if(n == null){	

			n = new Node(value);

		}
		else if(value < n.getValue()){ 	 // If value is less than the value at n, insert value in left subtree of n 	

			 n.setLeft( insert(n.getLeft(), value) );
		}
		else{

			n.setRight( insert(n.getRight(), value) ); //Otherwise, insert in the right subtree of n
		}

		// Return the root
		return n; 

	}


	// Inserts a new Node with specified value at Root Node. 
	// Calls overloaded insert(Node n, int value)

	public void insert(int value){

		root = insert(root,value);
	}	


	// Visits a Node in In Order fashion

	protected void inOrderVisit(Node n){

		// Return if n is null
		if(n == null)
			return;

		// Otherwise, visit Left subtree
		inOrderVisit(n.getLeft());

		// Print value at node
		System.out.print(n.getValue()+" ");

		// Visit Right subtree
		inOrderVisit(n.getRight());
	}


	// In Order traversal of tree. Calls inOrderVisit on Root
	// Throws NoSuchElementException if root is null i.e. tree is empty.

	public void inOrder(){

		// If tree is empty, throw NoSuchElementException
		if(root == null){
			throw new NoSuchElementException("Tree is empty");	
		}

		// Otherwise, call inOrderVisit on root
		inOrderVisit(root);

	}


	// Visits a Node in Pre Order fashion

	protected void preOrderVisit(Node n){

		// Return if n is null
		if(n == null)
			return;

		// Otherwise, Print value at node
		System.out.print(n.getValue()+" ");

		// Visit Left subtree
		preOrderVisit(n.getLeft());

		// Visit Right subtree
		preOrderVisit(n.getRight());
	}


	// Pre Order traversal of tree. Calls preOrderVisit on Root
	// Throws NoSuchElementException if root is null i.e. tree is empty.

	public void preOrder(){

		// If tree is empty, throw NoSuchElementException
		if(root == null){
			throw new NoSuchElementException("Tree is empty");	
		}

		// Otherwise, call preOrderVisit on root
		preOrderVisit(root);

	}



}

@@ -2840,12 +2840,704 @@
 );%0A%0A%09%7D%0A%0A
+%0A%09// Visits a Node in Post Order fashion%0A%0A%09protected void postOrderVisit(Node n)%7B%0A%0A%09%09// Return if n is null%0A%09%09if(n == null)%0A%09%09%09return;%0A%0A%09%09// Otherwise Visit Left subtree%0A%09%09postOrderVisit(n.getLeft());%0A%0A%09%09// Visit Right subtree%0A%09%09postOrderVisit(n.getRight());%0A%0A%09%09// Print value at node%0A%09%09System.out.print(n.getValue()+%22 %22);%0A%09%7D%0A%0A%0A%09// Post Order traversal of tree. Calls postOrderVisit on Root%0A%09// Throws NoSuchElementException if root is null i.e. tree is empty.%0A%0A%09public void postOrder()%7B%0A%0A%09%09// If tree is empty, throw NoSuchElementException%0A%09%09if(root == null)%7B%0A%09%09%09throw new NoSuchElementException(%22Tree is empty%22);%09%0A%09%09%7D%0A%0A%09%09// Otherwise, call postOrderVisit on root%0A%09%09postOrderVisit(root);%0A%0A%09%7D
 %0A%0A%7D%0A

Add post order traversal