Question

Design algorithms for the following functions and describe them in pseudocode. If you use auxiliary functions,...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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,

Add a comment
Know the answer?
Add Answer to:
Design algorithms for the following functions and describe them in pseudocode. If you use auxiliary functions,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ 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...

    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...

    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...

    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...

  • Name the program for this assignment "bst_driver.cpp." In this assignment you will make several modifications to...

    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 nee...

    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...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT