Question: Using a Binary Tree to Implement a Heap and Heap Sort I need help in the following assignment i do have a binary code written

Using a Binary Tree to Implement a Heap and Heap Sort

I need help in the following assignment i do have a binary code written but the output is just the sort but not what is wanted in the assignement if someone can help me by building the code With comments so i can understand i would appreciate it. the folowing are the specs behind the assignment. Implement Heap Sort b. Error Checking Make sure the user inputs only positive integers or -1. No letters, negative numbers other than -1, special characters etc. c. You must use the previous code (we never did create a previous code however i will include mine) to implement this AND not use any of the java built in functions for binary trees. 4. YOUR CODE MUST RUN! NO WARNINGS OR ERRORS! 5. Input/Output: (MUST BE USER FRIENDLY!!! MAKE SURE THEY CAN UNDERSTAND WHATS BEING ASKED OF THEM AND WHATS BEING DISPLAYED!!!) a. Prompt the user to input integers. Allow them to enter -1 in order to stop entering. b. Output the tree after you build it for the first time.

c. Output the numbers they gave you, the current sorted list at each step, and then the final output. Sample output: USER CAN ENTER AS MANY AND ANY POSITIVE INTEGER THEY WANT. DO NOT HARD CODE THIS!

Using a Binary Tree to Implement a Heap and Heap Sort I

the following is a check list and pointage value.

need help in the following assignment i do have a binary code

written but the output is just the sort but not what is

//Assignment Binary trees my code

package GutierrezBinaryTree;

import java.util.Scanner;

//Class Binary Tree

public class GutierrezBinaryTree

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

//Object

BT bt = new BT();

//Tree options and user interface

System.out.println("Binary Tree Assignment ");

char ch;

do

{

System.out.println(" Binary Tree Options ");

System.out.println("1. Insert ");

System.out.println("2. Search");

System.out.println("3. Count nodes");

System.out.println("4. Is empty? (true/false)");

System.out.println("5. Quit");

int option = scan.nextInt();

switch (option)

{

case 1:

System.out.println("Enter element to insert");

bt.insert( scan.nextInt() );

break;

case 2:

System.out.println("Enter element to search");

System.out.println("Results: "+ bt.search( scan.nextInt() ));

break;

case 3:

System.out.println("Nodes = "+ bt.countNodes());

break;

case 4:

System.out.println("Is it empty?= "+ bt.isEmpty());

break;

case 5:

System.out.println("Thank you! Terminating program!");

return;

default :

System.out.println("Not an option please try again: ");

break;

}

//Display interface for tree to keep track

System.out.print(" Post-order : ");

bt.postorder();

System.out.print(" Pre-order : ");

bt.preorder();

System.out.print(" In-order : ");

bt.inorder();

System.out.println(" Do you want to continue (Type y or n) ");

ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');

}

}

//Class BTNode

class BTNode

{

BTNode left, right;

int data;

//Constructor no input

public BTNode()

{

left = null;

right = null;

data = 0;

}

//Constructor input

public BTNode(int n)

{

left = null;

right = null;

data = n;

}

//Function to set left node

public void setLeft(BTNode n)

{

left = n;

}

//Function to set right node

public void setRight(BTNode n)

{

right = n;

}

//Function to get left node

public BTNode getLeft()

{

return left;

}

//Function to get right node

public BTNode getRight()

{

return right;

}

//Function to set data to node

public void setData(int d)

{

data = d;

}

//Function to get data from node

public int getData()

{

return data;

}

} //end of class BTNode

//Class BT

class BT

{

//declairing root node

private BTNode root;

//Constructor that creates an empty binary tree

public BT()

{

root = null;

}

//Checks if tree is empty

public boolean isEmpty()

{

return root == null;

}

//Insert data into the root node

public void insert(int data)

{

root = insert(root, data);

}

//Function to insert data recursively

private BTNode insert(BTNode node, int data)

{

//if node is null, create a new one.

if (node == null)

node = new BTNode(data);

else

{

//if the right child is null, insert node to the right and add data

if (node.getRight() == null)

node.right = insert(node.right, data);

//otherwise insert node to the left and add data

else

node.left = insert(node.left, data);

}

return node;

}

//Count number of nodes

public int countNodes()

{

return countNodes(root);

}

//Count number of nodes recursively

private int countNodes(BTNode r)

{

//if the root is null return 0

if (r == null)

return 0;

//Otherwise set l to 1, recursively count the

/umber of left nodes, right nodes and return results.

else

{

int l = 1;

l += countNodes(r.getLeft());

l += countNodes(r.getRight());

return l;

}

}

//Search for an element and then returns value

//if it does or doesn't exist.

public boolean search(int val)

{

return search(root, val);

}

//Search for an element recursively

private boolean search(BTNode r, int val)

{

if (r.getData() == val)

return true;

if (r.getLeft() != null)

if (search(r.getLeft(), val))

return true;

if (r.getRight() != null)

if (search(r.getRight(), val))

return true;

return false;

}

//Inorder traversal

public void inorder()

{

inorder(root);

}

private void inorder(BTNode r)

{

if (r != null)

{

inorder(r.getLeft());

System.out.print(r.getData() +" ");

inorder(r.getRight());

}

}

//Preorder traversal

public void preorder()

{

preorder(root);

}

private void preorder(BTNode r)

{

if (r != null)

{

System.out.print(r.getData() +" ");

preorder(r.getLeft());

preorder(r.getRight());

}

}

//Postorder traversal

public void postorder()

{

postorder(root);

}

private void postorder(BTNode r)

{

if (r != null)

{

postorder(r.getLeft());

postorder(r.getRight());

System.out.print(r.getData() +" ");

}

}

}

You entered: 59 7463124 Iteration 1:9 Iteration 2: 79 Iteration 3: 679 Iteration 4:5679 Iteration 5: 45679 Iteration 6:445679 Iteration 7:3445679 Iteration 8: 2 3445679 Iteration 9: 123 445679 8*8*8*2***8*288*83888 Complete sorted list: 123445679 O49 OS *9999O99 99 You entered: 59 7463124 Iteration 1:9 Iteration 2: 79 Iteration 3: 679 Iteration 4:5679 Iteration 5: 45679 Iteration 6:445679 Iteration 7:3445679 Iteration 8: 2 3445679 Iteration 9: 123 445679 8*8*8*2***8*288*83888 Complete sorted list: 123445679 O49 OS *9999O99 99

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!