1. The requested code is below:-
//----------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
void input(vector<int> &nums)
{
int n=0,num=0;
cout<<"Enter Limit: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter an Integer:
";
cin>>num;
nums.push_back(num);
}
}
void display(vector<int> nums)
{
for(int x: nums)
{
cout<<x<<" ";
}
cout<<endl;
}
void insertAfter(vector<int> &nums,int firstValue,int
secondValue)
{
int i=0;
for(int num: nums)
{
i++;
if(num==firstValue)
break;
}
nums.insert(nums.begin()+i,secondValue);
}
// BST node
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
typedef struct Node Node;
// Utility function to create a new Node
Node* createNode(int data)
{
Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
Node* insert(Node* node,int data)
{
if(node == NULL)
return createNode(data);
if (data <= node->data)
node->left =
insert(node->left, data);
else if (data > node->data)
node->right =
insert(node->right, data);
return node;
}
// Function to perform inorder traversal
void inorder(Node* root)
{
if (root == NULL)
return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
// Function to check if two BSTs
// are identical
int isIdentical(Node* root1, Node* root2)
{
// Check if both the trees are empty
if (root1 == NULL && root2 == NULL)
return 1;
// If any one of the tree is non-empty
// and other is empty, return false
else if (root1 != NULL && root2 == NULL)
return 0;
else if (root1 == NULL && root2 != NULL)
return 0;
else
{ // Check if current data of both trees equal
// and recursively check for left and right subtrees
if (root1->data == root2->data &&
isIdentical(root1->left, root2->left) &&
isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}
Node* buildTree(Node *root,vector<int> nums)
{
for(int x: nums)
{
root=insert(root,x);
}
return root;
}
int sum(Node*root)
{
if (root == NULL)
return 0;
else
return (root->data +
sum(root->left) + sum(root->right));
}
int compareTree(Node* root1, Node* root2)
{
if(isIdentical(root1,root2))
{ // if both trees are same return zero
return 0;
}
else
{
int sum1=sum(root1); // sum of
first tree
int sum2=sum(root2); // sum of
second tree
if(sum1>sum2)
{
return 1;
}
else if(sum1<sum2)
{
return -1;
}
else // sum1 == sum2
{
return 2;
}
}
}
int main(int argc,char**argv)
{
int cmp=0;
Node *root1=NULL;
Node *root2=NULL;
// Part 1 :-
vector<int> nums(0);
input(nums);
insertAfter(nums,6,5);
cout<<"Vector of Integers:-"<<endl;
display(nums);
// Part 2 :-
root1 = buildTree(root1,nums);
cout<<"Binary Tree 1 :-"<<endl;
inorder(root1);
cout<<endl;
root2 = insert(root2,9);
root2 = insert(root2,8);
root2 = insert(root2,7);
root2 = insert(root2,6);
root2 = insert(root2,5);
cout<<"Binary Tree 2 :-"<<endl;
inorder(root2);
cout<<endl;
cmp = compareTree(root1,root2);
cout<<"Result = "<<cmp<<endl;
return 0;
}
//----------------------------------------------------------------------------------------
2.Screenshot of the output:-
C++ Vectors and Binary Search Trees • Write a program that takes from the user n...
Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...
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,...
C++ Binary Search trees Depth first traversal is the same as _____ order traversal. When we add a new node to an existing binary search tree, it will always become a ______. When we delete a node with 2 children from a binary search tree, we replace the node with ______.
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...
Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...
implement a binary search function in 3 programming languages. In each program (identical, except for the programming language), carry out the same 20,000,000 unsuccessful searches for eight different-sized arrays, namely arrays of sizes 128, 512, 2048, 8192, 32768, 131072, 524288, and 2,097,152. Measure in each of the three programs the time it takes to do the 20,000,000 searches for each of the eight arrays. Compare these timings to the theoretical timings the algorithm binary search provides. Are there differences between...
LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...
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...
LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.
Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...