3rd semester files

This commit is contained in:
2024-02-22 14:26:13 -05:00
parent cd78e4d51b
commit 80a59b57a1
280 changed files with 220686 additions and 0 deletions

View File

@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -0,0 +1,33 @@
{
"files.associations": {
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"vector": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"fstream": "cpp"
}
}

28
3rd-Semester-Fall-2022/PDS/.vscode/tasks.json vendored Executable file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -0,0 +1,5 @@
{
"files.associations": {
"ostream": "cpp"
}
}

View File

@ -0,0 +1,47 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: September 19, 2022
* File Name: HW01_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 1 Problem 1
* Purpose: Recieve and perform an operation on 5 numbers
*****************************************************************/
#include <iostream>
#include <string>
#define ENTRIES 5
int main()
{
std::string entry;
int nums[ENTRIES];
int sum = 0, max, min;
// get entries
for(int i = 0; i < ENTRIES; i++)
{
std::cout << "Enter int (" << i+1 << "):\n";
std::cin >> entry;
nums[i] = std::stoi(entry);
}
min = nums[0];
max = nums[0];
std::cout << "Values Entered:";
for(int i = 0; i < ENTRIES; i++)
{
std::cout << " " << nums[i]; // repeat entries
min = (nums[i] < min) ? nums[i] : min; // update min to current min
max = (nums[i] > max) ? nums[i] : max; // update max to current max
sum += nums[i];
}
std::cout << "\nSUM:\t" << sum << std::endl;
std::cout << "MAX:\t" << max << std::endl;
std::cout << "MIN:\t" << min << std::endl;
std::cout << "AVG:\t" << (float)sum / ENTRIES << std::endl;
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Binary file not shown.

View File

@ -0,0 +1,140 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: September 23, 2022
* File Name: HW02_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 2 Problem 1
* Purpose: Sort and search through a vector of ints
*****************************************************************/
#include <iostream>
#include <vector>
/// @brief Sort an incoming vector via selection sort
/// @param vals Vector of random integers
/// @return Original values sorted in ascending order
std::vector<int> vectorSelectionSort(std::vector<int> vals);
/// @brief Find val in vals with linear search
/// @param val number to find
/// @param vals ints to look through
/// @return {index, # of comparisons}
std::vector<int> vecIntLinearSearch(int val, std::vector<int> vals);
/// @brief Find val in vals with binary search
/// @param val number to find
/// @param vals ints to look through
/// @return {index, # of comparisons}
std::vector<int> vecIntBinarySearch(int val, std::vector<int> vals);
/// @brief print a vector of ints
/// @param vals vector to print
/// @param isSorted whether or not the vector is known to be sorted
void printVecInt(std::vector<int> vals, bool isSorted);
int main()
{
std::vector<int> vals{
8284, 4663, 5670, 2284, 1197,
9109, 6547, 7652, 7925, 2421,
8717, 6976, 1671, 5533, 1170,
1112, 4921, 9702, 4177, 9821,
2454, 3211, 5594, 2417, 7294
};
printVecInt(vals, false);
vals = vectorSelectionSort(vals);
printVecInt(vals, true);
int sel;
std::cout << "Query a number:\n";
std::cin >> sel;
std::vector<int> linearS = vecIntLinearSearch(sel, vals);
if (linearS[0] >= 0)
std::cout << "Value " << sel << " was found with linear search at index " << linearS[0] << " with " << linearS[1]<< " comparisons.\n";
else
std::cout << "Linear search could not find " << sel << " after " << linearS[1] << " comparisons\n";
std::vector<int> binaryS = vecIntBinarySearch(sel, vals);
if (binaryS[0] >= 0)
std::cout << "Value " << sel << " was found with binary search at index " << binaryS[0] << " with " << binaryS[1]<< " comparisons.\n";
else
std::cout << "Binary search could not find " << sel << " after " << binaryS[1] << " comparisons\n";
return 0;
}
std::vector<int> vectorSelectionSort(std::vector<int> vals)
{
int temp; // throw-away variable for swapping
int mindex = 0; // index of the smallest number
for (int i = 0; i < vals.size() - 1; i++)
{
mindex = i;
// find index of smallest element
for (int j = i; j < vals.size(); j++)
if (vals[j] < vals[mindex])
mindex = j;
// swap element at i with smallest element
if (mindex > i)
{
temp = vals[mindex];
vals[mindex] = vals[i];
vals[i] = temp;
}
}
return vals;
}
void printVecInt(std::vector<int> vals, bool isSorted)
{
const int COLS = 5;
std::cout << "Values (" << ((isSorted) ? "" : "un") << "sorted):\n";
for (int i = 0; i < vals.size() / COLS + 1; i++)
{
for (int j = 0; j < COLS && i*COLS + j < vals.size(); j++)
std::cout << vals[i*COLS + j] << ", ";
std::cout << std::endl;
}
}
std::vector<int> vecIntLinearSearch(int val, std::vector<int> vals)
{
for (int i = 0; i < vals.size(); i++)
{
if (val == vals[i])
return std::vector<int>{i, i};
}
return std::vector<int>{-1, (int)vals.size()};
}
std::vector<int> vecIntBinarySearch(int val, std::vector<int> vals)
{
int min = 0, mid = (int)vals.size()/2, max = (int)vals.size() - 1;
bool found = false;
int comps = 0;
while (min != max)
{
comps++;
// short circuit find
if (vals[mid] == val)
return std::vector<int>{mid, comps};
// shrink search space to top half of remaining set
else if (vals[mid] < val)
{
min = mid + 1;
mid = (max + min)/2;
}
// shrink search space to bottom half of remaining set
else
{
max = mid - 1;
mid = (max + min)/2;
}
}
// if val is at the last remaining position, it has been found; else it has been proven to not be in the list
return (vals[mid] == val) ? std::vector<int>{mid, comps} : std::vector<int>{-1, comps};
}

View File

@ -0,0 +1,5 @@
{
"files.associations": {
"string": "cpp"
}
}

View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Binary file not shown.

View File

@ -0,0 +1,110 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: October 10, 2022
* File Name: HW03_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 3 Problem 1
* Purpose: Read, sort and write, ints from text files
*****************************************************************/
#include <iostream>
#include <fstream>
#include <string>
/// @brief sort a dynamically allocated array in-place
/// @param arrptr pointer to first element in dynamically allocated array
/// @param len length of dynamically allocated array
/// @return true if the array was sorted successfully
bool dynamicArraySelectionSort(int *arrptr, int len);
int main()
{
std::string fname;
std::string line;
std::ifstream readfile;
std::ofstream writefile;
std::cout << "Input file name:\n";
std::cin >> fname;
readfile.open(fname);
std::getline(readfile, line);
int num_count = std::stoi(line);
std::cout << "Reading " << num_count << " numbers...\n";
int *nums_ptr;
nums_ptr = new int[num_count];
// read numbers from file
// numbers are multi-digit and separated by either a space or newline char
int idx = 0;
while (std::getline(readfile, line))
{
std::string snum;
for (char c : line)
{
if (isdigit(c))
snum.push_back(c);
else if (c == ' ' || c == '\n')
{
*(nums_ptr + idx) = std::stoi(snum);
snum = "";
idx++;
}
}
}
readfile.close();
std::cout << "Done reading values.\n";
// sort numbers
std::cout << "Sorting values...\n";
dynamicArraySelectionSort(nums_ptr, num_count);
std::cout << "Enter filename for sorted values:\n";
std::cin >> fname;
writefile.open(fname);
// write sorted nums to file with 8 columns
std::cout << "Writing values to " << fname << "...\n";
for (int i = 0; i < num_count/8 + 1; i++)
{
for (int j = 0; j < 8 && i*8 + j < num_count; j++)
writefile << *(nums_ptr + i*8 + j) << " ";
writefile << "\n";
}
writefile.close();
std::cout << "Done writing values.\n";
// clear dynamically allocated pointers
delete [] nums_ptr;
nums_ptr = nullptr;
return 0;
}
bool dynamicArraySelectionSort(int *arrptr, int len)
{
int temp; // throw-away variable for swapping
int mindex = 0; // index of the smallest number
for (int i = 0; i < len - 1; i++)
{
mindex = i;
// find index of smallest element
for (int j = i; j < len; j++)
if (*(arrptr + j) < *(arrptr + mindex))
mindex = j;
// swap element at i with smallest element
if (mindex > i)
{
temp = *(arrptr + mindex);
*(arrptr + mindex) = *(arrptr + i);
*(arrptr + i) = temp;
}
}
return true;
}

View File

@ -0,0 +1,61 @@
1200
7208 77654 85070 28533 87834 22178 52619 85517 19695 3458 32378 17737 72813 72802 68711 69388 80828 17885 4353 24392
39248 72336 83773 32282 28494 21754 720 17880 85790 72018 36274 9350 66024 31344 37884 63858 53523 6855 59375 73218
10314 1753 7307 89479 80907 76019 75219 78087 3904 79572 12479 49505 68260 12605 88139 6755 34359 88859 30987 36502
77229 73613 52204 53253 14958 88 27111 74833 13296 2838 64403 23610 4592 71711 29441 1851 64082 14661 79939 74338
10585 8770 33843 78846 27727 31982 1953 68439 30841 32940 14941 24422 16554 67145 77675 37864 73586 21138 22697 86882
23977 3452 26844 34921 75163 62637 36772 55597 77298 33063 39936 87884 48186 73779 76730 75913 15762 78683 60704 52955
27975 75645 77378 50881 59143 65053 88745 49081 86192 27794 52315 26521 31247 85511 61442 22762 58148 14566 78360 45447
53982 34648 49683 12168 18427 42765 4433 34189 37800 65138 87145 65775 57135 74523 26657 32630 55928 31754 81711 58472
59549 50378 84993 7148 45889 56435 36262 14038 77354 30974 65837 41336 65622 25520 59856 84050 68285 64289 34591 16085
45779 38088 88212 19267 28963 24869 51897 84892 56624 49961 53364 32525 10339 54710 39673 62581 27497 82287 76619 14851
23262 52456 56187 88884 84328 26043 89286 62613 6685 33878 85050 58816 71966 83262 78083 10930 24484 46333 12174 87460
12646 71890 29985 22985 36600 76010 1918 64098 68297 78537 78949 7911 47345 51489 13148 41673 77532 12434 20638 569
52664 22040 59386 40983 21655 53821 58265 46139 16506 70439 43599 29152 52329 79936 58490 5282 72298 60408 69380 50595
48946 64681 64859 12643 26170 84359 60669 20055 6793 87659 20624 65810 19700 86362 23145 41355 50184 81410 3846 66690
68201 47445 5843 36882 43733 70685 42164 26031 41093 27896 82978 6391 2578 57837 19035 28748 52196 79704 55155 65342
77363 75780 47504 13415 72142 70649 54770 38678 68411 58616 21721 52964 22413 33916 89846 72498 14601 48363 8529 55694
76259 7860 62086 85189 72049 87473 30290 40598 77177 85445 22292 70892 71225 69796 84308 59720 56797 55430 14750 41560
30399 36471 4524 59164 70387 4370 41663 84988 52733 56544 50683 45345 64404 29121 40534 46454 26594 70824 87052 20123
72622 19344 1015 53847 5492 1675 29919 68641 57106 51022 20201 87505 87493 24725 56669 67881 35447 8332 69221 4533
71229 36256 49878 45633 65377 6764 8439 8323 83941 5491 28446 66563 31187 35814 36762 43031 37489 66682 21672 10947
34056 48225 8452 31549 72950 71474 15782 24750 86158 85004 29283 67387 37612 85513 29373 19342 2277 37812 27665 86218
49656 62464 69133 87195 14630 15896 40227 58471 88930 68251 69419 32986 26477 84223 64535 15779 72049 86670 40529 68208
88026 69812 45595 35638 71677 74968 54980 73955 29133 88998 76525 78789 67814 55659 75984 88796 71555 26211 57267 70485
10815 43038 19823 37292 43614 84358 53071 25663 87380 3601 10223 85406 79765 55819 31045 61443 47139 2377 51750 76272
7727 38275 71413 81893 10286 57398 80689 88193 89961 54309 68678 10776 13699 4853 54420 63665 5564 17492 89329 2944
27445 9552 88351 23562 71723 35748 85005 35215 44477 53107 27839 58557 7735 9253 50450 18021 73003 47492 16215 79316
18153 1245 93 38204 6099 54513 11870 11663 78357 17551 20959 15802 33455 25662 39365 15179 61410 40722 56746 22240
10182 84585 80797 17917 10190 47599 35938 89545 5091 58505 78862 29596 59751 85307 67801 72202 49820 79671 217 44530
7222 21176 66684 47029 46839 22401 62208 24601 63124 35306 53193 73306 36244 43990 7575 46434 1590 43513 45980 13033
18371 34842 42630 84474 36501 20431 66676 86321 16454 73245 47203 30028 10773 23888 77057 57612 46289 55618 88566 25765
924 51759 15423 37168 12102 22998 89955 20044 72864 52287 33077 1235 3481 82059 85709 39982 18842 68737 42655 41648
51982 89859 71676 62755 23747 65086 36720 76388 30704 41638 12154 37980 9749 27577 81501 21851 50576 81456 41895 33440
50095 81325 34675 53576 73384 36736 3558 8579 15473 52565 50227 73807 52424 31904 52914 82523 13342 89634 68912 50398
47624 81066 88378 57374 24995 86231 79225 75571 84039 37473 25363 44134 28798 60038 14062 18534 13126 17620 27113 34951
70186 77341 18758 38962 25597 78025 31486 38939 84011 16750 89337 41636 14168 4067 9010 39163 299 4587 24735 84338
42060 50098 44825 70858 26489 58887 5745 45967 76508 32858 80919 63046 20199 16029 18360 45796 4054 56198 1087 88066
72948 424 39702 87116 10844 55064 36280 11143 59651 67367 11833 18064 33817 56658 5274 60306 25546 11019 22626 18406
43878 13545 81452 70429 29574 16164 32578 39981 72363 33665 38047 55311 40442 84101 52428 51286 49165 5060 68781 25168
72427 80614 43232 16244 47273 48507 82903 72819 65878 15529 1225 19756 35426 89029 6538 71352 21545 39116 21333 3908
79133 65732 59220 35927 59833 28000 87213 25350 33060 72346 50519 21839 62961 10103 44435 26586 64962 37338 9405 40841
59219 16982 66949 4645 22363 73487 82350 43908 22603 13683 47817 18089 85768 23389 54016 55601 57741 57582 87304 7153
39928 54175 28992 19241 64278 73427 45827 45593 27118 61584 86434 86337 78566 69735 7335 17281 53223 89685 61190 82178
19720 25359 10267 15488 55100 70636 77442 29193 38218 74746 36346 84498 45273 71690 13740 25903 61469 59567 71496 4939
37504 74282 1277 26070 54018 14964 49704 17241 21001 20894 15771 40721 52605 26039 62562 24057 13027 56356 53250 57597
41102 5948 52095 86375 77638 72187 28630 49107 48107 10127 54047 85611 84409 61676 28033 54779 82992 77737 72020 20345
14983 87792 67418 67588 30183 39980 7997 49562 6336 67599 17159 53790 73547 69254 56517 61185 57794 85148 26645 15901
11627 87044 11512 6036 65072 39545 67168 64416 27283 49188 84761 48618 53332 62179 26207 89867 18512 40556 49429 24848
18156 72940 84991 1703 52195 51508 69241 19989 53008 12238 42242 64635 15634 60106 77024 87058 9651 54192 61474 43286
19732 62587 1905 79417 34766 34464 85636 53278 81372 51418 84479 9528 34358 85822 17584 2905 47330 3177 29246 16691
15415 71488 81326 31049 41594 74702 34459 57598 38894 5933 17236 64979 68520 25493 60748 19638 59957 56384 79269 57682
17802 73748 67210 58513 69570 1146 61418 33252 4323 665 49943 19738 78505 47622 57139 30100 32324 1598 4050 77571
13883 21286 58902 88755 53132 29650 18394 29441 2386 14015 87123 26541 4115 70686 85054 80037 71832 62824 23289 76156
63489 79585 12246 58347 43559 75738 4799 82235 83688 8849 76158 7572 36487 45060 12679 89619 81062 31073 29061 83449
51440 32536 26342 55555 13222 21396 45592 85055 84220 75234 71211 64062 64819 89809 32409 24730 75547 43560 23317 69236
58761 9476 83160 5248 60888 5839 11220 51951 43265 46633 51752 4705 79169 78094 60261 2392 15842 15853 3799 10062
1087 81362 80476 72258 81171 29237 13340 66719 72797 36658 52307 41558 46134 45467 53159 17022 51306 70731 75325 4571
27364 43429 15629 22885 31523 75890 31629 47365 8095 35428 63780 9183 26790 60608 87793 17962 6198 11134 1033 85347
47792 53340 43258 10278 8807 6417 27300 66465 83500 18978 77389 20864 62407 3018 50101 10283 85260 81731 64000 3355
27159 44132 18890 53950 14741 23036 78264 20939 34170 79297 16286 88314 42637 59544 8592 57796 72313 42244 34261 65813
67574 28002 3029 39982 37372 53131 50265 32632 51214 30617 35988 78373 74750 61230 42323 5843 84266 36939 26782 34788

View File

@ -0,0 +1,7 @@
60
148 312 197 86 339 444 102 186 136 118
30 177 242 161 30 432 281 349 276 37
426 95 12 354 347 294 443 157 213 222
194 361 34 243 448 225 40 402 412 28
20 442 205 114 455 236 46 237 437 323
126 364 270 490 70 117 285 365 126 350

View File

@ -0,0 +1,26 @@
200
-2782 -25551 -8602 -15364 -12908 -23579 -25330 -22003
-2854 -1104 -12268 -1829 -4658 -26906 -24245 -29246
-24187 -25296 -5399 -2943 -2636 -825 -14677 -14568
-16981 -26283 -465 -23903 -8126 -15535 -180 -17260
-17438 -15135 -8977 -346 -15066 -10659 -22349 -17920
-11763 -10969 -26101 -22773 -14227 -20346 -22020 -14767
-15642 -27419 -17710 -24631 -4596 -2388 -9199 -21577
-28671 -9664 -21832 -6797 -1551 -28365 -409 -25341
-19852 -9386 -25687 -4918 -20045 -24388 -22838 -8160
-11709 -18939 -934 -25937 -15638 -29306 -10704 -7632
-3077 -4766 -2263 -7674 -7154 -11462 -5603 -5825
-27478 -27436 -18974 -5381 -2153 -19384 -722 -22005
-5122 -2761 -26923 -1520 -27149 -19761 -9680 -15211
-15052 -16966 -17500 -690 -16272 -4556 -8323 -19350
-9322 -16938 -3376 -16477 -28401 -8979 -22302 -2231
-12767 -17629 -7613 -14920 -13365 -8335 -6925 -24839
-11097 -3848 -26359 -14598 -29961 -12392 -29809 -21366
-29358 -23661 -28408 -21983 -28217 -6731 -17685 -13892
-22 -21061 -369 -4775 -40 -29023 -7006 -12808
-23004 -14619 -27728 -6369 -29307 -11006 -1209 -16756
-14854 -3920 -1354 -21168 -16312 -7516 -12534 -22023
-7529 -10942 -14006 -5747 -24026 -1691 -25991 -24048
-29104 -2712 -28823 -29144 -8087 -12181 -18304 -1092
-26801 -22385 -13813 -2460 -3391 -15022 -19216 -24597
-18943 -26922 -22117 -11607 -4438 -11003 -3630 -11968

View File

@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/c++/12"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -0,0 +1,7 @@
{
"C_Cpp.errorSquiggles": "Enabled",
"files.associations": {
"iostream": "cpp",
"string": "cpp"
}
}

View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -0,0 +1,12 @@
126 5 350 95 164 1 136 31 468 129
473 303 248 88 166 26 330 476 159 225
343 463 311 356 104 162 27 497 320 294
36 73 301 255 207 265 41 151 410 480
351 339 499 181 215 487 324 449 443 409
316 152 179 134 209 402 20 42 317 411
13 200 456 346 106 101 444 144 247 331
491 67 249 270 387 360 185 172 383 61
451 119 28 107 244 455 267 110 277 199
454 290 397 123 30 481 435 140 333 448
344 488 258 103 148 305 483 427 457 143
485 33 133 40 167 349 413 268 198 450

View File

@ -0,0 +1,41 @@
TARGET_EXEC ?= a.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p

Binary file not shown.

View File

@ -0,0 +1 @@
build/./src/HW04_Aidan_Sharpe.cpp.o: src/HW04_Aidan_Sharpe.cpp

View File

@ -0,0 +1,134 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: October 17, 2022
* File Name: HW04_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 4 Problem 1
* Purpose: Read values to a linked list and rewrite them in reversed order
*****************************************************************/
#include <iostream>
#include <string>
#include <fstream>
struct LL_Node
{
int val;
LL_Node *next;
LL_Node(int d, LL_Node *p=nullptr)
{
val = d;
next = p;
}
};
/// @brief Adds a new node to the end of a linked list starting at memory address head
/// @param head address of first node
/// @param val value to add
void AddNodeEnd(LL_Node *&head, int val);
/// @brief Adds a new node to the begining of a linked list starting at memory address head
/// @param head address of first node
/// @param val value to add
void AddNodeBegin(LL_Node *&head, int val);
/// @brief prints a linked list in format [m0, m1, ... , mn]
/// @param head address of first node
void PrintLL(LL_Node *head);
/// @brief write a linked list to a space-separated text file
/// @param head address of first list node
/// @param fname file path relative to source directory
void WriteLLToFile(LL_Node *head, std::string fname);
/// @brief read LL from space/newline separated file
/// @param head address of first list node
/// @param fname file path relative to source directory
/// @param reversed whether or not to reverse the order of numbers
void ReadLLFromFile(LL_Node *&head, std::string fname, bool reversed=false);
int main()
{
LL_Node *head = nullptr;
std::string in, out;
std::cout << "Input data file name (path relative to makefile directory):\n";
std::cin >> in;
ReadLLFromFile(head, in, true);
std::cout << "Output data file name (path relative to makefile directory):\n";
std::cin >> out;
WriteLLToFile(head, out);
return 0;
}
void AddNodeEnd(LL_Node *&head, int val)
{
if (head == nullptr)
head = new LL_Node(val);
else
{
LL_Node* p = head;
while (p->next != nullptr)
p = p->next;
p->next = new LL_Node(val);
}
}
void AddNodeBegin(LL_Node *&head, int val)
{
LL_Node *p = new LL_Node(val);
p->next = head;
head = p;
}
void PrintLL(LL_Node *head)
{
LL_Node *p = head;
std::cout << "[";
while (p != nullptr)
{
std::cout << p->val;
p = p->next;
std::cout << ((p != nullptr) ? ", " : "]\n"); // separate elements by ',' and add close bracket if it's the last node
}
}
void WriteLLToFile(LL_Node *head, std::string fname)
{
std::ofstream outfile;
outfile.open(fname);
LL_Node* p = head;
while(p != nullptr)
{
outfile << p->val << ' ';
p = p->next;
}
outfile.close();
}
void ReadLLFromFile(LL_Node *&head, std::string fname, bool reversed)
{
std::ifstream infile;
std::string line;
infile.open(fname);
while (std::getline(infile, line))
{
std::string snum;
for (char c : line)
{
if (isdigit(c))
snum.push_back(c);
else if (c == ' ' || c == '\n')
{
if (reversed)
AddNodeBegin(head, std::stoi(snum));
else
AddNodeEnd(head, std::stoi(snum));
snum = "";
}
}
}
infile.close();
}

View File

@ -0,0 +1,44 @@
TARGET_EXEC ?= a.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p
run:
./build/$(TARGET_EXEC)

View File

@ -0,0 +1,250 @@
Alaska
AK
Juneau
731,545
665,384
Alabama
AL
Montgomery
4,903,185
52,420
Arkansas
AR
Little Rock
3,017,804
53,179
Arizona
AZ
Phoenix
7,278,717
113,990
California
CA
Sacramento
39,512,223
163,695
Colorado
CO
Denver
5,758,736
104,094
Connecticut
CT
Hartford
3,565,278
5,543
Delaware
DE
Dover
973,764
2,489
Florida
FL
Tallahassee
21,477,737
65,758
Georgia
GA
Atlanta
10,617,423
59,425
Hawaii
HI
Honolulu
1,415,872
10,932
Iowa
IA
Des Moines
3,155,070
56,273
Idaho
ID
Boise
1,787,065
83,569
Illinois
IL
Springfield
12,671,821
57,914
Indiana
IN
Indianapolis
6,732,219
36,420
Kansas
KS
Topeka
2,913,314
82,278
Kentucky
KY
Frankfort
4,467,673
40,408
Louisiana
LA
Baton Rouge
4,648,794
52,378
Massachusetts
MA
Boston
6,892,503
10,554
Maryland
MD
Annapolis
6,045,680
12,406
Maine
ME
Augusta
1,344,212
35,380
Michigan
MI
Lansing
9,986,857
96,714
Minnesota
MN
St. Paul
5,639,632
86,936
Missouri
MO
Jefferson City
6,137,428
69,707
Mississippi
MS
Jackson
2,976,149
48,432
Montana
MT
Helena
1,068,778
147,040
North Carolina
NC
Raleigh
10,488,084
53,819
North Dakota
ND
Bismarck
762,062
70,698
Nebraska
NE
Lincoln
1,934,408
77,348
New Hampshire
NH
Concord
1,359,711
9,349
New Jersey
NJ
Trenton
8,882,190
8,723
New Mexico
NM
Santa Fe
2,096,829
121,590
Nevada
NV
Carson City
3,080,156
110,572
New York
NY
Albany
19,453,561
54,555
Ohio
OH
Columbus
11,689,100
44,826
Oklahoma
OK
Oklahoma City
3,956,971
69,899
Oregon
OR
Salem
4,217,737
98,379
Pennsylvania
PA
Harrisburg
12,801,989
46,054
Rhode Island
RI
Providence
1,059,361
1,545
South Carolina
SC
Columbia
5,148,714
32,020
South Dakota
SD
Pierre
884,659
77,116
Tennessee
TN
Nashville
6,829,174
42,144
Texas
TX
Austin
28,995,881
268,596
Utah
UT
Salt Lake City
3,205,958
84,897
Virginia
VA
Richmond
8,535,519
42,775
Vermont
VT
Montpelier
623,989
9,616
Washington
WA
Olympia
7,614,893
71,298
Wisconsin
WI
Madison
5,822,434
65,496
West Virginia
WV
Charleston
1,792,147
24,230
Wyoming
WY
Cheyenne
578,759
97,813

Binary file not shown.

View File

@ -0,0 +1 @@
build/./src/HW06_Aidan_Sharpe.cpp.o: src/HW06_Aidan_Sharpe.cpp

View File

@ -0,0 +1,69 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: November 07, 2022
* File Name: HW06_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 6 Problem 1
* Purpose: Read state info data into and out of maps
*****************************************************************/
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
struct state{
std::string name, abrreviation, capitol;
int population, area;
};
int main()
{
std::ifstream infile;
std::map<std::string, state> stateInfo;
infile.open("StatesInfo.txt");
// parse state info into a map
for (int i=0; i<50; i++)
{
state s;
std::getline(infile, s.name);
std::getline(infile, s.abrreviation);
std::getline(infile, s.capitol);
std::string temp;
std::getline(infile, temp);
temp.erase(std::remove(temp.begin(), temp.end(), ','), temp.end()); // remove commas
s.population = std::stoi(temp);
std::getline(infile, temp);
temp.erase(std::remove(temp.begin(), temp.end(), ','), temp.end()); // remove commas
s.area = std::stoi(temp);
stateInfo.emplace(s.abrreviation, s); // add state to state info; state referenced by abbreviation
}
bool quit = false;
std::string abbr;
// query state info from shell
while (true)
{
std::cout << "Enter a U.S. State Abbreviation or 'Q' to quit.\n";
std::cin >> abbr;
if (abbr == "Q" || abbr == "q")
break;
if (stateInfo.count(abbr))
{
std::cout << "Name: " << stateInfo[abbr].name << '\n';
std::cout << "Abbreviation: " << stateInfo[abbr].abrreviation << '\n';
std::cout << "Capitol: " << stateInfo[abbr].capitol << '\n';
std::cout << "Population: " << stateInfo[abbr].population << '\n';
std::cout << "Area: " << stateInfo[abbr].area << " square miles\n";
}
else
std::cout << "Not a valid U.S. State Abbreviation.\n";
}
return 0;
}

View File

@ -0,0 +1,5 @@
311 625 880 701 835 761 291
600 238 17 643 421 299 307
880 698 967 937 982 819 861
857 833 643 558 534 956 620
702 240 639 732 584 520 434

View File

@ -0,0 +1 @@
5 3 8 7 2 10 1 9 6 4

View File

@ -0,0 +1,45 @@
TARGET_EXEC ?= a.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p
run:
make
./build/$(TARGET_EXEC)

Binary file not shown.

View File

@ -0,0 +1,3 @@
build/./src/HW07_Aidan_Sharpe.cpp.o: src/HW07_Aidan_Sharpe.cpp \
src/PDS_IntBinaryTree.h
src/PDS_IntBinaryTree.h:

View File

@ -0,0 +1,61 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: November 07, 2022
* File Name: HW07_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 7 Problem 1
* Purpose: Read values into a binary tree and show the data in several ways
*****************************************************************/
#include <iostream>
#include <fstream>
#include "PDS_IntBinaryTree.h"
/// @brief Read data from a file into a binary tree
/// @param tree binary tree to read into
/// @param fname file to read from
void ReadBTFromFile(IntBinaryTree *tree, std::string fname);
int main()
{
IntBinaryTree binTree = IntBinaryTree();
std::string fname;
std::cout << "Data file name and path relative to Makefile:\n";
std::cin >> fname;
ReadBTFromFile(&binTree, fname);
std::cout << "In Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes(); // custom function to show leafnodes (located in PDS_IntBinaryTree.h)
std::cout << std::endl;
return 0;
}
void ReadBTFromFile(IntBinaryTree *tree, std::string fname)
{
std::ifstream infile;
std::string line;
infile.open(fname);
while (std::getline(infile, line))
{
std::string snum;
for (int i = 0; i < line.length(); i++)
{
if (isdigit(line.at(i)))
snum.push_back(line.at(i));
if (line.at(i) == ' ' || i == line.length() - 1)
{
tree->insert(std::stoi(snum));
snum = "";
}
}
}
infile.close();
}

View File

@ -0,0 +1,255 @@
#pragma once
#include <iostream>
class IntBinaryTree
{
private:
// The TreeNode struct is used to build the tree.
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int value1,
TreeNode *left1 = nullptr,
TreeNode *right1 = nullptr)
{
value = value1;
left = left1;
right = right1;
}
};
TreeNode *root; // Pointer to the root of the tree
//**************************************************
// This version of insert inserts a number into *
// a given subtree of the main binary search tree. *
//**************************************************
void insert(TreeNode * &tree, int num)
{
// If the tree is empty, make a new node and make it
// the root of the tree. We are passing the node pointer by
// reference so that if it is replaced like below, the
// change will also impact the incoming argument.
if (!tree)
{
tree = new TreeNode(num);
return; //Terminate the function
}
// If num is already in tree: return.
if (tree->value == num)
return;
// The tree is not empty: insert the new node into the
// left or right subtree.
if (num < tree->value)
insert(tree->left, num);
else
insert(tree->right, num);
}
//***************************************************
// destroySubTree is called by the destructor. It *
// deletes all nodes in the tree. *
//***************************************************
void destroySubtree(TreeNode *tree)
{
if (!tree) return;
destroySubtree(tree->left);
destroySubtree(tree->right);
// Delete the node at the root.
delete tree;
}
//********************************************
// remove deletes the node in the given tree *
// that has a value member the same as num. *
//********************************************
void remove(TreeNode *&tree, int num)
{
if (tree == nullptr) return;
if (num < tree->value)
remove(tree->left, num);
else if (num > tree->value)
remove(tree->right, num);
else
// We have found the node to delete.
makeDeletion(tree);
}
//***********************************************************
// makeDeletion takes a reference to a tree whose root *
// is to be deleted. If the tree has a single child, the *
// the tree is replaced by the single child after the *
// removal of its root node. If the tree has two children *
// the left subtree of the deleted node is attached at *
// an appropriate point in the right subtree, and then *
// the right subtree replaces the original tree. *
//***********************************************************
void makeDeletion(TreeNode *&tree)
{
// Used to hold node that will be deleted.
TreeNode *nodeToDelete = tree;
// Used to locate the point where the
// left subtree is attached.
TreeNode *attachPoint;
if (tree->right == nullptr)
{
// Replace tree with its left subtree.
tree = tree->left;
}
else if (tree->left == nullptr)
{
// Replace tree with its right subtree.
tree = tree->right;
}
else
//The node has two children
{
// Move to right subtree.
attachPoint = tree->right;
// Locate the smallest node in the right subtree
// by moving as far to the left as possible.
while (attachPoint->left != nullptr)
attachPoint = attachPoint->left;
// Attach the left subtree of the original tree
// as the left subtree of the smallest node
// in the right subtree.
attachPoint->left = tree->left;
// Replace the original tree with its right subtree.
tree = tree->right;
}
// Delete root of original tree
delete nodeToDelete;
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayInOrder(TreeNode *tree) const
{
if (tree)
{
displayInOrder(tree->left);
std::cout << tree->value << " ";
displayInOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayPreOrder(TreeNode *tree) const
{
if (tree)
{
std::cout << tree->value << " ";
displayPreOrder(tree->left);
displayPreOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in postorder. *
//*********************************************************
void displayPostOrder(TreeNode *tree) const
{
if (tree)
{
displayPostOrder(tree->left);
displayPostOrder(tree->right);
std::cout << tree->value << " ";
}
}
/// @brief Displays the leaf values of a binary tree
/// @param tree Binary tree being traced recursively
void displayLeafNodes(TreeNode *tree) const
{
if (tree)
{
if(tree->left || tree->right)
{
displayLeafNodes(tree->left);
displayLeafNodes(tree->right);
}
else
std::cout << tree->value << ' ';
}
}
public:
// These member functions are the public interface.
IntBinaryTree() // Constructor
{
root = nullptr;
}
~IntBinaryTree() // Destructor
{
destroySubtree(root);
}
void insert(int num)
{
insert(root, num);
}
void remove(int num)
{
remove(root, num);
}
void showInOrder(void) const
{
displayInOrder(root);
}
void showPreOrder() const
{
displayPreOrder(root);
}
void showPostOrder() const
{
displayPostOrder(root);
}
void showLeafNodes() const
{
displayLeafNodes(root);
}
//***************************************************
// searchNode determines if a value is present in *
// the tree. If so, the function returns true. *
// Otherwise, it returns false. *
//***************************************************
bool search(int num) const
{
TreeNode *tree = root;
while (tree)
{
if (tree->value == num)
return true;
else if (num < tree->value)
tree = tree->left;
else
tree = tree->right;
}
return false;
}
};

View File

@ -0,0 +1,33 @@
{
"files.associations": {
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp"
}
}

View File

@ -0,0 +1,4 @@
14 54 34 67 89 56 43 12 21 23 98 31 52 74 93
4 54 89 34 21
5 54 23 10 93 12

View File

@ -0,0 +1,45 @@
TARGET_EXEC ?= a.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p
run:
make
./build/$(TARGET_EXEC)

Binary file not shown.

View File

@ -0,0 +1 @@
build/./src/HW07_Aidan_Sharpe.cpp.o: src/HW07_Aidan_Sharpe.cpp

View File

@ -0,0 +1,124 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: November 07, 2022
* File Name: HW07_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 7 Problem 1
* Purpose: Read values into a binary tree and show the data in several ways
*****************************************************************/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "PDS_IntBinaryTree.h"
/// @brief Read data from a file into a binary tree
/// @param tree binary tree to read into
/// @param removals list of items to remove from binary tree
/// @param searchTerms list of items to search tree for
/// @param hasMetadata whether or not each line's first entry is the entry count
/// @param fname file to read from
void ReadBTFromFile(IntBinaryTree *tree, std::vector<int> *removals, std::vector<int> *searchTerms, bool hasMetadata, std::string fname);
int main()
{
IntBinaryTree binTree = IntBinaryTree();
std::vector<int> removals;
std::vector<int> searchTerms;
char hasMetadataAnswer;
bool hasMetadata;
std::string fname;
std::cout << "Data file name and path relative to Makefile:\n";
std::cin >> fname;
std::cout << "Does data set contain metadata? [y/N]\n";
std::cin >> hasMetadataAnswer;
hasMetadata = tolower(hasMetadataAnswer) == 'y';
ReadBTFromFile(&binTree, &removals, &searchTerms, hasMetadata, fname);
std::cout << "In Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nRemoving values";
for (int removal : removals)
{
std::cout << ' ' << removal;
binTree.remove(removal);
}
std::cout << "\nIn Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nSearching for values:\n";
for(int term : searchTerms)
std::cout << term << ((binTree.search(term)) ? " found" : " not found") << std::endl;
return 0;
}
void ReadBTFromFile(IntBinaryTree *tree, std::vector<int> *removals, std::vector<int> *searchTerms, bool hasMetadata, std::string fname)
{
std::ifstream infile;
std::string line;
infile.open(fname);
bool isFirstEntry;
int lineNum = 0;
while (std::getline(infile, line))
{
isFirstEntry = true;
std::string snum;
for (int i = 0; i < line.length(); i++)
{
if (isdigit(line.at(i)))
snum.push_back(line.at(i));
if (line.at(i) == ' ' || i == line.length() - 1)
{
if (!(hasMetadata && isFirstEntry))
{
switch (lineNum)
{
case 0:
tree->insert(std::stoi(snum));
break;
case 1:
removals->push_back(std::stoi(snum));
break;
case 2:
searchTerms->push_back(std::stoi(snum));
break;
}
}
snum = "";
isFirstEntry = false;
}
}
lineNum++;
}
infile.close();
}

View File

@ -0,0 +1,320 @@
#pragma once
#include <iostream>
class IntBinaryTree
{
private:
int nodeCount, sum;
float mean;
// The TreeNode struct is used to build the tree.
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int value1,
TreeNode *left1 = nullptr,
TreeNode *right1 = nullptr)
{
value = value1;
left = left1;
right = right1;
}
};
TreeNode *root; // Pointer to the root of the tree
//**************************************************
// This version of insert inserts a number into *
// a given subtree of the main binary search tree. *
//**************************************************
void insert(TreeNode * &tree, int num)
{
// If the tree is empty, make a new node and make it
// the root of the tree. We are passing the node pointer by
// reference so that if it is replaced like below, the
// change will also impact the incoming argument.
if (!tree)
{
tree = new TreeNode(num);
nodeCount++;
sum += num;
mean = static_cast<float>(sum)/nodeCount;
return; //Terminate the function
}
// If num is already in tree: return.
if (tree->value == num)
return;
// The tree is not empty: insert the new node into the
// left or right subtree.
if (num < tree->value)
insert(tree->left, num);
else
insert(tree->right, num);
}
/// @brief find the minimum value in an integer binary tree
/// @param tree the binary tree to traverse
/// @return the minimum value
int findMin(TreeNode *tree)
{
if (!tree)
throw std::length_error("Empty set has no min.");
else
{
TreeNode *leftest = tree;
while (leftest->left)
leftest = leftest->left;
return leftest->value;
}
}
/// @brief find the maximum value in an integer binary tree
/// @param tree the binary tree to traverse
/// @return the maximum value
int findMax(TreeNode *tree)
{
if (!tree)
throw std::length_error("Empty set has no max.");
else
{
TreeNode *rightest = tree;
while (rightest->right)
rightest = rightest->right;
return rightest->value;
}
}
//***************************************************
// destroySubTree is called by the destructor. It *
// deletes all nodes in the tree. *
//***************************************************
void destroySubtree(TreeNode *tree)
{
if (!tree) return;
destroySubtree(tree->left);
destroySubtree(tree->right);
// Delete the node at the root.
delete tree;
}
//********************************************
// remove deletes the node in the given tree *
// that has a value member the same as num. *
//********************************************
void remove(TreeNode *&tree, int num)
{
if (tree == nullptr) return;
if (num < tree->value)
remove(tree->left, num);
else if (num > tree->value)
remove(tree->right, num);
else
{
// We have found the node to delete.
nodeCount--;
sum -= num;
mean = static_cast<float>(sum)/nodeCount;
makeDeletion(tree);
}
}
//***********************************************************
// makeDeletion takes a reference to a tree whose root *
// is to be deleted. If the tree has a single child, the *
// the tree is replaced by the single child after the *
// removal of its root node. If the tree has two children *
// the left subtree of the deleted node is attached at *
// an appropriate point in the right subtree, and then *
// the right subtree replaces the original tree. *
//***********************************************************
void makeDeletion(TreeNode *&tree)
{
// Used to hold node that will be deleted.
TreeNode *nodeToDelete = tree;
// Used to locate the point where the
// left subtree is attached.
TreeNode *attachPoint;
if (tree->right == nullptr)
{
// Replace tree with its left subtree.
tree = tree->left;
}
else if (tree->left == nullptr)
{
// Replace tree with its right subtree.
tree = tree->right;
}
else
//The node has two children
{
// Move to right subtree.
attachPoint = tree->right;
// Locate the smallest node in the right subtree
// by moving as far to the left as possible.
while (attachPoint->left != nullptr)
attachPoint = attachPoint->left;
// Attach the left subtree of the original tree
// as the left subtree of the smallest node
// in the right subtree.
attachPoint->left = tree->left;
// Replace the original tree with its right subtree.
tree = tree->right;
}
// Delete root of original tree
delete nodeToDelete;
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayInOrder(TreeNode *tree) const
{
if (tree)
{
displayInOrder(tree->left);
std::cout << tree->value << " ";
displayInOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayPreOrder(TreeNode *tree) const
{
if (tree)
{
std::cout << tree->value << " ";
displayPreOrder(tree->left);
displayPreOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in postorder. *
//*********************************************************
void displayPostOrder(TreeNode *tree) const
{
if (tree)
{
displayPostOrder(tree->left);
displayPostOrder(tree->right);
std::cout << tree->value << " ";
}
}
/// @brief Displays the leaf values of a binary tree
/// @param tree Binary tree being traced recursively
void displayLeafNodes(TreeNode *tree) const
{
if (tree)
{
if(tree->left || tree->right)
{
displayLeafNodes(tree->left);
displayLeafNodes(tree->right);
}
else
std::cout << tree->value << ' ';
}
}
public:
// These member functions are the public interface.
IntBinaryTree() // Constructor
{
root = nullptr;
nodeCount = 0;
sum = 0;
mean = 0;
}
~IntBinaryTree() // Destructor
{
destroySubtree(root);
}
void insert(int num)
{
insert(root, num);
}
void remove(int num)
{
remove(root, num);
}
void showInOrder(void) const
{
displayInOrder(root);
}
void showPreOrder() const
{
displayPreOrder(root);
}
void showPostOrder() const
{
displayPostOrder(root);
}
void showLeafNodes() const
{
displayLeafNodes(root);
}
int getMin()
{
findMin(root);
}
int getMax()
{
findMax(root);
}
int getNodeCount()
{
return nodeCount;
}
int getSum()
{
return sum;
}
float getMean()
{
return mean;
}
//***************************************************
// searchNode determines if a value is present in *
// the tree. If so, the function returns true. *
// Otherwise, it returns false. *
//***************************************************
bool search(int num) const
{
TreeNode *tree = root;
while (tree)
{
if (tree->value == num)
return true;
else if (num < tree->value)
tree = tree->left;
else
tree = tree->right;
}
return false;
}
};

View File

@ -0,0 +1,49 @@
{
"files.associations": {
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"tuple": "cpp",
"utility": "cpp"
}
}

View File

@ -0,0 +1,3 @@
14 h f d e r t y i j k l a s v
4 h a i v
5 h f d z y

View File

@ -0,0 +1,3 @@
10 70.2 12.4 0.5 91.3 56.4 23.7 86.2 42.2 117.8 148.2
3 86.2 0.5 12.4
5 70.2 0.5 0 117.8 -30

View File

@ -0,0 +1,3 @@
14 54 34 67 89 56 43 12 21 23 98 31 52 74 93
4 54 89 34 21
5 54 23 10 93 12

View File

@ -0,0 +1,45 @@
TARGET_EXEC ?= a.out
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
# assembly
$(BUILD_DIR)/%.s.o: %.s
$(MKDIR_P) $(dir $@)
$(AS) $(ASFLAGS) -c $< -o $@
# c source
$(BUILD_DIR)/%.c.o: %.c
$(MKDIR_P) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
.PHONY: clean
clean:
$(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p
run:
make
./build/$(TARGET_EXEC)

Binary file not shown.

View File

@ -0,0 +1,3 @@
build/./src/ECHW_Aidan_Sharpe.cpp.o: src/ECHW_Aidan_Sharpe.cpp \
src/PDS_BinaryTree.h
src/PDS_BinaryTree.h:

View File

@ -0,0 +1,297 @@
/****************************************************************
* Name: Aidan Sharpe
* Course: Principles of Data Structures
* Class: CS04225
* Assignment Date: November 07, 2022
* File Name: ECHW_Aidan_Sharpe.cpp
*****************************************************************
* ID: Homework 7 Problem 1
* Purpose: Read values into a binary tree and show the data in several ways
*****************************************************************/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "PDS_BinaryTree.h"
void ReadBTIntFromFile(BinaryTree<int> *tree, std::vector<int> *removals, std::vector<int> *searchTerms, bool hasMetadata, std::string fname);
void ReadBTCharFromFile(BinaryTree<char> *tree, std::vector<char> *removals, std::vector<char> *searchTerms, bool hasMetadata, std::string fname);
void ReadBTDoubleFromFile(BinaryTree<double> *tree, std::vector<double> *removals, std::vector<double> *searchTerms, bool hasMetadata, std::string fname);
int main()
{
char type;
std::string fname;
char hasMetadataAnswer;
bool hasMetadata;
std::cout << "Path of data file relative to Makefile.\n";
std::cin >> fname;
std::cout << "Enter type of data. I for ints, C for chars, D for doubles.\n";
std::cin >> type;
std::cout << "Does data set contain metadata? [y/N]\n";
std::cin >> hasMetadataAnswer;
hasMetadata = tolower(hasMetadataAnswer) == 'y';
if (type == 'C')
{
BinaryTree<char> binTree = BinaryTree<char>();
std::vector<char> removals;
std::vector<char> searchTerms;
ReadBTCharFromFile(&binTree, &removals, &searchTerms, hasMetadata, fname);
std::cout << "In Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nRemoving values";
for (char removal : removals)
{
std::cout << ' ' << removal;
binTree.remove(removal);
}
std::cout << "\nIn Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nSearching for values:\n";
for(char term : searchTerms)
std::cout << term << ((binTree.search(term)) ? " found" : " not found") << std::endl;
}
else if(type == 'I')
{
BinaryTree<int> binTree = BinaryTree<int>();
std::vector<int> removals;
std::vector<int> searchTerms;
ReadBTIntFromFile(&binTree, &removals, &searchTerms, hasMetadata, fname);std::cout << "In Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nRemoving values";
for (int removal : removals)
{
std::cout << ' ' << removal;
binTree.remove(removal);
}
std::cout << "\nIn Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nSearching for values:\n";
for(int term : searchTerms)
std::cout << term << ((binTree.search(term)) ? " found" : " not found") << std::endl;
}
else if(type == 'D')
{
BinaryTree<double> binTree = BinaryTree<double>();
std::vector<double> removals;
std::vector<double> searchTerms;
ReadBTDoubleFromFile(&binTree, &removals, &searchTerms, hasMetadata, fname);
std::cout << "In Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nRemoving values";
for (double removal : removals)
{
std::cout << ' ' << removal;
binTree.remove(removal);
}
std::cout << "\nIn Order: ";
binTree.showInOrder();
std::cout << "\nPre-Order: ";
binTree.showPreOrder();
std::cout << "\nPost-Order: ";
binTree.showPostOrder();
std::cout << "\nLeaf Nodes: ";
binTree.showLeafNodes();
std::cout << "\nMax: " << binTree.getMax();
std::cout << "\nMin: " << binTree.getMin();
std::cout << "\nNode Count: " << binTree.getNodeCount();
std::cout << "\nSum: " << binTree.getSum();
std::cout << "\nMean: " << binTree.getMean();
std::cout << "\nSearching for values:\n";
for(double term : searchTerms)
std::cout << term << ((binTree.search(term)) ? " found" : " not found") << std::endl;
}
else
std::cout << "Selection " << type << " does not signify any valid type.\n";
return 0;
}
void ReadBTIntFromFile(BinaryTree<int> *tree, std::vector<int> *removals, std::vector<int> *searchTerms, bool hasMetadata, std::string fname)
{
std::ifstream infile;
std::string line;
infile.open(fname);
bool isFirstEntry;
int lineNum = 0;
while (std::getline(infile, line))
{
isFirstEntry = true;
std::string snum;
for (int i = 0; i < line.length(); i++)
{
if (isdigit(line.at(i)))
snum.push_back(line.at(i));
if (line.at(i) == ' ' || i == line.length() - 1)
{
if (!(hasMetadata && isFirstEntry))
{
switch (lineNum)
{
case 0:
tree->insert(std::stoi(snum));
break;
case 1:
removals->push_back(std::stoi(snum));
break;
case 2:
searchTerms->push_back(std::stoi(snum));
break;
}
}
snum = "";
isFirstEntry = false;
}
}
lineNum++;
}
infile.close();
}
void ReadBTCharFromFile(BinaryTree<char> *tree, std::vector<char> *removals, std::vector<char> *searchTerms, bool hasMetadata, std::string fname)
{
std::ifstream infile;
std::string line;
infile.open(fname);
int lineNum = 0;
bool isFirstEntry;
char c;
while (std::getline(infile, line))
{
isFirstEntry = true;
for (int i = 0; i < line.length(); i++)
{
if (line.at(i) != ' ')
c = line.at(i);
if (line.at(i) == ' ' || i == line.length() - 1)
{
if (!(hasMetadata && isFirstEntry))
{
switch (lineNum)
{
case 0:
tree->insert(c);
break;
case 1:
removals->push_back(c);
break;
case 2:
searchTerms->push_back(c);
break;
}
}
isFirstEntry = false;
}
}
lineNum++;
}
infile.close();
}
void ReadBTDoubleFromFile(BinaryTree<double> *tree, std::vector<double> *removals, std::vector<double> *searchTerms, bool hasMetadata, std::string fname)
{
std::ifstream infile;
std::string line;
infile.open(fname);
int lineNum = 0;
bool isFirstEntry;
while (std::getline(infile, line))
{
isFirstEntry = true;
std::string snum;
for (int i = 0; i < line.length(); i++)
{
if (isdigit(line.at(i)) || line.at(i) == '.')
snum.push_back(line.at(i));
if (line.at(i) == ' ' || i == line.length() - 1)
{
if (!(hasMetadata && isFirstEntry))
{
switch (lineNum)
{
case 0:
tree->insert(std::stod(snum));
break;
case 1:
removals->push_back(std::stod(snum));
break;
case 2:
searchTerms->push_back(std::stod(snum));
break;
}
}
snum = "";
isFirstEntry = false;
}
}
lineNum++;
}
infile.close();
}

View File

@ -0,0 +1,322 @@
#pragma once
#include <iostream>
template <class T>
class BinaryTree
{
private:
int nodeCount;
float sum;
float mean;
// The TreeNode struct is used to build the tree.
struct TreeNode
{
T value;
TreeNode *left;
TreeNode *right;
TreeNode(T value1,
TreeNode *left1 = nullptr,
TreeNode *right1 = nullptr)
{
value = value1;
left = left1;
right = right1;
}
};
TreeNode *root; // Pointer to the root of the tree
//**************************************************
// This version of insert inserts a number into *
// a given subtree of the main binary search tree. *
//**************************************************
void insert(TreeNode * &tree, T num)
{
// If the tree is empty, make a new node and make it
// the root of the tree. We are passing the node pointer by
// reference so that if it is replaced like below, the
// change will also impact the incoming argument.
if (!tree)
{
tree = new TreeNode(num);
nodeCount++;
sum += static_cast<float>(num);
mean = static_cast<float>(sum)/nodeCount;
return; //Terminate the function
}
// If num is already in tree: return.
if (tree->value == num)
return;
// The tree is not empty: insert the new node into the
// left or right subtree.
if (num < tree->value)
insert(tree->left, num);
else
insert(tree->right, num);
}
/// @brief find the minimum value in an integer binary tree
/// @param tree the binary tree to traverse
/// @return the minimum value
T findMin(TreeNode *tree)
{
if (!tree)
throw std::length_error("Empty set has no min.");
else
{
TreeNode *leftest = tree;
while (leftest->left)
leftest = leftest->left;
return leftest->value;
}
}
/// @brief find the maximum value in an integer binary tree
/// @param tree the binary tree to traverse
/// @return the maximum value
T findMax(TreeNode *tree)
{
if (!tree)
throw std::length_error("Empty set has no max.");
else
{
TreeNode *rightest = tree;
while (rightest->right)
rightest = rightest->right;
return rightest->value;
}
}
//***************************************************
// destroySubTree is called by the destructor. It *
// deletes all nodes in the tree. *
//***************************************************
void destroySubtree(TreeNode *tree)
{
if (!tree) return;
destroySubtree(tree->left);
destroySubtree(tree->right);
// Delete the node at the root.
delete tree;
}
//********************************************
// remove deletes the node in the given tree *
// that has a value member the same as num. *
//********************************************
void remove(TreeNode *&tree, T num)
{
if (tree == nullptr) return;
if (num < tree->value)
remove(tree->left, num);
else if (num > tree->value)
remove(tree->right, num);
else
{
// We have found the node to delete.
nodeCount--;
sum -= static_cast<float>(num);
mean = static_cast<float>(sum)/nodeCount;
makeDeletion(tree);
}
}
//***********************************************************
// makeDeletion takes a reference to a tree whose root *
// is to be deleted. If the tree has a single child, the *
// the tree is replaced by the single child after the *
// removal of its root node. If the tree has two children *
// the left subtree of the deleted node is attached at *
// an appropriate point in the right subtree, and then *
// the right subtree replaces the original tree. *
//***********************************************************
void makeDeletion(TreeNode *&tree)
{
// Used to hold node that will be deleted.
TreeNode *nodeToDelete = tree;
// Used to locate the point where the
// left subtree is attached.
TreeNode *attachPoint;
if (tree->right == nullptr)
{
// Replace tree with its left subtree.
tree = tree->left;
}
else if (tree->left == nullptr)
{
// Replace tree with its right subtree.
tree = tree->right;
}
else
//The node has two children
{
// Move to right subtree.
attachPoint = tree->right;
// Locate the smallest node in the right subtree
// by moving as far to the left as possible.
while (attachPoint->left != nullptr)
attachPoint = attachPoint->left;
// Attach the left subtree of the original tree
// as the left subtree of the smallest node
// in the right subtree.
attachPoint->left = tree->left;
// Replace the original tree with its right subtree.
tree = tree->right;
}
// Delete root of original tree
delete nodeToDelete;
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayInOrder(TreeNode *tree) const
{
if (tree)
{
displayInOrder(tree->left);
std::cout << tree->value << " ";
displayInOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in inorder. *
//*********************************************************
void displayPreOrder(TreeNode *tree) const
{
if (tree)
{
std::cout << tree->value << " ";
displayPreOrder(tree->left);
displayPreOrder(tree->right);
}
}
//*********************************************************
// This function displays the values stored in a tree *
// in postorder. *
//*********************************************************
void displayPostOrder(TreeNode *tree) const
{
if (tree)
{
displayPostOrder(tree->left);
displayPostOrder(tree->right);
std::cout << tree->value << " ";
}
}
/// @brief Displays the leaf values of a binary tree
/// @param tree Binary tree being traced recursively
void displayLeafNodes(TreeNode *tree) const
{
if (tree)
{
if(tree->left || tree->right)
{
displayLeafNodes(tree->left);
displayLeafNodes(tree->right);
}
else
std::cout << tree->value << ' ';
}
}
public:
// These member functions are the public interface.
BinaryTree() // Constructor
{
root = nullptr;
nodeCount = 0;
sum = 0;
mean = 0;
}
~BinaryTree() // Destructor
{
destroySubtree(root);
}
void insert(T num)
{
insert(root, num);
}
void remove(T num)
{
remove(root, num);
}
void showInOrder(void) const
{
displayInOrder(root);
}
void showPreOrder() const
{
displayPreOrder(root);
}
void showPostOrder() const
{
displayPostOrder(root);
}
void showLeafNodes() const
{
displayLeafNodes(root);
}
T getMin()
{
findMin(root);
}
T getMax()
{
findMax(root);
}
int getNodeCount()
{
return nodeCount;
}
float getSum()
{
return sum;
}
float getMean()
{
return mean;
}
//***************************************************
// searchNode determines if a value is present in *
// the tree. If so, the function returns true. *
// Otherwise, it returns false. *
//***************************************************
bool search(T num) const
{
TreeNode *tree = root;
while (tree)
{
if (tree->value == num)
return true;
else if (num < tree->value)
tree = tree->left;
else
tree = tree->right;
}
return false;
}
};

View File

BIN
3rd-Semester-Fall-2022/PDS/t.exe Executable file

Binary file not shown.

View File

@ -0,0 +1,8 @@
#include <stdio.h>
void main()
{
int x[5] = {1,2,3,4,5};
printf("%d", *(x+1));
}