Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. class Node
  2. {
  3. public:
  4.     Node* left;
  5.     Node* right;
  6.     Node* parent;
  7.  
  8.     inline int getValue() const noexcept
  9.     {
  10.         return value;
  11.     }
  12.  
  13.     ///
  14.     /// \brief Useful when you have to remove a node with two children.
  15.     ///
  16.     /// In this implementation we remove a node by taking a random leaf and
  17.     /// putting it in the \p start's place.
  18.     ///
  19.     /// \param start Where to start looking for a leaf node.
  20.     ///
  21.     static Node* getLeafStarting(Node* start) noexcept
  22.     {
  23.         while(start->left != nullptr) {
  24.             start = start->left;
  25.         }
  26.  
  27.         if(start->right != nullptr) {
  28.             return getLeafStarting(start->right);
  29.         }
  30.  
  31.         return start;
  32.     }
  33.  
  34.     ///
  35.     /// \brief What should removing a node with 2 children mean? Is it a binary
  36.     ///        search tree?
  37.     ///
  38.     static Node* removeNodes(Node* node, int value)
  39.     {
  40.         Node* current = node;
  41.  
  42.         bool hasLeft = current->left != nullptr;
  43.         bool hasRight = current->right != nullptr;
  44.  
  45.         if(hasLeft) {
  46.             removeNodes(current->left, value);
  47.         }
  48.         if(hasRight) {
  49.             removeNodes(current->right, value);
  50.         }
  51.  
  52.         if(!hasLeft && !hasRight && value == current->getValue()) {
  53.             node = current->parent;
  54.  
  55.             deleteNode(current);
  56.         }
  57.         else if((!hasLeft || !hasRight) && value == current->getValue()) {
  58.             if(hasLeft) {
  59.                 current->left->parent = current->parent;
  60.             }
  61.             else if(hasRight) {
  62.                 current->right->parent = current->parent;
  63.             }
  64.  
  65.             node = current->right;
  66.  
  67.             deleteNode(current);
  68.         }
  69.         else if(hasLeft && hasRight && value == current->getValue()) {
  70.             auto leaf = getLeafStarting(current);
  71.  
  72.             leaf->parent = current->parent;
  73.             leaf->left = current->left;
  74.             leaf->right = current->right;
  75.  
  76.             node = leaf;
  77.  
  78.             deleteNode(current);
  79.         }
  80.  
  81.         return node;
  82.     }
  83.  
  84. private:
  85.     int value;
  86.  
  87.     static void deleteNode(Node* node) noexcept
  88.     {
  89.         node->left = nullptr;
  90.         node->right = nullptr;
  91.         node->parent = nullptr;
  92.  
  93.         delete node;
  94.     }
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement