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,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;
}
};