Cleaning up and adding comments
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
#include "option1.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
// Entry point for Category 3, Option 1.
|
||||
// Demonstrates:
|
||||
// 1) Building a BST of 200 employees from DATA/random_names.txt
|
||||
// 2) Running all BST traversals
|
||||
// 3) Searching and deleting by employee ID
|
||||
// 4) Building and printing an AVL tree with random integer keys
|
||||
int RunApp() {
|
||||
//Reading names from file for BST population
|
||||
bst = new TBST();
|
||||
/* Path to the names data file
|
||||
This is MY absolute path -- change to your local path for this to read properly
|
||||
something like "C:\Users\Username\FolderYouSavedTheSubmissionIn\Exam\IKT203Exam\DATA\random_names.txt"
|
||||
Double slash is needed for string to pass the correct file path */
|
||||
const std::string filename = "C:\\Users\\csand\\IKT203\\Exam\\IKT203Exam\\DATA\\random_names.txt";
|
||||
|
||||
// Read 200 employees from the names file and populate the BST.
|
||||
// IMPORTANT: Working directory must be the Portfolio/Assignment-03 folder
|
||||
// so that "DATA/random_names.txt" resolves correctly.
|
||||
const std::string filename = "DATA/random_names.txt";
|
||||
readNamesFromFile(filename, onNameRead);
|
||||
|
||||
// BST traversal -- comment out the entire block
|
||||
// when done inspecting for more manageable terminal output
|
||||
// --- BST traversals ---
|
||||
// These calls demonstrate all four traversal orders on the employee BST.
|
||||
// Comment out this block if the console output becomes too noisy.
|
||||
pack("Inorder traversal (sorted by ID)");
|
||||
bst->Inorder();
|
||||
|
||||
@@ -26,6 +32,8 @@ int RunApp() {
|
||||
pack("Postorder traversal");
|
||||
bst->Postorder();
|
||||
|
||||
// --- BST search demo ---
|
||||
// Ask the user for an ID, search in the BST, and print the matching employee (if any).
|
||||
pack("Search function");
|
||||
std::cout << "\nInput the ID you want to search for\n" << std::endl;
|
||||
int choice;
|
||||
@@ -36,6 +44,9 @@ int RunApp() {
|
||||
else
|
||||
std::cout << "ID not found" << std::endl;
|
||||
|
||||
// --- BST delete demo ---
|
||||
// Ask the user for an ID, delete it from the BST if it exists,
|
||||
// then print the new inorder traversal to show the updated structure.
|
||||
pack("Remove function");
|
||||
std::cout << "\nInput the ID you want to remove\n" << std::endl;
|
||||
std::cin >> choice;
|
||||
@@ -49,13 +60,13 @@ int RunApp() {
|
||||
bst->Inorder();
|
||||
// End of BST block
|
||||
|
||||
// Start of AVL block
|
||||
// Again, comment out the block if terminal output is
|
||||
// too noisy
|
||||
// --- AVL demo ---
|
||||
// Build an AVL tree using random integers in [1, 200].
|
||||
// This tree only stores keys (no TEmployee data) and is used
|
||||
// to demonstrate balancing and traversals.
|
||||
pack("AVL");
|
||||
avl = new TAVL;
|
||||
|
||||
|
||||
TAVL::Populate(avl, 100, 1, 200);
|
||||
pack("Inorder");
|
||||
avl->PrintOrder(TAVL::Inorder);
|
||||
@@ -71,9 +82,9 @@ int RunApp() {
|
||||
// End of AVL block
|
||||
|
||||
|
||||
// Cleaning to free up memory.
|
||||
// Used only at end of runtime, but useful for when runtime needs
|
||||
// to be continuous
|
||||
// --- Cleanup ---
|
||||
// TBST destructor deletes all TEmployee objects it owns.
|
||||
// Here we delete the tree objects themselves to avoid leaks.
|
||||
pack ("Cleaning up");
|
||||
delete bst;
|
||||
delete avl;
|
||||
|
||||
Reference in New Issue
Block a user