Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help me with the toLL() function. All of the details are list

ID: 3828146 • Letter: C

Question

Can someone please help me with the toLL() function. All of the details are listed below

No documentation is required on this assignment.

Start with the binaryTree class provided in lesson 23 and make the following changes. Don't make any changes other than the additions listed here. (Adding private helper functions is also ok.)

toLL(): Add a "toLL()" function to the class that converts the calling binary tree object into an LL object. The items in the LL object should be in increasing order. The created LL object should be returned. Here's a sample client to illustrate how this function might be used:

the class and implementation file are listed below:

#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <cstdlib>   // for size_t

class binarytree {

public:
   typedef std::size_t size_type;
   binarytree();
   void insert(int item);
   void print() const;
   size_type size() const;
   int find(int target, bool& found) const;
   void del(int target, bool& found);
private:
   struct treenode {
       int data;
       treenode* left;
       treenode* right;
   };

   treenode* root;
   friend void insert_aux(treenode*& root, int item);
   friend void print_aux(treenode* root);
   friend size_type size_aux(treenode* root);
   friend int find_aux(treenode* root, int target, bool& found);
   friend void del_aux(treenode*& root, int target, bool& found);
   friend void remove_max(treenode*& root, int& max);
};
#endif

//implementation

#include <iostream>
#include "binarytree.h"
using namespace std;

binarytree::binarytree() {
   root = nullptr;
}

void binarytree::print() const {
   print_aux(root);
}


void binarytree::insert(int item) {
   insert_aux(root, item);
}

binarytree::size_type binarytree::size() const {
   return size_aux(root);
}


int binarytree::find(int target, bool& found) const {
   return find_aux(root, target, found);
}


void binarytree::del(int target, bool& found) {
   del_aux(root, target, found);
}

void del_aux(binarytree::treenode*& root,
   int target,
   bool& found) {

   if (root == nullptr) {
       found = false;
   }
   else if (target < root->data) {
       del_aux(root->left, target, found);
   }
   else if (target > root->data) {
       del_aux(root->right, target, found);
   }
   else if (root->left == nullptr) {
       binarytree::treenode* tempptr = root;
       root = root->right;
       delete tempptr;
       found = true;
   }
   else {
       int max;
       remove_max(root->left, max);
       root->data = max;
       found = true;
   }
}

// pre: root != nullptr

void remove_max(binarytree::treenode*& root, int& max) {
   if (root->right == nullptr) {
       max = root->data;
       binarytree::treenode* tempptr = root;
       root = root->left;
       delete tempptr;
   }
   else {
       remove_max(root->right, max);
   }
}


int find_aux(binarytree::treenode* root,
   int target,
   bool& found) {

   if (root == nullptr) {
       found = false;
       return 0;
   }
   else if (target == root->data) {
       found = true;
       return root->data;
   }
   else if (target < root->data) {
       return find_aux(root->left, target, found);
   }
   else {
       return find_aux(root->right, target, found);
   }
}

binarytree::size_type size_aux(binarytree::treenode* root) {
   if (root == nullptr) {
       return 0;
   }
   else {
       return size_aux(root->left)
           + size_aux(root->right)
           + 1;
   }
}


void insert_aux(binarytree::treenode*& root, int item) {
   if (root == nullptr) {
       // root = new treenode(item, nullptr, nullptr);
       root = new binarytree::treenode;
       root->data = item;
       root->left = nullptr;
       root->right = nullptr;
   }
   else if (item <= root->data) {
       insert_aux(root->left, item);
   }
   else {
       insert_aux(root->right, item);
   }
}


void print_aux(binarytree::treenode* root) {
   if (root != nullptr) {
       print_aux(root->left);
       cout << root->data << " ";
       print_aux(root->right);
   }
}

//client file

// Here is the file binarytree-client.cpp

#include <iostream>
#include "binarytree.h"
using namespace std;

int main() {
   binarytree list;

   int num;
   bool found;

   cout << "enter number to insert (negative to quit): ";
   cin >> num;
   while (num >= 0) {
       list.insert(num);
       cout << "enter number to insert (negative to quit): ";
       cin >> num;
   }

   list.print();
   cout << endl;

   binarytree::size_type numItems;
   numItems = list.size();
   cout << "There are " << numItems << " items." << endl;

   cout << "enter a number to find (negative to quit): ";
   cin >> num;
   while (num >= 0) {
       int result = list.find(num, found);
       if (!found) {
           cout << "not found" << endl;
       }
       else {
           cout << "found. The data is " << result << endl;
       }
       cout << "enter a number to find (negative to quit): ";
       cin >> num;
   }


   cout << "enter a number to delete (negative to quit): ";
   cin >> num;
   while (num >= 0) {
       list.del(num, found);
       if (found) {
           cout << "the list is now ";
           list.print();
           cout << endl;
       }
       else {
           cout << num << " is not in the list." << endl;
       }
       cout << "enter a number to delete (negative to quit): ";
       cin >> num;
   }


   binarytree list2(list);

   cout << "Now list 2 should be a copy of list. Here it is: ";
   list2.print();
   cout << endl;


   list2.del(3, found);


   cout << "After deleting a 3 from list2, list2 is now: ";
   list2.print();
   cout << endl << "list should be unchanged. Here it is: ";
   list.print();
   cout << endl;

   list = list2;

   cout << "Now list has been assigned list2 so it should match list2. Here it is: ";
   list.print();
   cout << endl;

   list.del(4, found);

   cout << "After deleting a 4 from list, list is now: ";
   list.print();
   cout << endl << "list2 should be unchanged. Here it is: ";
   list2.print();
   cout << endl;

}

Explanation / Answer

//To sort binary tree objects into increasing order applying bubble sort and defining toLL function that can return //integer data/value

int toLL(int i;int j;tree node* &root,int temp)

{

i=root->data;

j=root->left;

for(i=0;i<n;i++)

for(j=0;j<n-1;j++)

{

//compare root with left child and return whatever value is greater

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

return temp;

else

if(a[i]<a[j])

temp=a[j];

a[j]=a[i];

a[i]=temp

}

return temp;

//convert binary tree object into LL Object

//define function convert that can return LL Object

int convet(LL<int> l,int data,tree node* &root,int *i)

{

&i=root->data;

LL <int> l=&i;

}

list.print();

}