Design algorithms for the following functions and describe them in pseudocode. If you use auxiliary functions, describe them in pseudocode as well. Evaluate the runtime efficiency of your algorithms and express it in O( ) notation.
a. Union(T1.root, T2.root): T1.root and T2.root point to the roots of two B-trees. The function returns a pointer to the root of a B-tree containing every key contained in T1 or T2.
b. Intersection(T1.root, T2.root): T1.root and T2.root point to the roots of two B-trees. The function returns a pointer to the root of a B-tree containing every key contained in both of T1 and T2. Assume that every key value is unique in both T1 and T2.
Both Union and Intersection functions will keep T1 and T2 trees intact.
Code in C++ :
Union tree:
I am assuming each tree has distinct elements. Union creates a binary search tree such that every element in the union_binary tree is distinct and they are set of elements from T1 andT2.
Logic:
1. Basic Union_tree is is T1 (as T1 has all distinct elements), now I will pick one element X from T2 and search it in T1. If T1 has X, I will discard it, else X will be added into T1.
By examing the logic, it's clear that from all elements of T2 will be visited and in T1 tree, Search will be always in one-half of the elements. therefore time complexity: O(T2*log(T1))
Intersection: here, a brand new binary tree will be created:
Logic: T1 tree will be visited and each element X of T1 will search in T2, if found, it will be added in new Binary tree (the first common node will be root of this new tree), like Union, its time complexity: O(T1*log(T2))
Code in C++:
All necessary steps (logic flow including) are mentioned in the code.
#include <iostream>
#include <iomanip>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
};
class bst{
public:
node* intersect_root;
node* root;
//initially addNode() creates two binary tree with two different roots
// function calls itself recursively either left child or child depending upon nullptr.
// at the end, it adds the element into the tree.
node* addNode(int x, node* root){
if(root==nullptr){
node* nd=new node();
nd->data=x;
nd->left=nullptr;
nd->right=nullptr;
root=nd;
return(root);
}else if(x<root->data){
root->left=addNode(x, root->left);
}else{
root->right=addNode(x, root->right);
}
return(root);
}
// print() function prints tree data in preorder fashion
void print(node* r){
if(r==nullptr){
return;
}
cout << r->data << endl;
print(r->left);
print(r->right);
}
// -------------union code starts------------------
void makeUnion(node* r, int data){
node* r1=r;
while(r!=nullptr){
if(data==r->data){
return;
}
if(data<r->data){
r1=r;
r=r->left;
}else{
r1=r;
r=r->right;
}
}
node* nd=new node();
nd->data=data;
nd->left=nullptr;
nd->right=nullptr;
if(data<r1->data){
r1->left=nd;
}else{
r1->right=nd;
}
}
void utility(node* r1, node* r2){
if(r2==nullptr){
return;
}
makeUnion(r1, r2->data);
utility(r1, r2->left);
utility(r1, r2->right);
}
node* Union(node* r1, node* r2){
utility(r1, r2);
return(r1);
}
// ---------------union code ends----------------
// -------------------Code for Intersection--------------
void final_intersect(int data, node* r){
while(r!=nullptr){
if(data==r->data){
intersect_root=addNode(data, intersect_root);
return;
}
if(data<r->data){
r=r->left;
}else{
r=r->right;
}
}
}
void utility_intersect(node* r1, node* r2){
if(r1==nullptr){
return;
}
final_intersect(r1->data, r2);
utility_intersect(r1->left, r2);
utility_intersect(r1->right, r2);
}
node* intersection(node* r1, node* r2){
// intersect_root is root of new binary intersection tree
intersect_root=nullptr;
utility_intersect(r1, r2);
return(intersect_root);
}
};
int main(){
bst* b=new bst();
b->root=nullptr;
b->root=b->addNode(1, b->root);
b->root=b->addNode(2, b->root);
b->root=b->addNode(11, b->root);
b->root=b->addNode(7, b->root);
b->root=b->addNode(12, b->root);
bst* b1=new bst();
b1->root=nullptr;
b1->root=b1->addNode(1, b1->root);
b1->root=b1->addNode(10, b1->root);
b1->root=b1->addNode(18, b1->root);
b1->root=b1->addNode(12, b1->root);
b1->root=b1->addNode(9, b1->root);
bst* newB=new bst();
newB->root=newB->intersection(b->root, b1->root);
newB->print(newB->root);
return 0;
}
Note: Just be sure about braces in the code. for Pseudocode,
Design algorithms for the following functions and describe them in pseudocode. If you use auxiliary functions,...
C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int>values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; Write a recursive function...
You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...
Objectives: - Practice designing/implementing algorithms - Practice use of functions Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them from a simple interactive main() function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu. The program should contain a loop...
Objectives: - Practice designing/implementing algorithms - Practice use of functions Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them from a simple interactive main() function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu. The program should contain a loop...
I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to the BST class in the file “bst.cpp” that I provided. Consider the following: 1. Change the declaration of treenode to class treenode //node in a BST { public: string county_name; double population_size; treenode *lchild, *rchild; //left and right children pointers }; 2. Make the following changes to the BST class: a) change the name from “BST” to “bst”; b) change default constructor from “BST”...
attention!!!!!!! I need python method!!!!!!!!!
the part which need to edit is below: i need python
one!!!!!!!!!
the part below is interface for the range search tree which
don’t need to modify it.
Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...
C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...