To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
In this lab assignment, we are tasked with writing a Java program to traverse a binary tree using InOrder traversal with recursion. In InOrder traversal, we visit the left node first, followed by the root, and then the right node. The program should create a binary tree with numeric placeholders (instead of letters) to satisfy the requirements of a valid binary search tree. While user input for numbers is optional, it will result in extra credit points if implemented. This assignment focuses on implementing the InOrder traversal algorithm and creating a binary tree data structure.
The assignment involves the following steps:
Below is the Java code for implementing the InOrder traversal of a binary tree:
import java.util.Stack;
public class Lab8 {
public static void main(String[] args) {
// Create the binary tree
BinaryTree bt = BinaryTree.create();
// Traverse the binary tree using recursive InOrder traversal
System.out.println("Nodes of binary tree on recursive InOrder traversal:");
bt.inOrder();
}
}
class BinaryTree {
static class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode(int value) {
this.data = value;
left = right = null;
}
}
// Root of binary tree
TreeNode root;
/**
* Algorithm to traverse the binary tree using recursive InOrder traversal
*/
public void inOrder() {
inOrder(root);
}
private void inOrder(TreeNode node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.data);
inOrder(node.right);
}
}
/**
* Java method to create binary tree with test data
*
* @return a sample binary tree for testing
*/
public static BinaryTree create() {
BinaryTree tree = new BinaryTree();
TreeNode root = new TreeNode(4);
tree.root = root;
root.left = new TreeNode(2);
root.right = new TreeNode(6);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(7);
return tree;
}
}
Upon running the provided Java program, it will create a binary tree with the following structure:
4 / \ 2 6 / \ / \ 1 3 5 7
Then, it will perform an InOrder traversal of the binary tree and print the nodes in the order they are visited.
The output will be:
1 2 3 4 5 6 7
The program successfully demonstrates the InOrder traversal of a binary search tree.
If you want to implement user input for filling the binary tree with numbers, you can modify the code to prompt the user for input.
Here's how you can achieve this:
By implementing these changes, your program can ask the user to input numbers and sort them into a binary tree, resulting in a valid binary search tree. This modification will earn you 5 extra credit points, making the assignment worth up to 200%.
This lab assignment has provided hands-on experience in working with binary trees and implementing the InOrder traversal algorithm using recursion. It demonstrates how to create a binary tree data structure and traverse it in a specific order. Additionally, it highlights the flexibility of the code by allowing for user input to create a binary search tree.
Below are the Java classes used in this assignment:
import java.util.Stack;
public class Lab8 {
public static void main(String[] args) {
// ... (Refer to the Methodology section for the complete code)
}
}
class BinaryTree {
static class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode(int value) {
this.data = value;
left = right = null;
}
}
// Root of binary tree
TreeNode root;
// Methods for creating the binary tree and performing InOrder traversal...
}
Lab Assignment Report - InOrder Binary Search Tree Traversal. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-8-inorder-binary-search-tree-traversal
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment