java data structure series
This commit is contained in:
@@ -12,29 +12,26 @@ public class BinarySearchTreeTest {
|
||||
/ \
|
||||
15 56
|
||||
/ \ / \
|
||||
9 11 54 61
|
||||
9 16 54 61
|
||||
/ \
|
||||
3 5 */
|
||||
3 10 */
|
||||
|
||||
bst.insert(52);
|
||||
bst.insert(15);
|
||||
bst.insert(56);
|
||||
bst.insert(9);
|
||||
bst.insert(11);
|
||||
bst.insert(16);
|
||||
bst.insert(54);
|
||||
bst.insert(3);
|
||||
bst.insert(5);
|
||||
bst.insert(10);
|
||||
bst.insert(61);
|
||||
|
||||
//inorder traversal
|
||||
bst.inOrderTraversal();
|
||||
|
||||
System.out.println("********* Pre Order *******************");
|
||||
System.out.println("*****************");
|
||||
//pre-order
|
||||
bst.preOrderTraversal();
|
||||
|
||||
System.out.println("************ Post Order ****************");
|
||||
|
||||
System.out.println("*****************");
|
||||
//post-order
|
||||
bst.postOrderTraversal();
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class BinarySearchTree {
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path left-current-right
|
||||
*/
|
||||
private void inOrderTraversal(Node node){
|
||||
public void inOrderTraversal(Node node){
|
||||
|
||||
//We will continue until null or empty node is found
|
||||
if(node!=null){
|
||||
@@ -101,7 +101,7 @@ public class BinarySearchTree {
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path current-left-right
|
||||
*/
|
||||
private void preOrderTraversal(Node node){
|
||||
public void preOrderTraversal(Node node){
|
||||
|
||||
//We will continue until null or empty node is found
|
||||
if(node!=null){
|
||||
@@ -110,10 +110,10 @@ public class BinarySearchTree {
|
||||
System.out.println(node.data);
|
||||
|
||||
//visit the left subtree until the leaf node
|
||||
inOrderTraversal(node.left);
|
||||
preOrderTraversal(node.left);
|
||||
|
||||
//process the same step for the right node
|
||||
inOrderTraversal(node.right);
|
||||
preOrderTraversal(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,22 +123,23 @@ public class BinarySearchTree {
|
||||
root node to start with and will visit the tree recursively using the following
|
||||
path right-left-current
|
||||
*/
|
||||
private void postOrderTraversal(Node node){
|
||||
public 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);
|
||||
postOrderTraversal(node.left);
|
||||
|
||||
//process the same step for the right node
|
||||
inOrderTraversal(node.right);
|
||||
postOrderTraversal(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