CS 10C Programming Concepts and Methodologies 2 2021/11/4 上午11:41 CS 10C Programming Concepts and Methodologies 2 https://daveteaches.com/10c/a12.shtml 1/2 CS 10C Programming Concepts and...

1 answer below »
Follow the instructions in assignment 12.pdf and complete all questions. Textbooks are included. NOTE: YOU MUST do this by yourself, and source of copying from online source will be detected and I will receive zero!! If I find that you copy from online, I have the reason to let you write again!!!


CS 10C Programming Concepts and Methodologies 2 2021/11/4 上午11:41 CS 10C Programming Concepts and Methodologies 2 https://daveteaches.com/10c/a12.shtml 1/2 CS 10C Programming Concepts and Methodologies 2 Assignment 12: Binary Search Trees Skip to Main Content Assignment 12.1 [10 points] Do Chapter 16 exercises 1 - 5. When performing a deletion, use the technique from the lesson (copy the largest value in root's left subtree), not the technique used in the text (copy the smallest value in root's right subtree). Note that exercise 4 calls Figure 15-19 a binary search tree, but it is not. Make the following changes to the tree before you start: Change the H to an E Change the U to an S Swap the T and the V Assignment 12.2 [40 points] Note: No documentation (comments) whatsoever are 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.) 1. mSize: Add a data member to store the size of the tree. Call it "mSize" to distinguish it from the "size()" function. You'll need to update this new data member in all of the appropriate places in the class implementation. Change the size() function so that it returns this data member instead of calculating the size. 2. numPrimes(): Add a "numPrimes()" function to the class that returns the number of nodes in the tree that contain prime integers. Good decomposition dictates that you will want a helper function that determines whether its parameter is prime. Don't worry about making this efficient. Just test all of the numbers less than the parameter and if any of them divide evenly into the parameter, then it's not prime. 3. toLL(): Add a "toLL()" function to the class that converts the calling binary tree object into an LL object (see the hint about working with the LL class below). The items in the LL object should be in increasing order. The created LL object should be returned. Hint: The LL class was developed in lesson 20.7. You may need to go back and study it before attempting this part of the assignment; however, in my solution the only member function I use is push_front(). Your solution will be a client of the LL class, so you shouldn't need to even look at the implementation of the LL class, you just need to understand how to use push_front(). Hint: With push_front(), the last item pushed becomes the first item in list, so traverse tree from greatest value to lowest value (right subtree before left), in order for list to be in ascending order. Here's a sample client to illustrate how this function might be used: #include #include "LL.h" #include "binarytree.h" using namespace std; int main() { binarytree myTree; for (int i = 0; i < 20;="" i++)="" {="" mytree.insert(rand()="" %="" 50);="" }="" cout="">< "the="" binary="" tree:="" ";="" mytree.print();="" cout="">< endl;="" 2021/11/4="" 上午11:41="" cs="" 10c="" programming="" concepts="" and="" methodologies="" 2="" https://daveteaches.com/10c/a12.shtml="" 2/2=""> myList; myList = myTree.toLL(); cout < "the="" linked="" list:="" ";="" if="" you="" haven't="" studied="" iterators="" yet,="" the="" following="" may="" not="" make="" sense="" don't="" worry.="" you="" just="" have="" to="" know="" that="" this="" for="" loop="" prints="" the="" contents="" of="" mylist.="" for="">::iterator i = myList.begin(); i != myList.end(); i++) { cout < *i="">< "="" ";="" }="" cout="">< endl;="" cout="">< "the="" original="" binary="" tree="" still="" intact:="" ";="" mytree.print();="" cout="">< endl;="" }="" 4.="" big-3:="" finally,="" add="" the="" big-3="" to="" the="" class.="" first="" add="" private="" static="" functions="" named="" "copy()"="" and="" "clear()".="" because="" these="" functions="" are="" being="" called="" from="" within="" the="" class="" (instead="" of="" being="" called="" by="" the="" client),="" you="" won't="" need="" separate="" recursive="" "copy_aux()"="" and="" "clear_aux()"="" functions.="" instead,="" "copy()"="" and="" "clear()"="" will="" themselves="" be="" recursive.="" if="" you="" write="" these="" two="" functions="" correctly,="" your="" big-3="" will="" be="" trivial.="" "copy()"="" should="" be="" a="" static="" void="" function="" with="" two="" treenode*="" parameters;="" it="" assigns="" a="" deep="" copy="" of="" the="" second="" parameter="" to="" the="" first="" parameter.="" it="" will="" be="" called="" from="" your="" copy="" constructor="" and="" from="" your="" assignment="" operator.="" "clear()"="" should="" be="" a="" static="" void="" function="" that="" deallocates="" the="" entire="" binary="" tree="" pointed="" to="" by="" its="" single="" treenode*="" argument.="" it="" will="" be="" called="" from="" your="" destructor="" and="" from="" your="" assignment="" operator.="" submit="" your="" work="" submit="" your="" three="" files="" ("binarytree.h"="" and="" "binarytree.cpp",="" and="" a13.1="" image).="" no="" client="" code="" or="" output="" is="" necessary.="" when="" you="" submit="" your="" assignment="" there="" will="" be="" a="" text="" field="" in="" which="" you="" can="" add="" a="" note="" to="" me="" (called="" a="" "comment",="" but="" don't="" confuse="" it="" with="" a="" c++="" comment).="" in="" this="" "comment"="" section="" of="" the="" submission="" page="" let="" me="" know="" whether="" the="" program="" works="" as="" required.="" ©="" 1999="" -="" 2021="" dave="" harden="" cs="" 10c="" programming="" concepts="" and="" methodologies="" 2="" 2021/11/4="" 上午11:40="" cs="" 10c="" programming="" concepts="" and="" methodologies="" 2="" https://daveteaches.com/10c/23.shtml="" 1/4="" cs="" 10c="" programming="" concepts="" and="" methodologies="" 2="" lesson="" 23:="" binary="" search="" trees="" skip="" to="" main="" content="" below="" is="" the="" code="" that="" is="" developed="" in="" this="" lesson's="" videos.="" i="" strongly="" recommend="" that="" you="" watch="" the="" videos.="" unfortunately,="" the="" name="" of="" the="" class="" used="" here="" and="" in="" the="" videos="" is="" "binarytree"="" even="" though="" it="" should="" be="" "binarysearchtree".="" i'm="" keeping="" the="" name="" the="" same="" here="" to="" avoid="" confusion="" between="" the="" code="" below="" and="" the="" code="" developed="" in="" the="" videos.="" note="" that="" a="" major="" difference="" between="" this="" code="" and="" that="" developed="" in="" the="" text="" is="" that="" this="" code="" represents="" the="" treenode="" with="" a="" simple="" struct="" nested="" inside="" the="" binarytree="" class.="" this="" makes="" the="" code="" in="" the="" text="" far="" more="" complex.="" there's="" no="" problem="" with="" making="" the="" data="" inside="" the="" treenode="" struct="" public,="" because="" that="" struct="" is="" in="" the="" private="" area="" of="" the="" class.="" in="" fact,="" we="" could="" safely="" keep="" the="" data="" public="" even="" if="" the="" treenode="" struct="" was="" not="" nested="" inside="" the="" binarytree="" class,="" because="" these="" treenode="" objects="" will="" be="" fully="" protected="" by="" their="" being="" private="" members="" of="" the="" tree="" class="" in="" which="" they="" are="" used.="" (the="" loceff="" lesson="" uses="" this="" approach="" of="" declaring="" a="" treenode="" class="" with="" all="" public="" data="" members="" but="" outside="" of="" the="" tree="" class.)="" here="" is="" the="" file="" binarytree.h="" #ifndef="" binarytree_h="" #define="" binarytree_h="" #include=""> // 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; static void insert_aux(treenode*& root, int item); static void print_aux(const treenode* root); static size_type size_aux(const treenode* root); static int find_aux(const treenode* root, int target, bool& found); static void del_aux(treenode*& root, int target, bool& found); static void remove_max(treenode*& root, int& max); }; #endif // Here is the file binarytree.cpp #include #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); 2021/11/4 上午11:40 CS 10C Programming Concepts and Methodologies 2 https://daveteaches.com/10c/23.shtml 2/4 } 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 binarytree::del_aux(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) { 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 binarytree::remove_max(treenode*& root, int& max) { if (root -> right == nullptr) { max = root -> data; treenode* tempptr = root; root = root -> left; delete tempptr; } else { remove_max(root -> right, max); } } int binarytree::find_aux(const 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 binarytree::size_aux(const treenode* root){ if (root == nullptr) { return 0; } else { return size_aux(root -> left) + size_aux(root -> right) + 1; } } void binarytree::insert_aux(treenode*& root, int item) { if (root == nullptr) { root = new treenode; root -> data = item; root -> left = nullptr; root -> right = nullptr; } else if (item <= root="" -=""> data) { insert_aux(root -> left, item); } else {
Answered 2 days AfterNov 04, 2021

Answer To: CS 10C Programming Concepts and Methodologies 2 2021/11/4 上午11:41 CS 10C Programming Concepts and...

Bikram answered on Nov 07 2021
130 Votes
// Here is the file binarytree.cpp
#include
#include "binarytree.h"
using namespace std;
int prime=0;
int binarytree::mSize=0;

binarytree::binarytree()
{
root = nul
lptr;
}
binarytree::binarytree(binarytree &obj)
{

Copy(obj.root);

}
binarytree::~binarytree()
{
Clear(root);
}
void binarytree::print() const
{
print_aux(root);
}
void binarytree::insert(int item)
{
insert_aux(root, item);
}
int binarytree::size() const
{

return mSize;
}
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);
}
int binarytree::numPrimes() const
{
numPrime_aux(root);
return prime;
}
void binarytree::del_aux(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)
{
treenode* tempptr = root;
root = root -> right;
delete tempptr;
found = true;
mSize=mSize-1;
}
else
{
int max;
remove_max(root -> left, max);
root -> data = max;
found = true;
mSize=mSize-1;
}

}
// pre: root != nullptr
void binarytree::remove_max(treenode*& root, int& max)
{
if (root -> right == nullptr)
{
max = root -> data;
treenode* tempptr = root;
root = root -> left;
delete tempptr;
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here