Advertisement
smatskevich

Seminar10

Nov 21st, 2022
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4.   ~Node();
  5.  
  6.   int Data;
  7.   Node* Left = nullptr;
  8.   Node* Right = nullptr;
  9.   Node* Parent = nullptr;
  10.  
  11.   explicit Node(int data, Node* parent = nullptr) : Data(data), Parent(parent) {}
  12. };
  13.  
  14. Node::~Node() {
  15.   // std::cout << "Deleting " << Data << std::endl;
  16.   delete Left;
  17.   // Соответствует двум операциям
  18.   // Left->~Node();
  19.   // free(Left);
  20.   delete Right;
  21.   // std::cout << "Deleted " << Data << std::endl;
  22. }
  23.  
  24. int main1() {
  25.   Node* root = new Node(3);
  26.   root->Right = new Node(5);
  27.   root->Right->Parent = root;
  28.   std::cout << root->Data << std::endl;
  29.   std::cout << root->Right->Data << std::endl;
  30.   delete root->Right;
  31.   delete root;
  32.   std::cout << "It's ok" << std::endl;
  33.   return 0;
  34. }
  35.  
  36. //
  37. class Tree {
  38.  public:
  39.   ~Tree();
  40.   void Add(int key);
  41.   // Возвращает true, если успешно удалили
  42.   bool Remove(int key);
  43.   int GetDepth() const;
  44.   void Print() const;
  45.  
  46.  private:
  47.   Node* root = nullptr;
  48.  
  49.   int GetDepth(Node* node) const;
  50.   void Print(Node* node, int tabs) const;
  51.   void Print1(Node* node, int tabs) const;
  52. };
  53.  
  54. Tree::~Tree() {
  55.   delete root;
  56. }
  57.  
  58. void Tree::Add(int key) {
  59.   if (!root) {
  60.     root = new Node(key);
  61.     return;
  62.   }
  63.   Node* current = root;
  64.   while (true) {
  65.     if (current->Data > key) {
  66.       // Идем в Left
  67.       if (current->Left != nullptr)
  68.         current = current->Left;
  69.       else {
  70.         current->Left = new Node(key, current);
  71.         break;
  72.       }
  73.     } else {
  74.       // Идем в Right
  75.       if (current->Right != nullptr)
  76.         current = current->Right;
  77.       else {
  78.         current->Right = new Node(key, current);
  79.         break;
  80.       }
  81.     }
  82.   }
  83. }
  84.  
  85. bool Tree::Remove(int key) {
  86.   Node* current = root;
  87.   while (current) {
  88.     if (current->Data < key) current = current->Right;
  89.     else if (current->Data > key) current = current->Left;
  90.     else break;
  91.   }
  92.   if (!current) return false;
  93.  
  94.   if (!current->Right && !current->Left) {
  95.     if (current == root)
  96.       root = nullptr;
  97.     else if (current->Parent->Left == current)
  98.       current->Parent->Left = nullptr;
  99.     else
  100.       current->Parent->Right = nullptr;
  101.   } else if (current->Right && !current->Left) {
  102.     if (current == root)
  103.       root = current->Right;
  104.     else if (current->Parent->Left == current)
  105.       current->Parent->Left = current->Right;
  106.     else
  107.       current->Parent->Right = current->Right;
  108.     current->Right->Parent = current->Parent;
  109.   } else if (!current->Right && current->Left) {
  110.     if (current == root)
  111.       root = current->Left;
  112.     if (current->Parent->Left == current)
  113.       current->Parent->Left = current->Left;
  114.     else
  115.       current->Parent->Right = current->Left;
  116.     current->Left->Parent = current->Parent;
  117.   }
  118.   current->Left = nullptr;
  119.   current->Right = nullptr;
  120.   delete current;
  121.   return true;
  122. }
  123.  
  124. int Tree::GetDepth() const {
  125.   return GetDepth(root);
  126. }
  127.  
  128. int Tree::GetDepth(Node *node) const {
  129.   if (!node) return 0;
  130.   return std::max(GetDepth(node->Left), GetDepth(node->Right)) + 1;
  131. }
  132.  
  133. void Tree::Print() const {
  134.   if (root) Print(root, 0);
  135. }
  136.  
  137. void Tree::Print1(Node* node, int tabs) const {
  138.   if (!node) return;
  139.  
  140.   std::cout << node->Data << "\t";
  141.   if (node->Right) {
  142.     Print(node->Right, tabs + 1);
  143.     std::cout << std::endl;
  144.   }
  145.   if (node->Left) {
  146.     for (int i = 0; i < tabs; ++i) std::cout << "\t";
  147.     Print(node->Left, tabs);
  148.   }
  149. }
  150.  
  151. void Tree::Print(Node* node, int tabs) const {
  152.   if (node->Right) Print(node->Right, tabs + 1);
  153.   for (int i = 0; i < tabs; ++i) std::cout << "\t";
  154.   std::cout << node->Data << std::endl;
  155.   if (node->Left) Print(node->Left, tabs + 1);
  156. }
  157.  
  158. int main2() {
  159.   Tree tree;
  160.   tree.Add(4);
  161.   tree.Add(2);
  162.   tree.Add(3);
  163.   tree.Add(1);
  164.   tree.Add(6);
  165.   tree.Add(7);
  166.   tree.Add(5);
  167.   tree.Add(10);
  168.   tree.Add(11);
  169. //  tree.Remove(1);
  170. //  tree.Remove(2);
  171.   // std::cout << sizeof(tree) << std::endl;
  172.   tree.Print();
  173.   std::cout << tree.GetDepth() << std::endl;
  174.   return 0;
  175. }
  176.  
  177. // Декартовы деревья
  178. struct TreapNode {
  179.   int Key;
  180.   int Priority;
  181.   TreapNode* Left = nullptr;
  182.   TreapNode* Right = nullptr;
  183.  
  184.   TreapNode(int key, int priority) : Key(key), Priority(priority) {}
  185. };
  186.  
  187. class Treap {
  188.  public:
  189.   void Add(int key);
  190.   void Print() const { if (root) Print(root, 0); }
  191.  
  192.  private:
  193.   TreapNode* root = nullptr;
  194.   std::pair<TreapNode*, TreapNode*> Split(int key, TreapNode* node);
  195.   TreapNode* Merge(TreapNode* t1, TreapNode* t2);
  196.  
  197.   void Print(TreapNode* node, int tabs) const;
  198. };
  199.  
  200. std::pair<TreapNode*, TreapNode*> Treap::Split(int key, TreapNode* node) {
  201.   if (!node) return {nullptr, nullptr};
  202.   if (node->Key < key) {
  203.     auto [t1, t2] = Split(key, node->Right);
  204.     node->Right = t1;
  205.     return {node, t2};
  206.   } else {
  207.     auto [t1, t2] = Split(key, node->Left);
  208.     node->Left = t2;
  209.     return {t1, node};
  210.   }
  211. }
  212.  
  213. TreapNode* Treap::Merge(TreapNode* left, TreapNode* right) {
  214.   if (left == nullptr || right == nullptr) {
  215.     return left == nullptr ? right : left;
  216.   }
  217.   if (left->Priority > right->Priority) {
  218.     left->Right = Merge(left->Right, right);
  219.     return left;
  220.   } else {
  221.     right->Left = Merge(left, right->Left);
  222.     return right;
  223.   }
  224. }
  225.  
  226. void Treap::Add(int key) {
  227.   auto [t1, t2] = Split(key, root);
  228.   int r = rand();
  229.   TreapNode* new_node = new TreapNode(key, r);
  230.   t1 = Merge(t1, new_node);
  231.   root = Merge(t1, t2);
  232. }
  233.  
  234. void Treap::Print(TreapNode* node, int tabs) const {
  235.   if (node->Right) Print(node->Right, tabs + 1);
  236.   for (int i = 0; i < tabs; ++i) std::cout << "\t";
  237.   std::cout << node->Key << std::endl;
  238.   if (node->Left) Print(node->Left, tabs + 1);
  239. }
  240.  
  241. int main() {
  242.   srand(clock());
  243.   Treap treap;
  244.   treap.Add(4);
  245.   treap.Add(5);
  246.   treap.Add(1);
  247.   treap.Add(8);
  248.   treap.Add(5);
  249.   treap.Add(3);
  250.   treap.Print();
  251.  
  252.   return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement