Programming assignment learning objectives: Binary tree structure and traversal implementation. This assignment is on binary trees. The attached file provides a Treetest class, as the main program to...

1 answer below »

Programming assignment learning objectives: Binary tree structure and traversal implementation.






This assignment is on binary trees. The attached file provides a Treetest class, as the main program to test your binary tree code, and a TreeNode class. The TreeNode class is missing the method code to insert a node into the binary tree, and it is missing code for the three types of binary tree traversal (inorder, postorder, preorder). Your assignment is to complete the binary tree code so that it will effectively insert nodes and display all three types of binary tree traversals correctly. When building a binary tree, all nodes are inserted as leaf nodes and the smaller value is always to the left of the parent node. This program completes three types of traversals on a binary tree using a sentence input by the user. The words in the sentence are parsed into a binary tree structure in alphabetical order. Test your code using the following input: “This is my favorite programming assignment”. Make sure to provide some commends on each code line. Your execution screenshot for a correct program should look like the following:






Please enter a string


this is my favorite programming assignment






Preorder traversal


this is favorite assignment my programming






Inorder traversal


assignment favorite is my programming this






Postorder traversal


assignment favorite programming my is this




import java.util.Scanner; public class Treetest { public static void main(String[] args) { // get input string Scanner scanner = new Scanner(System.in); System.out.println("Please enter a string"); String input = scanner.nextLine(); Tree tree = new Tree<>(); // insert input string into tree for (String word : input.split("\\s+")) { tree.insertNode(word); } System.out.println("\n\nPreorder traversal"); tree.preorderTraversal(); System.out.println("\n\nInorder traversal"); tree.inorderTraversal(); System.out.println("\n\nPostorder traversal"); tree.postorderTraversal(); System.out.println(); // print trailing newline } } //class TreeNode definition class TreeNode> { // package access members TreeNode leftNode; // left node T data; // node value TreeNode rightNode; // right node // constructor initializes data and makes this a leaf node public TreeNode(T nodeData) { data = nodeData; leftNode = rightNode = null; // node has no children } // locate insertion point and insert new node; ignore duplicate values public void insert(T insertValue) { // insert in left subtree **YOUR CODE GOES HERE // insert in right subtree ** YOUR CODE GOES HERE //class Tree definition public class Tree> { private TreeNode root; // constructor initializes an empty Tree of integers public Tree() { root = null; } // insert a new node in the binary search tree public void insertNode(T insertValue) { if (root == null) { root = new TreeNode<>(insertValue); // create root node } else { root.insert(insertValue); // call the insert method } } // begin preorder traversal public void preorderTraversal() { preorderHelper(root); } // recursive method to perform preorder traversal private void preorderHelper(TreeNode node) { ** YOUR CODE GOES HERE // begin inorder traversal public void inorderTraversal() { inorderHelper(root); } // recursive method to perform inorder traversal private void inorderHelper(TreeNode node) { **YOUR CODE GOES HERE // begin postorder traversal public void postorderTraversal() { postorderHelper(root); } // recursive method to perform postorder traversal private void postorderHelper(TreeNode node) { ** YOUR CODE GOES HERE
Answered Same DayDec 11, 2021

Answer To: Programming assignment learning objectives: Binary tree structure and traversal implementation. This...

Ramachandran answered on Dec 12 2021
127 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here