Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bst_tree.h"
- void bst_tree::insert(int64_t data) {
- node* new_node = new node(data);
- node* nullNode = nullptr;
- // TODO: Naimplementujte zde vlaknove-bezpecne vlozeni do binarniho vyhledavaciho stromu
- if (root == nullptr) {
- if (root.compare_exchange_strong(nullNode, new_node)) {
- return;
- }
- }
- node* cur = root;
- while (true) {
- if (data > cur->data) {
- node* tmp = cur->right;
- if (tmp == nullptr && cur->right.compare_exchange_strong(tmp, new_node)) {
- return;
- }
- cur = cur->right;
- } else {
- node *tmp = cur->left;
- if (tmp == nullptr && cur->left.compare_exchange_strong(tmp, new_node)) {
- return;
- }
- cur = cur->left;
- }
- }
- }
- // Rekurzivni funkce pro pruchod stromu a dealokaci pameti prirazene jednotlivym uzlum
- void delete_node(bst_tree::node* node) {
- if (node == nullptr) {
- return;
- }
- delete_node(node->left);
- delete_node(node->right);
- delete node;
- }
- bst_tree::~bst_tree() {
- delete_node(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement