Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need inline comments for this code so I understand what is happening. original question is: Create a subclass of BinaryTree whose nodes have fields for
Need inline comments for this code so I understand what is happening. original question is:
Create a subclass of BinaryTree whose nodes have fields for storing preorder, post-order, and in-order numbers. Write methods preOrderNumber(), inOrderNumber(), and postOrderNumbers() that assign these numbers correctly. These methods should each run in O(n) time.
import java.util.*; public class Nodetree { //Question 5 int keyvalue; Nodetree l, r; public Nodetree(int item) { keyvalue = item; l = r = null; } } class BTree{ Nodetree root; BTree(){ root = null; } void postOrderNumbers(Nodetree node1){ if(node1 == null) return; postOrderNumbers(node1.l); postOrderNumbers(node1.r); System.out.print(node1.keyvalue+""); } void inOrderNumber(Nodetree node1){ if(node1 == null) return; inOrderNumber(node1.l); System.out.print(node1.keyvalue+""); inOrderNumber(node1.r); } void preOrderNumber(Nodetree node1){ if(node1 == null) return; System.out.print(node1.keyvalue+""); preOrderNumber(node1.l); preOrderNumber(node1.r); } void postOrderNumbers(){postOrderNumbers(root);} void inOrderNumber() {inOrderNumber(root);} void preOrderNumber() {preOrderNumber(root);} public static void main(String[] args){ BTree tree = new BTree(); tree.root = new Nodetree(5); tree.root = new Nodetree(10); tree.root = new Nodetree(15); tree.root = new Nodetree(20); tree.root = new Nodetree(25); System.out.println("Preorder for binary tree is "); tree.preOrderNumber(); System.out.println("Inorder for binary tree is "); tree.inOrderNumber(); System.out.println("Postorder for binary tree is "); tree.postOrderNumbers(); } }
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