3rd semester files
This commit is contained in:
49
3rd-Semester-Fall-2022/PDS/HomeworkEC/.vscode/settings.json
vendored
Executable file
49
3rd-Semester-Fall-2022/PDS/HomeworkEC/.vscode/settings.json
vendored
Executable 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"
|
||||
}
|
||||
}
|
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_charInput.txt
Executable file
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_charInput.txt
Executable 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
|
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_doubleInput.txt
Executable file
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_doubleInput.txt
Executable 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
|
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_intInput.txt
Executable file
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/ECHW_intInput.txt
Executable 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
|
45
3rd-Semester-Fall-2022/PDS/HomeworkEC/Makefile
Executable file
45
3rd-Semester-Fall-2022/PDS/HomeworkEC/Makefile
Executable 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)
|
BIN
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/a.out
Executable file
BIN
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/a.out
Executable file
Binary file not shown.
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/src/ECHW_Aidan_Sharpe.cpp.d
Executable file
3
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/src/ECHW_Aidan_Sharpe.cpp.d
Executable 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:
|
BIN
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/src/ECHW_Aidan_Sharpe.cpp.o
Executable file
BIN
3rd-Semester-Fall-2022/PDS/HomeworkEC/build/src/ECHW_Aidan_Sharpe.cpp.o
Executable file
Binary file not shown.
297
3rd-Semester-Fall-2022/PDS/HomeworkEC/src/ECHW_Aidan_Sharpe.cpp
Executable file
297
3rd-Semester-Fall-2022/PDS/HomeworkEC/src/ECHW_Aidan_Sharpe.cpp
Executable 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();
|
||||
}
|
322
3rd-Semester-Fall-2022/PDS/HomeworkEC/src/PDS_BinaryTree.h
Executable file
322
3rd-Semester-Fall-2022/PDS/HomeworkEC/src/PDS_BinaryTree.h
Executable 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;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user