Question
/** This program demonstrates construction and traversal of binary trees. */ public class BinaryTreeNodes { // Creates a binary tree from nodes and traverses it
/** This program demonstrates construction and traversal of binary trees. */ public class BinaryTreeNodes { // Creates a binary tree from nodes and traverses it in // in inorder. public static void main(String[] args) { Node root = null; // Will be root of the binary tree. Node aNode = new Node(10); aNode.left = new Node(20); Node dNode = new Node(40); Node cNode = new Node(30, dNode, new Node(50)); aNode.right = cNode; root = aNode; System.out.print("Inorder traversal is: "); NodeUtilities.inorder(root); System.out.println(); } }
/** This program demonstrates construction and traversal of binary trees. */ public class BinaryTreeNodes { // Creates a binary tree from nodes and traverses it in // in inorder. public static void main(String[] args) { Node root = null; // Will be root of the binary tree. Node aNode = new Node(10); aNode.left = new Node(20); Node dNode = new Node(40); Node cNode = new Node(30, dNode, new Node(50)); aNode.right = cNode; root = aNode; System.out.print("Inorder traversal is: "); NodeUtilities.inorder(root); System.out.println(); } }
/** This class has various utility methods for working with nodes. */ public class NodeUtilities { /** Inorder traversal of a binary tree rooted at a node. @param btree : The root of the binary tree. */ static public void inorder(Node btree) { if (btree != null) { inorder(btree.left); System.out.print(btree.value + " "); inorder(btree.right); } } }
50 90 70 100 80 60 40 75
Study the three Java source files.
Create a project in eclipse to run the code and run it to see the output.
Then, modify the code in BinaryTreeNodes.java according to the following:
build a binary tree as the values are read from NodeVals.txt
print the inorder traversal of the tree using the routine in NodeUtilities.java
the internal order of the tree is represented by the diagram in the "Binary Tree.doc" file.
Note:
don't create variable names for each node so the program will work with varying input sizes (contrary to the sample file where the values are hard coded);
create a variable to hold the root node, and, a variable to address the node being visited/worked on.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started