Advertisement
eggggggggor

bst tree insert omp parallel

Jun 14th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include "bst_tree.h"
  2.  
  3. void bst_tree::insert(int64_t data) {
  4.     node* new_node = new node(data);
  5.     node* nullNode = nullptr;
  6.  
  7.     // TODO: Naimplementujte zde vlaknove-bezpecne vlozeni do binarniho vyhledavaciho stromu
  8.  
  9.     if (root == nullptr) {
  10.         if (root.compare_exchange_strong(nullNode, new_node)) {
  11.             return;
  12.         }
  13.     }
  14.  
  15.     node* cur = root;
  16.     while (true) {
  17.         if (data > cur->data) {
  18.             node* tmp = cur->right;
  19.             if (tmp == nullptr && cur->right.compare_exchange_strong(tmp, new_node)) {
  20.                 return;
  21.             }
  22.             cur = cur->right;
  23.         } else {
  24.             node *tmp = cur->left;
  25.             if (tmp == nullptr && cur->left.compare_exchange_strong(tmp, new_node)) {
  26.                 return;
  27.             }
  28.             cur = cur->left;
  29.         }
  30.     }
  31. }
  32.  
  33. // Rekurzivni funkce pro pruchod stromu a dealokaci pameti prirazene jednotlivym uzlum
  34. void delete_node(bst_tree::node* node) {
  35.     if (node == nullptr) {
  36.         return;
  37.     }
  38.  
  39.     delete_node(node->left);
  40.     delete_node(node->right);
  41.     delete node;
  42. }
  43.  
  44. bst_tree::~bst_tree() {
  45.     delete_node(root);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement