Question

Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node ne

public class BTNode { int key; BTNode left; // left subtree BTNode right; // right subtree public BTNode (int key) { this.key

30 Figure 3 Suppose there are unique keys in the BST and the BST is rooted at level 1. The height of the BST is the largest l

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Code:

Queue.java (unchanged)

public class Queue
{
private static class Node {
Object object;
Node next;
Node() { object = null; next = null; }
Node(Object object, Node next) {
this.object = object;
this.next = next;
}
}
private Node header;
private int size = 0;
public Object dequeue() {
if(size == 0) {
return null;
} else {
Object remove_object = header.object;
header = header.next;
size--;
return remove_object;
}
}
public void enqueue(Object object) {
if(size == 0)
header = new Node(object, null);
else {
Node p = new Node(object, null);
Node r = header;
while(r.next != null)
r = r.next;
r.next = p;
}
size++;
}
}

BTNode.java (unchanged)

public class BTNode
{
int key;
BTNode left;
BTNode right;
public BTNode(int key) {
this.key = key;
}
}

BTNodeTree.java

public class BTNodeTree
{
public BTNode r;
  
// a) - function to print postOrder of BST
public void postOrder(BTNode t) {
  
// base condition for null node
if(t==null)
return;
  
// call for the left child
postOrder(t.left);
// call for the right child
postOrder(t.right);
  
// now print the value at the node
System.out.print(t.key + " ");
}
  
// function to print level order traversal for BST
public void levelOrder(BTNode t) {
  
// return for empty tree
if(t==null)
return;
  
// create an empty queue for levelOrder traversal
Queue q = new Queue();
// put the root in queue
q.enqueue(t);
  
// pop the first element from queue
BTNode curr = (BTNode) q.dequeue();
  
// loop till all elements are traversed
while(curr != null) {
  
// Pushing left child current node
if (curr.left != null)
q.enqueue(curr.left);

// Pushing right child current node
if (curr.right != null)
q.enqueue(curr.right);
  
// print the present node value
System.out.print(curr.key + " ");
  
// move to next node
curr = (BTNode) q.dequeue();
}
}
  
// b) - funtion to insert values in BST
public void insert(int theKey, BTNode t) {
  
// for null tree root
if(t==null) {
t = new BTNode(theKey);
return;
}
  
// insert at the left half
if(theKey < t.key) {
if(t.left == null)
t.left = new BTNode(theKey);
else insert(theKey, t.left);
}
// else insert in the right
else {
if(t.right == null)
t.right = new BTNode(theKey);
else insert(theKey, t.right);
}
}
  
// c) - recursive function to return the height of tree
public int height(BTNode t) {
  
// end of tree
if(t == null)
return 0;
  
// add present height and maximum of the heights of the 2 children
return 1 + Math.max(height(t.left), height(t.right));
}
}

Main.java (driver class)

// driver class to test the program
public class Main
{
public static void main(String[] args)
{
// sample run
BTNodeTree ob = new BTNodeTree();
BTNode root = new BTNode(19);
ob.insert(22, root);
ob.insert(15, root);
ob.insert(7, root);
ob.insert(35, root);
ob.insert(18, root);
ob.insert(30, root);

// part a)
System.out.print("PostOrder traversal: ");
ob.postOrder(root);
System.out.println();

// print height for part c)
System.out.println("Height of tree: " + ob.height(root));

// for level order, the tree should print like this -
// 19 15 22 7 18 35 30
System.out.print("LevelOrder traversal: ");
ob.levelOrder(root);
System.out.println();
}
}

Sample output:

Postorder traversal: 7 18 15 30 35 22 19 Height of tree: 4 LevelOrder traversal: 19 15 22 7 18 35 30

Code screenshots (only for changed code):

BTNodeTree.java

1 public class BTNodeTree public BTNode r; // a) - function to print postOrder of BST public void postOrder (BTNode t) { // b

q. enqueue (curr.left); 41 42 // Pushing right child current node if (curr.right != null) q. enqueue (curr.right); 43 44 45 4

// end of tree 81 null) if(t 82 =3= return 0; 83 84 // add present height and maximum of the heights of the 2 children 85 ret

Main.java

1 // driver class to test the program 2 public class Main 3- { public static void main(String[] args) { // sample run BTNodeT

Add a comment
Know the answer?
Add Answer to:
Question B1 You are given the following Java classes: public class Queue { private static class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Test Data would be as follows: Using java language. Build an expression tree. • When you...

    Test Data would be as follows: Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values...

    package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT