java data structure series
This commit is contained in:
@@ -12,7 +12,7 @@ public class BinarySearchTreeTest {
|
||||
/ \
|
||||
15 56
|
||||
/ \ / \
|
||||
9 11 54 61
|
||||
9 11 54 61
|
||||
/ \
|
||||
3 5 */
|
||||
|
||||
@@ -25,5 +25,17 @@ public class BinarySearchTreeTest {
|
||||
bst.insert(3);
|
||||
bst.insert(5);
|
||||
bst.insert(61);
|
||||
|
||||
//inorder traversal
|
||||
bst.inOrderTraversal();
|
||||
|
||||
System.out.println("********* Pre Order *******************");
|
||||
//pre-order
|
||||
bst.preOrderTraversal();
|
||||
|
||||
System.out.println("************ Post Order ****************");
|
||||
|
||||
//post-order
|
||||
bst.postOrderTraversal();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,91 @@ public class BinarySearchTree {
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* In order BST traversal allows to traverse tree in the sorted order.
|
||||
* This traversal follow left->current-right pattern.
|
||||
* <li>left sub-tree will be viisted first.</li>
|
||||
* <li> Current node will be viisted after left traversal</li>
|
||||
* <li>Right subtree will be visited in the end.</li>
|
||||
*/
|
||||
public void inOrderTraversal(){
|
||||
inOrderTraversal(root);
|
||||
}
|
||||
|
||||
public void postOrderTraversal(){
|
||||
postOrderTraversal(root);
|
||||
}
|
||||
|
||||
public void preOrderTraversal(){
|
||||
preOrderTraversal(root);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Internal private method to do in order traversal.We will pass the
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path left-current-right
|
||||
*/
|
||||
private void inOrderTraversal(Node node){
|
||||
|
||||
//We will continue until null or empty node is found
|
||||
if(node!=null){
|
||||
|
||||
//visit the left subtree until the leaf node
|
||||
inOrderTraversal(node.left);
|
||||
|
||||
//Print the node
|
||||
System.out.println(node.data);
|
||||
|
||||
//process the same step for the right node
|
||||
inOrderTraversal(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Internal private method to do pre order traversal.We will pass the
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path current-left-right
|
||||
*/
|
||||
private void preOrderTraversal(Node node){
|
||||
|
||||
//We will continue until null or empty node is found
|
||||
if(node!=null){
|
||||
|
||||
//Print the node
|
||||
System.out.println(node.data);
|
||||
|
||||
//visit the left subtree until the leaf node
|
||||
inOrderTraversal(node.left);
|
||||
|
||||
//process the same step for the right node
|
||||
inOrderTraversal(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Internal private method to do post-order traversal.We will pass the
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path right-left-current
|
||||
*/
|
||||
private void postOrderTraversal(Node node){
|
||||
|
||||
//We will continue until null or empty node is found
|
||||
if(node!=null){
|
||||
|
||||
//visit the left subtree until the leaf node
|
||||
inOrderTraversal(node.left);
|
||||
|
||||
//process the same step for the right node
|
||||
inOrderTraversal(node.right);
|
||||
|
||||
//Print the node
|
||||
System.out.println(node.data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal node class representing the node of the BST. This contains the following information
|
||||
* <li>data- actual data stored in the Tree</li>
|
||||
|
||||
Reference in New Issue
Block a user