Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to know answer for the bottom EXTRA CREDIT question only but it\'s also b

ID: 3547709 • Letter: I

Question

I want to know answer for the bottom EXTRA CREDIT question only but it's also based on question number 1. Please provide method with its demo(JAVA PROGRAM).

Given the two binary trees below: Write a method called swap Subtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class Binary Tree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Start with an empty heap, and enter ten items with priorities 1 through 10. Draw the resulting heap. - Now remove three entries from the heap that you created in the above exercise. Draw the resulting heap. Write a method called singleParent, which returns the number of nodes in a binary tree that have only one child for the 2 trees given in problem #1. Add this method to the class Binary Tree and create a program to test this method. Note: To test your algorithm, first create a binary search tree.

Explanation / Answer


please rate


public class BinaryTree {
    BinaryTree left;
    BinaryTree right;
    int data;
   
    public BinaryTree(int data){
        this.data = data;
        left = null;
        right = null;
    }
   
    // Binary search insert
    public BinaryTree insert(BinaryTree root,int data){
        if(root==null){
            return new BinaryTree(data);
        }else if(data<root.data){
            root.left=insert(root.left,data);
        }else{
            root.right = insert(root.right,data);
        }       
        return root;
    }
   
    public void displayInorder(BinaryTree root){
        if(root==null)
            return;
        displayInorder(root.left);
        System.out.println(root.data);
        displayInorder(root.right);
    }
   

// helper method for required method
    private int singleParentHelper(BinaryTree root,int count){
        if(root==null||(root.left==null&&root.right==null))
            return count;
        count = singleParentHelper(root.left, count);
        if(root.left==null||root.right==null)
            count ++;
        count = singleParentHelper(root.right, count);
        return count;
    }
   

   // required method
    public int singleParent(BinaryTree root){
        return singleParentHelper(root, 0);
    }
   

    // Test case for creating a binary search tree
    public void createBinarySearchTree(){
        BinaryTree root = null;
        root = insert(root,5);
        insert(root,4);
        insert(root,7);
        insert(root,1);
        insert(root,9);
        System.out.println("Tree is");
        displayInorder(root);
        System.out.println("single parent"+singleParent(root));
    }
   
    public static void main(String[] args){
        new BinaryTree(0).createBinarySearchTree();
    }
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote