100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

COS assignment 2 solution

Rating
5.0
(3)
Sold
17
Pages
15
Uploaded on
13-07-2023
Written in
2022/2023

COS assignment 2 solution with source code and link to the zipped files. Create a C++ program that will display a binary search tree: inorder traversal, preorder traversal, postorder traversal and height of the tree. The nodes of the binary search tree will be read into the code using a text file. Download from Additional Resources, the files for Assessment 2, Question 1. The file COS2611 AS2 BST S contains a skeleton framework for binary search trees (BST) and two test files. Notice that in main() are the calling procedures to the classes BST and TreeNode. Use the skeleton as a starting point for you. The insertNode function is given to you. This will help you to get started. You may not change the following classes and procedures: • class TreeNode • readFromFileData(string thePath) • main() You must use main() as is. The marker will use the same code and constructs as in main() to call the objects in the classes. What you should provide: the code for the functions (methods): (a) heightOfTree(5) (b) printPreorder(5) (c) printInorder(5) (d) printPostorder Question 2 Company XFibre, is requested by the municipality to install fibre in a new neighbourhood. The company is constrained to bury the fibre cable only along certain paths. Some of the paths might be more expensive. The cost is represented as a weight on the edge. You need to write a C++ program that will determine the minimum spanning tree that will represent the least expensive path for laying the fibre cable. To solve the above problem for Company XFibre, you may use AI (artificial intelligence) to help. As you are aware AI tools, such as ChatGPT1 , are becoming a valuable tool to help programmers in saving valuable time in the development of code for specific problems. Use an AI tool and generate C++ code that will implement Prim’s algorithm using ADT with a time complexity of O(n2 ). After you have downloaded the code from the AI tool, you will need to make some changes. 1. Insert as a comment your student name and number. 2. Add as a comment the question that you have asked the AI tool to generate the code for you. 3. Add as comments in your code, the updates and changes that you are making. This is important as the marker needs to see your input, changes, and updates to the code to make it work for this case study. Adapt the code to allow you to read from a text file the test data. To test your code, download from Additional Resources, Assessment 2, Question 2 the file and copy it to your C-drive: data folder (the same folder as in Question 1). The path in your code should refer to this text file. As with Question 1, the marker will use a different set of data to test your code. The first value in , represents the number of vertices, and the second value is the number of edges. The values thereafter represent combinations of (vertex, edge, and weight)

Show more Read less
Institution
Module











Whoops! We can’t load your doc right now. Try again or contact support.

Connected book

Written for

Institution
Module

Document information

Uploaded on
July 13, 2023
Number of pages
15
Written in
2022/2023
Type
Other
Person
Unknown

Subjects

Content preview

COS2611 2023 ASSIGNMENT 2
SOLUTION(100% working)
Link to the zipped file is provided at the end of this
document

Crystal Indigo!
Crystal Indigo!
Providing all solutions you need anytime
+27 76 626 8187




Copy and run the code and submit what you are suppose to submit

,Outputs:

,Question 1
// COS2611 Skeleton for Assessment 3
#include <iostream>
#include <fstream>
#include <string>
#include<vector>

//Add your student name and student number
//
//
//



using namespace std;

class TreeNode
{
//keep this class as is
public:

int value; //key or data
TreeNode* left;
TreeNode* right;

//constructors

TreeNode() {
value = 0;
left = NULL;
right = NULL;
} //default constructor

TreeNode(int v) {
value = v;
left = NULL;
right = NULL;
} //parametrized constructor

}; //TreeNode



class BST
{
public:
TreeNode* root;

//member functions

BST() {
root = NULL;

, }

bool isTreeEmpty()
{
if (root == NULL)
return true; //the tree is empty
else
return false; //the tree is not empty
}

void insertNode(TreeNode* new_node) {
if (root == NULL) {
root = new_node;
} //insert the root node
else {
TreeNode* temp = root;
while (temp != NULL) {
if (new_node->value == temp->value) {
cout << "Value Already exist," <<
"Insert another value!" << endl;
return;
}
else if ((new_node->value < temp->value) && (temp->left == NULL)) {
temp->left = new_node; //insert value to the left
break;
}
else if (new_node->value < temp->value) {
temp = temp->left;
}
else if ((new_node->value > temp->value) && (temp->right == NULL)) {
temp->right = new_node; //insert value to the right
break;
}
else {
temp = temp->right;
}
}
}
} //insertNode
£4.06
Get access to the full document:
Purchased by 17 students

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Reviews from verified buyers

Showing all 3 reviews
2 year ago

Running and working perfectly

2 year ago

2 year ago

5.0

3 reviews

5
3
4
0
3
0
2
0
1
0
Trustworthy reviews on Stuvia

All reviews are made by real Stuvia users after verified purchases.

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
CrystalIndigo University of South Africa (Unisa)
Follow You need to be logged in order to follow users or courses
Sold
486
Member since
5 year
Number of followers
226
Documents
73
Last sold
1 month ago
CrystalIndigo Solutions

providing all solutions to all computer science modules

4.1

51 reviews

5
27
4
13
3
6
2
1
1
4

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions