class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class BST {
Node root;
BST() {
root = null;
}
Node insert(Node root, int data) {
// If the root is null than tree
is empty, new node will become root
if (root == null) {
root = new
Node(data);
return
root;
}
// if data is < root go
left
if (data < root.data)
root.left =
insert(root.left, data);
// if data > root go right
else if (data > root.data)
root.right =
insert(root.right, data);
return root;
}
public boolean isIdentical(Node a, Node b)
{
//If both trees are empty than return true
if (a == null && b == null)
return true;
// non empty than compare the data of each node
// by iterating the left and right sub tree
if (a != null && b != null)
{
return isIdentical(a.left, b.left) &&
isIdentical(a.right, b.right) && a.data == b.data ;
}
return false;
}
}
public class BstIdentical{
public static void main(String[] args) {
BST tree = new BST();
tree.root=tree.insert(tree.root,
10);
tree.root=tree.insert(tree.root,
15);
tree.root=tree.insert(tree.root,
12);
tree.root=tree.insert(tree.root,
8);
BST tree1 = new BST();
tree1.root=tree1.insert(tree1.root,
10);
tree1.root=tree1.insert(tree1.root,
15);
tree1.root=tree1.insert(tree1.root,
12);
tree1.root=tree1.insert(tree1.root,
8);
//compring the both tres
System.out.println(tree.isIdentical(tree.root,tree1.root));
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Given two binary tree, can you determine if they are identical or not? Write a program...
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...
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...
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,...
WİTH C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 125 pointsl Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, rightChild). Make content random. 60 50 40 60 70 30 45 42 b) 125 points Display the trees on screen the way we did in slide 9 using listAll...
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...
Program in JAVA: A Perfect binary tree is a complete binary tree with all levels fully filled. Add a method in the BST class to return true if the tree is a perfect binary tree.(Hint: The number of nodes in the nonempty perfect binary tree is 2 raised to the power of height - 1) (/** returns true if the tree is a perfect binary tree, boolean isPerfectBST() **/)
CAN SOMEONE HELP! The following problem is to design an algorithm which check if a binary tree is a binary search tree. The following code was given. There exists a bug in this code for the variable last printed. (20 points) 6. Find the bug and provide a way to fix this bug: public static Integer last printed-null: public static boolean checkBST (TreeNode n) if (nnull) return true // check/ recurse left if (checkBST (n.left)) return false; /I check current...
Write a java program for creating a binary search tree from an empty tree where you must consider 10 data items to form the tree.
IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...
Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very high load factors...