Question

In: Computer Science

How would I create a program in C++ where you build and maintain two binary search...

How would I create a program in C++ where you build and maintain two binary search trees of information?

I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations:

L -- for launching a satellite, which will save it's info to the first set or

D -- for deorbit the satellite.

The other operations I'm looking to implement are:

F -- for find, where user inputs the satelite name and the program searches the trees and displays a message if the satellite was found launched (in orbit) or if it is no longer in orbit.  

A -- for listing all satellites in each tree in order of names.

S -- for save the satellites in two files in a format suitable to be read in by your program later. The first filename should be “inorbit.txt” and the second filename should be “deorbit.txt”

R -- for read in information from the two files produced by a save and builds a new orbital and non-orbital tree. Any existing trees are deallocated before the new trees are built.

and

Q -- for quit. Quits the program and exits. It does NOT save the trees.

Here is the satellite reader I have created thus far:

int main()
{
ifstream inFile;
string name, country, who, classification, purpose,
detail, orbit, dateLaunched, launchSite, launchVehicle, expectedLife, junk;
int noradNumber;
float apogee, perigee, period;

inFile.open("dataset1.txt");
if (inFile.is_open()) {
while (inFile.good()) {
getline(inFile, name);
if (inFile.bad() || inFile.eof()) { // end of data
inFile.close();
break;
}
getline(inFile, country);
getline(inFile, who);
getline(inFile, classification);
getline(inFile, purpose);
getline(inFile, detail);
getline(inFile, orbit);
inFile >> apogee >> perigee >> period;
getline(inFile, junk); // move past the newline after period
getline(inFile, dateLaunched);
getline(inFile, expectedLife); // floating Point or blank line.
getline(inFile, launchSite);
getline(inFile, launchVehicle);
inFile >> noradNumber;
getline(inFile, junk); //move past the newline after the norad number
}

}
else {
cerr << "Couldn't open dataset1.txt for reading\n";
}
return EXIT_SUCCESS;
}

Solutions

Expert Solution

fresh code is here to merge two binary trees.

// c++ program to merge two balanced binary trees

#include<bits/stdc++.h>

using namespace std;

/* A binary tree node has data, pointer to left child and a pointer to right child */

class node

{

public:

int data;

node* left;

node* right;

};

// A utility unction to merge two sorted into one

int *merge(int arr1[], int arr2[],int m, int n);

// Ahelper function that stores inorder

// traversal of a tree in inorder array

void storeInorder(node* node, int inorder[],

int *index_ptr);

/* A function that contructs balanced

binary search tree from a sorted array

node* sortedArrayToBST(int arr[], int start, int end);

/* this function merges two balanced

BSTs with roots as root1 and root2.

m and n are the sizes of the trees respectively */

node* mergeTrees(node *root1, node *root2, int m, int n)

{

// store inorder traversal of

// first tree in an array arr1[]

int *arr1 = new int[m];

int i =0;

storeInorder(root1, arr1, &i);

// store inorder traversal of second

// tree in another array arr2[]

int *arr2 = new int[n[;

int j = 0;

storeInorder(root2, arr2, &j);

// merge the two sorted array into one

int *mergedArr = merge(arr1, arr2, m, n);

// construct a tree from the merged

// array and return root of the tree

return sortedArrayToBST (mergedArr, 0, m + n - 1);

}

/* helper function that allocates a new node with the given data and null left and right pointers .*/

node* newNode(int data)

{

node* node = new node();

node->data = data;

node->left = null;

node->right = null;

return(node);

}

// a utility function to print inorder

// traversal of a given binary tree

void printInorder(node* node)

{

if (node == null)

return;

/* first recur on left child */

printIorder(node->left);

count << node->data << " ";

/* now recur on right child */

printInorder(node->right);

}

// a utility function to merge

// two sorted arrays into one

int *merge(int arr1[], int arr2[], int m, int n)

{

// mergedArr[] is going to contain result

int *mergeArr = new int[m + n];

int i = 0, j = 0, k = 0;

// traverse trough both arrays

while (i , m && j < n)

{

// pick the smaler aelement and put it in mergedArr

if (arr1[i] < arr2[j])

{

mergedArr[k] = arr1[i];

i++;

}

else

{

megedArr[k] = arr2[j];

j++;

}

k++;

}

// if there are more elements in first array

while (i < m)

{

mergedArr[k] = arr1[i];

i++; k++;

}

// if there are more elements in second array while (j < n)

{

mergedArr[k] = arr2[j];

j++; k++;

}

return mergedArr;

}

//a helper function that stores in order

// traversal of a tree rooted with node

void storeIorder(node* node, int inorder[], int *index_ptr)

{

if (node == NULL)

return;

/* first recur on left child */

storeInorder(node->left, inorder, index_ptr);

inorder[*index_ptr] = node->data;

(*index_ptr)++;

// increase index for next entry

/* now recur on right child */

storeInorder(node->right, inorder, index_ptr_);

}

/* afunction that constructs balanced

// binary search tree from a sorted array

node* sortedArrayToBST(int arr[], int start, int end)

{

/* base case */

if (start > end)

return NULL;

/* get the middle element and make it root */

int mid = (start + end)/2;

node *root = newnode(arr[mid]);

/* recursively construct the left subtree and make it left child of root */

root->left = sortedArrayToBST(arr, start, mid-1);

root->right = sortedArrayToBST(arr, mid+1, end);

retuen root;

}

/* driver code*/

int main

{

/* create folloeing tree as first balanced BST

node *root1 = newnode(100);

root1->left = newnode(50);

root1->right = newnode(300);

root1->left->left = newnode(20);

root1->left->right = newnode(70);

/* create second balanced BST*/

node *root2 = newnode(80);

root2->left = newnode(40);

root2->right = newnode(120);

node *mergedTree =mergeTrees(root1, root2, 5, 3);

count << "following is Inorder traversal of the merged tree \n";

printInorder(mergedTree);

return 0;

}


Related Solutions

This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT