Question
in java ..write all complete program from a- e
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees
ree lem# a) Create a TreeNode class with the following methods: default constructor, two overloaded constructors, copy constr
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree 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. 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 the above. Add this method to the class BinaryTree and create a program to test this method. Note: To test your algorithm, first create a binary search tree. 2. 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.
ree lem# a) Create a TreeNode class with the following methods: default constructor, two overloaded constructors, copy constructor, getValue, getLeft, getRight, setValue, setLeft, setRight b) Create a BTInterface with the following abstract methods: getRoot, setRoot, isEmpty, swapSubtrees, singleParent, preorder, postOrder, inOrder, insert c) Create an abstract BinaryTree class that implements the interface. Include the default constructor, a private helper method called checkNode for singleParent) and toString. Make insert an abstract method d) Derive a BinarySearchTree class from Binary Tree with the following methods: default constructor, overloaded constructor (use a variable length parameter), insert, e) Create a TreeDemo class that creates two BinarySearch Tree objects. Use default and overloaded constructors. For tree one: call the methods make Tree (makes a complete tree), printTree, swapSubtrees, printTree. Simliar for tree two. Also, print out the number of single parents in each tree. Define make Tree and printTree in this class
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

import java.util.Queue;
import java.util.LinkedList;
class Node
{
int data;
Node left, right;
  
public Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
  
void swapSubtrees()
{
root = swapSubtrees(root);
}
  
Node swapSubtrees(Node node)
{
if (node == null)
return node;
  
/* do the subtrees */
Node left = swapSubtrees(node.left);
Node right = swapSubtrees(node.right);
  
/* swap the left and right pointers */
node.left = right;
node.right = left;
  
return node;
}
  
void inOrder()
{
inOrder(root);
}
  
/* Helper function to test mirror(). Given a binary
search tree, print out its data elements in
increasing sorted order.*/
void inOrder(Node node)
{
if (node == null)
return;
  
inOrder(node.left);
System.out.print(node.data + " ");
  
inOrder(node.right);
}
int singleParent()
{
// If tree is empty
if (root==null)
return 0;
  
// Do level order traversal starting from root
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
  
int count=0; // Initialize count of half nodes
while (!queue.isEmpty())
{
  
Node temp = queue.poll();
if (temp.left!=null && temp.right==null ||
temp.left==null && temp.right!=null)
count++;
  
// Enqueue left child
if (temp.left != null)
queue.add(temp.left);
  
// Enqueue right child
if (temp.right != null)
queue.add(temp.right);
}
return count;
}
}
public class Main
{
   public static void main(String[] args) {
       BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
  
/* print inorder traversal of the input tree */
System.out.println("Inorder traversal of input tree is :");
tree.inOrder();
System.out.println("");
  
/* convert tree to its mirror */
tree.swapSubtrees();
  
/* print inorder traversal of the minor tree */
System.out.println("Inorder traversal of binary tree is : ");
tree.inOrder();
  
/* second tree*/
BinaryTree tree1=new BinaryTree();
tree1.root=new Node(14);
tree1.root.left=new Node(4);
tree1.root.right=new Node(15);
tree1.root.left.left=new Node(3);
tree1.root.left.right=new Node(9);
tree1.root.left.right.left=new Node(7);
tree1.root.left.right.left.left=new Node(5);
tree1.root.right.right=new Node(18);
tree1.root.right.right.left=new Node(16);
tree1.root.right.right.right=new Node(20);
tree1.root.right.right.left.right=new Node(17);
  
System.out.println("Inorder traversal of input tree1 is :");
tree1.inOrder();
System.out.println("");
  
/* convert tree to its mirror */
tree1.swapSubtrees();
  
/* print inorder traversal of the minor tree */
System.out.println("Inorder traversal of binary tree is : ");
tree1.inOrder();
System.out.println("Count of Single Child node of Tree:"+tree.singleParent());
System.out.println("Count of Single Child node of Tree1:"+tree1.singleParent());
   
   }
}

2.


import java.util.*;

public class Main
{
   public static void main(String[] args) {
  
PriorityQueue<Integer> heapQueue = new PriorityQueue<Integer>();
  
// Adding items to the heapQueue using add()
heapQueue.add(1);
heapQueue.add(2);
heapQueue.add(3);
heapQueue.add(4);
heapQueue.add(5);
heapQueue.add(6);
heapQueue.add(7);
heapQueue.add(8);
heapQueue.add(9);
heapQueue.add(10);
System.out.println("The queue elements:");
Iterator itr = heapQueue.iterator();
while (itr.hasNext())
System.out.println(itr.next());
heapQueue.remove(4);heapQueue.remove(6);heapQueue.remove(3);
System.out.println("after removing 4 ,6,3 from heap:");
Iterator<Integer> itr1 = heapQueue.iterator();
while (itr1.hasNext())
System.out.println(itr1.next());
   }
}

Add a comment
Know the answer?
Add Answer to:
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...
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
  • please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE...

    please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The nat...

    Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binar...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

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