Please code in Java and ignore
question 2, thank you!
CODE
================
import java.util.ArrayList;
public class Main {
// function for constructing trees
static ArrayList<Node> constructTrees(int start, int end)
{
ArrayList<Node> list=new ArrayList<>();
/* if start > end then subtree will be empty so returning NULL
in the list */
if (start > end)
{
list.add(null);
return list;
}
/* iterating through all values from start to end for constructing\
left and right subtree recursively */
for (int i = start; i <= end; i++)
{
/* constructing left subtree */
ArrayList<Node> leftSubtree = constructTrees(start, i - 1);
/* constructing right subtree */
ArrayList<Node> rightSubtree = constructTrees(i + 1, end);
/* now looping through all left and right subtrees and connecting
them to ith root below */
for (int j = 0; j < leftSubtree.size(); j++)
{
Node left = leftSubtree.get(j);
for (int k = 0; k < rightSubtree.size(); k++)
{
Node right = rightSubtree.get(k);
Node node = new Node(i); // making value i as root
node.left = left; // connect left subtree
node.right = right; // connect right subtree
list.add(node); // add this tree to list
}
}
}
return list;
}
// A utility function to do preorder traversal of BST
static void preorder(Node root)
{
if (root != null)
{
System.out.print(root.key+" ") ;
preorder(root.left);
preorder(root.right);
}
}
public static void main(String args[])
{
ArrayList<Node> totalTreesFrom1toN = constructTrees(1, 3);
System.out.println("Number of unique BSTs = " + totalTreesFrom1toN.size());
// IF YOU WANT TO PRINT THE TREES TOO, UNCOMMENT THE BELOW LINES OF CODE
// /* Printing preorder traversal of all constructed BSTs */
// System.out.println("Preorder traversals of all constructed BSTs are ");
// for (int i = 0; i < totalTreesFrom1toN.size(); i++)
// {
// preorder(totalTreesFrom1toN.get(i));
// System.out.println();
// }
}
}
// node structure
class Node
{
int key;
Node left, right;
Node(int data)
{
this.key=data;
left=right=null;
}
};
NOTE: As per HOMEWORKLIB POLICY, I am allowed to answer only 1 coding question (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.
Please code in Java and ignore question 2, thank you! 1. Write a Java method that...
I need question 9-10 answered. Thank you
Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...
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...
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 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,...
Please the answer have to be in JAVA programing language Problem 1: Write a class that implements a generic binary search tree including the methods add, contains and printInorder. (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string of the form “[e1, e2, …, en]” containing all of the elements in the tree in order. Problem 3: Add a method to the class that returns the number of...
In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...
using java to write,show me the output. please write some
common.
You CAN NOT use inbuild functions for Tree ADT operations.
using code below to finsih
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);
tree.root.right.right = new Node(7);
tree.root.left.left.left = new Node(8);
tree.root.left.left .right= new Node(9);...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...
Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...
Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....