Fix insert root node with false default value
parent
6a2a5948fb
commit
e5613c8007
|
@ -35,9 +35,13 @@ namespace blt {
|
||||||
BST_node* m_root = nullptr;
|
BST_node* m_root = nullptr;
|
||||||
private:
|
private:
|
||||||
void insert(BST_node* root, const T& element) {
|
void insert(BST_node* root, const T& element) {
|
||||||
|
if (root == nullptr)
|
||||||
|
throw binary_search_tree_error{"Unable to insert. Provided root is null!\n"};
|
||||||
BST_node* searchNode = root;
|
BST_node* searchNode = root;
|
||||||
// basically we are iterating through the tree looking for a valid node to insert into.
|
// basically we are iterating through the tree looking for a valid node to insert into.
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (element == searchNode->payload)
|
||||||
|
throw binary_search_tree_error{"Unable to insert. Nodes cannot have equal values!\n"};
|
||||||
// check for left and right tree traversal if it exists
|
// check for left and right tree traversal if it exists
|
||||||
if (searchNode->left != nullptr && element < searchNode->left->payload) {
|
if (searchNode->left != nullptr && element < searchNode->left->payload) {
|
||||||
searchNode = searchNode->left;
|
searchNode = searchNode->left;
|
||||||
|
@ -47,8 +51,6 @@ namespace blt {
|
||||||
searchNode = searchNode->right;
|
searchNode = searchNode->right;
|
||||||
continue;
|
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
|
// insert into the lowest node consistent with a BST
|
||||||
if (element < searchNode->payload) {
|
if (element < searchNode->payload) {
|
||||||
searchNode->left = new BST_node();
|
searchNode->left = new BST_node();
|
||||||
|
@ -108,11 +110,14 @@ namespace blt {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
node_binary_search_tree() {
|
node_binary_search_tree() = default;
|
||||||
m_root = new BST_node();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void insert(const T& element) {
|
inline void insert(const T& element) {
|
||||||
|
if (m_root == nullptr) {
|
||||||
|
m_root = new BST_node();
|
||||||
|
m_root->payload = element;
|
||||||
|
return;
|
||||||
|
}
|
||||||
insert(m_root, element);
|
insert(m_root, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue