Husi012

ADS Praktikum 3 / ProofRBCriterion 2

May 16th, 2022 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. int proofRBCriterion2(TreeNode* node) {
  2.     TreeNode* left = node->getLeft();
  3.     TreeNode* right = node->getRight();
  4.  
  5.     int height;
  6.     int add = 0;
  7.    
  8.     if (left == nullptr && right == nullptr) {
  9.         return 0;
  10.     }
  11.     else if (left == nullptr) {
  12.         height = proofRBCriterion2(right);
  13.         if (right->isBlack()) {
  14.             add = 1;
  15.         }
  16.     }
  17.     else if (right == nullptr) {
  18.         height = proofRBCriterion2(left);
  19.         if (left->isBlack()) {
  20.             add = 1;
  21.         }
  22.     }
  23.     else {
  24.         int leftHeight = proofRBCriterion2(left);
  25.         int rightHeight = proofRBCriterion2(right);
  26.  
  27.         if (left->isBlack()) {
  28.             leftHeight += 1;
  29.         }
  30.         if (right->isBlack()) {
  31.             rightHeight += 1;
  32.         }
  33.  
  34.         if (leftHeight != rightHeight) {
  35.             return -1;
  36.         }
  37.  
  38.         height = leftHeight;
  39.     }
  40.  
  41.     if (height == -1) {
  42.         return -1;
  43.     }
  44.  
  45.     return height + add;
  46. }
Add Comment
Please, Sign In to add comment