Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use this second part of code to check if you can call BinSort to see the result of adding the numbers 1-5 and calling the

image text in transcribedimage text in transcribed

Use this second part of code to check if you can call BinSort to see the result of adding the numbers 1-5 and calling the above traversal method

image text in transcribed

public class BinaryTree> { BinaryTreeNode root; public BinaryTree() { root = null; } private void addToSubTree (BinaryTreeNode n, E v) { if (n!=null) // sanity check! { E nValue = n.getValue(); if (v.compareTo(nValue) (v)); else addToSubTree(n.getLeft(), v); } else { System.out.println("Adding "+v+" to right sub-tree of "+nValue); if (n.getRight()==null) n.setRight(new BinaryTreeNode(v)); else addToSubTree(n.getRight(), v); } } } public void add(E v) { if (root==null) { System.out.println("Adding "+v+" to root."); root = new BinaryTreeNode(v); } else addToSubTree(root, v); } private void inOrder(BinaryTreeNode n) { if (n!=null) { inOrder(n.getLeft()); System.out.print(((Integer)n.getValue()). intValue()+" "); inOrder(n.getRight()); } } // INCOMPLETE. private void preorder(BinaryTreeNode n) { // this method is to be completed... } // INCOMPLETE. private void postOrder (BinaryTreeNode n) { // this method is to be completed... } public void traversal(). { System.out.print("Inorder traversal: "); inOrder(root); System.out.println(); Implemented preOrder Implemented postOrder Modified the traversal method so that it shows the output of inOrder, preOrder and postOrder * Sorting integers using a binary tree */ public class BinSort { public static void main(String[] args) { BinaryTree t = new BinaryTree(); t.add(2); t.add(4); t.add(1); t.add(5); t.add(3); t.traversal(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

7. Discuss the key features of the learning organization.

Answered: 1 week ago