Working on binary trees
parent
9c87a56ed0
commit
fa18b01220
|
@ -1,2 +1,2 @@
|
||||||
# BLT
|
# blt
|
||||||
A common utilties library for my future creations
|
A common utilties library for my future creations
|
||||||
|
|
|
@ -10,10 +10,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <vector>
|
||||||
#include <blt/std/time.h>
|
#include <blt/std/time.h>
|
||||||
|
#include <blt/std/queue.h>
|
||||||
|
|
||||||
namespace BLT {
|
namespace blt {
|
||||||
struct CapturePoint {
|
struct CapturePoint {
|
||||||
std::string_view name;
|
std::string_view name;
|
||||||
long point;
|
long point;
|
||||||
|
@ -34,9 +35,9 @@ namespace BLT {
|
||||||
HASHMAP_TYPE<std::string_view, CaptureInterval> intervals{};
|
HASHMAP_TYPE<std::string_view, CaptureInterval> intervals{};
|
||||||
|
|
||||||
// profiling points
|
// profiling points
|
||||||
std::vector<std::queue<CapturePoint>> cyclicPointsHistory{};
|
std::vector<blt::flat_queue<CapturePoint>> cyclicPointsHistory{};
|
||||||
std::queue<CapturePoint> points{};
|
blt::flat_queue<CapturePoint> points{};
|
||||||
std::queue<CapturePoint> cyclicPoints{};
|
blt::flat_queue<CapturePoint> cyclicPoints{};
|
||||||
|
|
||||||
std::mutex timerLock{};
|
std::mutex timerLock{};
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -0,0 +1,146 @@
|
||||||
|
/*
|
||||||
|
* Created by Brett on 09/01/23.
|
||||||
|
* Licensed under GNU General Public License V3.0
|
||||||
|
* See LICENSE file for license detail
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <vector>
|
||||||
|
#include <blt/std/queue.h>
|
||||||
|
|
||||||
|
#ifndef BLT_BINARY_TREE_H
|
||||||
|
#define BLT_BINARY_TREE_H
|
||||||
|
|
||||||
|
namespace blt {
|
||||||
|
|
||||||
|
class binary_search_tree_error : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
explicit binary_search_tree_error(const std::string& string): runtime_error(string) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class node_binary_search_tree {
|
||||||
|
protected:
|
||||||
|
struct BST_node {
|
||||||
|
BST_node* left = nullptr;
|
||||||
|
BST_node* right = nullptr;
|
||||||
|
T payload;
|
||||||
|
|
||||||
|
~BST_node() {
|
||||||
|
delete (left);
|
||||||
|
delete (right);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BST_node* m_root = nullptr;
|
||||||
|
private:
|
||||||
|
void insert(BST_node* root, const T& element) {
|
||||||
|
BST_node* searchNode = root;
|
||||||
|
// basically we are iterating through the tree looking for a valid node to insert into.
|
||||||
|
while (true) {
|
||||||
|
// check for left and right tree traversal if it exists
|
||||||
|
if (searchNode->left != nullptr && element < searchNode->left->payload) {
|
||||||
|
searchNode = searchNode->left;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (searchNode->right != nullptr && element > searchNode->right->payload) {
|
||||||
|
searchNode = searchNode->right;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (element == searchNode->payload)
|
||||||
|
throw binary_search_tree_error{"Unable to insert. Nodes cannot have equal values!\n"};
|
||||||
|
// insert into the lowest node consistent with a BST
|
||||||
|
if (element < searchNode->payload) {
|
||||||
|
searchNode->left = new BST_node();
|
||||||
|
searchNode->left->payload = element;
|
||||||
|
} else {
|
||||||
|
searchNode->right = new BST_node();
|
||||||
|
searchNode->right->payload = element;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BST_node* search(BST_node*& parent, const T& element) {
|
||||||
|
BST_node* searchNode = m_root;
|
||||||
|
// basically we are iterating through the tree looking for a valid node to insert into.
|
||||||
|
while (true) {
|
||||||
|
if (searchNode->payload == element)
|
||||||
|
return searchNode->payload;
|
||||||
|
// check for left and right tree traversal if it exists
|
||||||
|
if (searchNode->left != nullptr && element < searchNode->left->payload) {
|
||||||
|
parent = searchNode;
|
||||||
|
searchNode = searchNode->left;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (searchNode->right != nullptr && element > searchNode->right->payload) {
|
||||||
|
parent = searchNode;
|
||||||
|
searchNode = searchNode->right;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<BST_node*> inOrderTraverse(BST_node* root) {
|
||||||
|
std::vector<BST_node*> nodes{};
|
||||||
|
blt::flat_stack<BST_node*> nodeStack{};
|
||||||
|
|
||||||
|
BST_node* current = root;
|
||||||
|
while (current != nullptr || !nodeStack.isEmpty()) {
|
||||||
|
// go all the way to the left subtree
|
||||||
|
while (current != nullptr){
|
||||||
|
nodeStack.push(current);
|
||||||
|
current = current->left;
|
||||||
|
}
|
||||||
|
current = nodeStack.front();
|
||||||
|
nodeStack.pop();
|
||||||
|
nodes.push_back(current);
|
||||||
|
current = current->right;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
node_binary_search_tree() {
|
||||||
|
m_root = new BST_node();
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(const T& element) {
|
||||||
|
insert(m_root, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] BST_node* search(const T& element) const {
|
||||||
|
search(nullptr, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(const T& element) {
|
||||||
|
BST_node* parent = nullptr;
|
||||||
|
auto elementNode = search(parent, element);
|
||||||
|
// reconstruct subtree. More efficient way of doing this... TODO
|
||||||
|
std::vector<BST_node*> subNodes = inOrderTraverse(elementNode);
|
||||||
|
for (auto* node : subNodes){
|
||||||
|
if (node != elementNode)
|
||||||
|
insert(parent, node->payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~node_binary_search_tree() {
|
||||||
|
delete (m_root);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class flat_binary_search_tree {
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using node_BST = node_binary_search_tree<T>;
|
||||||
|
template<typename T>
|
||||||
|
using flat_BST = flat_binary_search_tree<T>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLT_BINARY_TREE_H
|
|
@ -0,0 +1,16 @@
|
||||||
|
/*
|
||||||
|
* Created by Brett on 09/01/23.
|
||||||
|
* Licensed under GNU General Public License V3.0
|
||||||
|
* See LICENSE file for license detail
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLT_MATH_H
|
||||||
|
#define BLT_MATH_H
|
||||||
|
|
||||||
|
namespace blt {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLT_MATH_H
|
|
@ -10,7 +10,7 @@
|
||||||
/**
|
/**
|
||||||
* Do no use any queue in this file. They are slower than std::queue.
|
* Do no use any queue in this file. They are slower than std::queue.
|
||||||
*/
|
*/
|
||||||
namespace BLT {
|
namespace blt {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct node {
|
struct node {
|
||||||
|
@ -185,7 +185,6 @@ namespace BLT {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_QUEUE_H
|
#endif //BLT_QUEUE_H
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
namespace BLT {
|
namespace blt {
|
||||||
/**
|
/**
|
||||||
* Creates a container class for generating random number distributions
|
* Creates a container class for generating random number distributions
|
||||||
* @tparam T numeric type
|
* @tparam T numeric type
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace BLT::String {
|
namespace blt::String {
|
||||||
/**
|
/**
|
||||||
* Converts the string into lower case
|
* Converts the string into lower case
|
||||||
* @param s string to lower case
|
* @param s string to lower case
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#ifndef BLT_SYSTEM_H
|
#ifndef BLT_SYSTEM_H
|
||||||
#define BLT_SYSTEM_H
|
#define BLT_SYSTEM_H
|
||||||
|
|
||||||
namespace BLT::System {
|
namespace blt::System {
|
||||||
// TODO: system memory and current CPU usage. (Linux Only currently)
|
// TODO: system memory and current CPU usage. (Linux Only currently)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace BLT::System {
|
namespace blt::System {
|
||||||
static inline auto getCurrentTimeNanoseconds() {
|
static inline auto getCurrentTimeNanoseconds() {
|
||||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,6 @@
|
||||||
*/
|
*/
|
||||||
#include <blt/profiling/profiler.h>
|
#include <blt/profiling/profiler.h>
|
||||||
|
|
||||||
namespace BLT {
|
namespace blt {
|
||||||
|
|
||||||
}
|
}
|
|
@ -5,6 +5,6 @@
|
||||||
*/
|
*/
|
||||||
#include <blt/std/queue.h>
|
#include <blt/std/queue.h>
|
||||||
|
|
||||||
namespace BLT {
|
namespace blt {
|
||||||
|
|
||||||
}
|
}
|
|
@ -4,3 +4,4 @@
|
||||||
* See LICENSE file for license detail
|
* See LICENSE file for license detail
|
||||||
*/
|
*/
|
||||||
#include <blt/std/system.h>
|
#include <blt/std/system.h>
|
||||||
|
#include <blt/std/binary_tree.h>
|
Loading…
Reference in New Issue