Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tree-Traversal #120

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Java/BST.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ void inorderTraversal(Node rootNode)
}
}

void preorder()
{
preorderTraversal(this.rootNode);
}

void preorderTraversal(Node rootNode)
{
if(rootNode != null)
{
System.out.print(rootNode.data + " => ");
preorderTraversal(rootNode.leftSideNode);
preorderTraversal(rootNode.rightSideNode);
}
}

void postorder()
{
postorderTraversal(this.rootNode);
}

void postorderTraversal(Node rootNode)
{
if(rootNode != null)
{
postorderTraversal(rootNode.leftSideNode);
postorderTraversal(rootNode.rightSideNode);
System.out.print(rootNode.data + " => ");
}
}

public static void main(String[] args) {
// create a new binary search tree
BST binary_search_tree = CreateBST();
Expand Down Expand Up @@ -191,8 +221,14 @@ public static void main(String[] args) {
}
} while (test != 101);
sc.close();
System.out.println("The BST uptil now is");
System.out.println("The BST uptil now is:");
System.out.println(" -in Inorder Traversal:");
binary_search_tree.inorder();
System.out.println(" -in Preorder Traversal:");
binary_search_tree.preorder();
System.out.println(" -in Postorder Traversal:");
binary_search_tree.postorder();


}

Expand Down