Following are the things we need to keep into consideration.
Following is the code for the same in JAVA>
// Java program to construct BST from given preorder traversal
import java.util.*;
// A binary tree node
class Node {
int data;
Node left, right;
Node(int d) {
data = d;
left = right = null;
}
}
class BinaryTree {
// The main function that constructs BST
from array[]
Node constructTree(int array[], int size)
{
Node root = new Node(array[0]);
Stack<Node> s = new Stack<Node>();
// Push root
s.push(root);
// Iterate through rest
preorder array
for (int i = 1; i < size; ++i)
{
Node temp =
null;
while
(!s.isEmpty() && array[i] > s.peek().data) {
temp = s.pop();
}
if (temp !=
null) {
temp.right = new Node(array[i]);
s.push(temp.right);
}
else {
temp = s.peek();
temp.left = new Node(array[i]);
s.push(temp.left);
}
}
return root;
}
// A utility function to print inorder
traversal of a Binary Tree
void printInorder(Node node) {
if (node == null) {
return;
}
printInorder(node.left);
System.out.print(node.data + "
");
printInorder(node.right);
}
// function for printing the nodes which are coming in
the path of data
void printNodesInPath(Node node, int data) {
if(node == null) {
return;
}
if(node.data == data) {
return;
}
System.out.print(node.data+"
");
if(node.data < data) {
printNodesInPath(node.right, data);
}else {
printNodesInPath(node.left, data);
}
}
// this is the function for checking if both the
trees
// passed are same or not using a recursive
function
boolean checkRecIfSame(Node node1, Node node2) {
if(node1==null &&
node2==null) {
return
true;
}else if(node1 == null || node2
==null) {
return
false;
}
if(node1.data != node2.data)
{
return
false;
}else {
return
checkRecIfSame(node1.left, node2.left) &&
checkRecIfSame(node1.right, node2.right);
}
}
// This is same as the above function,
// but a non-recursive funciton using the stack
boolean checkNonRecIfSame(Node node1, Node node2)
{
Stack<Node> s1 = new
Stack<Node>(), s2 = new Stack<Node>();
if(node1 == null && node2
== null) {
return
true;
}
if(node1 == null || node2 == null)
{
return
false;
}
s1.push(node1);s2.push(node2);
while(!s1.isEmpty() &&
!s2.isEmpty()) {
Node n1 =
s1.pop(), n2 = s2.pop();
if(n1.data !=
n2.data) {
return false;
}
if(n1.left !=
null) {
s1.push(n1.left);
}
if(n1.right !=
null) {
s1.push(n1.right);
}
if(n2.left !=
null) {
s2.push(n2.left);
}
if(n2.right !=
null) {
s2.push(n2.right);
}
}
if(s1.isEmpty() &&
s2.isEmpty()) {
return
true;
}else {
return
false;
}
}
// here in this function to count the number of
nodes
// with one child
int countNodesWithSingleChild(Node node) {
if(node == null) {
return 0;
}else if(node.left != null
&& node.right != null) {
return
countNodesWithSingleChild(node.left) +
countNodesWithSingleChild(node.right);
}
else if(node.left == null
&& node.right == null) {
return 0;
}else if(node.right == null)
{
return
1+countNodesWithSingleChild(node.left);
}else {
return 1+countNodesWithSingleChild(node.right);
}
}
// Driver program to test above functions
public static void main(String[] args) {
BinaryTree tree1 = new
BinaryTree();
int array1[] = new int[]{310, 35,
31, 37, 340, 350};
int size = array1.length;
Node root1 =
tree1.constructTree(array1, size);
System.out.print("Inorder traversal
of the constructed tree is ");
tree1.printInorder(root1);
System.out.println();
BinaryTree tree2 = new
BinaryTree();
int array2[] = new int[]{310, 35,
31, 37, 340, 350};
int size2 = array1.length;
Node root2 =
tree1.constructTree(array2, size2);
System.out.print("Inorder traversal
of the constructed tree is ");
tree1.printInorder(root2);
System.out.println();
System.out.print("Nodes In the path
are: ");
tree1.printNodesInPath(root1,
350);
System.out.println("Checking if
both trees are same with recursive function: " +
tree1.checkRecIfSame(root1, root2));;
System.out.println("Checking if
both trees are same with recursive function: " +
tree1.checkNonRecIfSame(root1, root2));
System.out.println("Counting the
number of nodes with one child: " +
tree1.countNodesWithSingleChild(root1));
}
}
Thanks
*** using java application Q1. BST: 1. Write a method that takes an array A, as parameter (A contains the elements of a BST traversed in preorder) and returns the corresponding BST 2. Write a method...
Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...
please use java application to solve methods:
5,6,10,12,13 (no need for the rest)
Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) where the number of nodes and the range of the values are 1. from a file. 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST...
Problemi: Create a new Java Application that has the following methods: 1. A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST in in-order traversal 5. A method to find the largest value in a BST...
Write a client method that returns a count of the number of
nodes in a binary search tree that contain scores less than or
equal to a passed-in argument (parameter) value. The method header
is:
int countLowerScores (BinarySearchTree tree, int maxValue)
The BinarySearchTree contains these methods in the
picture.
public class BinarySearchTreecT> implements BSTInterfacecT> //reference to the root of this BST public BSTNode<T> root; public Comparator<T> comp: IIused for all comparisons public boolean found: / used by remove public BinarYSearchTree()...
please explain each line of code! ( in python
)
1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....
1) Extend the Binary Search Tree ADT to include
a public method leafCount that returns the number of leaf nodes in
the tree.
2) Extend the Binary Search Tree ADT to include a
public method singleParent-Count that returns the number of nodes
in the tree that have only one child.
3) The Binary search tree ADT is extended to
include a boolean method similarTrees that receives references to
two binary trees and determines whether the shapes of the trees are...
In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
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...
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...