Advertisement
smatskevich

Seminar13

Dec 24th, 2021
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <ctime>
  4.  
  5. // Стуктура узла декартового дерева по неявному ключу.
  6. struct Node {
  7.   int Data;
  8.   int Priority;
  9.   int Count = 1;
  10.   Node* Left = nullptr;
  11.   Node* Right = nullptr;
  12.  
  13.   explicit Node(int data): Data(data), Priority(rand()) {}
  14. };
  15.  
  16. int Count(Node* node) {
  17.   return node ? node->Count : 0;
  18. }
  19.  
  20. std::pair<Node*, Node*> Split(Node* node, int k) {
  21.   assert(k <= Count(node));
  22.   if (k == 0) return {nullptr, node};
  23.   if (k == Count(node)) return {node, nullptr};
  24.  
  25.   if (Count(node->Left) >= k) {
  26.     auto [first, second] = Split(node->Left, k);
  27.     node->Left = second;
  28.     return {first, node};
  29.   } else {
  30.     auto [first, second] = Split(node->Right, k - Count(node->Left) - 1);
  31.     node->Right = first;
  32.     return {node, second};
  33.   }
  34. }
  35.  
  36. int main() {
  37.   time_t sid = time(0);
  38.   std::cout << sid << std::endl;
  39.   srand(sid);
  40.   Node* root = new Node(5);
  41.   std::cout << "Hello, World!" << std::endl;
  42.   return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement