Advertisement
Guest User

Untitled

a guest
May 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. // Problem 1
  2. bool BinaryTree::isFullRecursion(Node *root) {
  3.     if (root == nullptr)
  4.         return true;
  5.     int mid = 1;
  6.     if (root->left!=nullptr && root->right == nullptr)
  7.         mid = 0;
  8.     if (root->left==nullptr && root->right != nullptr)
  9.         mid = 0;
  10.     int left = isFullRecursion(root->left);
  11.     int right = isFullRecursion(root->right);
  12.     return mid && left && right;
  13. }
  14.  
  15. bool BinaryTree::isFull()
  16. {
  17.     return isFullRecursion(root);
  18. }
  19.  
  20. // Problem 2
  21. int BinaryTree::countNodeRecursion(Node *root)
  22. {
  23.     if (root == nullptr)
  24.         return 0;
  25.     int count = 0;
  26.     int left = countNodeRecursion(root->left);
  27.     int right = countNodeRecursion(root->right);
  28.     return left + right + 1;
  29. }
  30.  
  31. bool BinaryTree::isCompleteRecursion(Node *root) {
  32.     if (root == nullptr)
  33.         return true;
  34.     int height = getHeightRecursion(root);
  35.     int numNode = countNodeRecursion(root);
  36.     if (numNode == pow(2, height)-1)
  37.         return true;
  38.     return false;
  39. }
  40.  
  41. bool BinaryTree::isComplete()
  42. {
  43.     return isCompleteRecursion(root);
  44. }
  45.  
  46. // Problem 3
  47. bool BinaryTree::isNearlyComplete()
  48. {
  49.     return isNearlyComplete2(root);
  50. }
  51.  
  52. bool BinaryTree::isNearlyComplete2(Node *root) {
  53.     if (root == NULL)
  54.         return true;
  55.     Queue<Node*> q;
  56.     q.enQueue(root);
  57.     bool leafFound = false;
  58.     Node* temp;
  59.     while(!q.isClear()) {
  60.         q.deQueue(temp);
  61.         if((leafFound == true) && (temp->left != NULL || temp->right != NULL))
  62.             return false;
  63.         if(temp->left == NULL && temp->right != NULL)
  64.             return false;
  65.         if(temp->left == NULL && temp->right ==NULL && leafFound == false)
  66.             leafFound = true;
  67.         if(temp->left)
  68.             q.enQueue(temp->left);
  69.         if(temp->right)
  70.             q.enQueue(temp-> right);
  71.     }
  72.     return true;
  73. }
  74.  
  75. //Problem 4
  76. int BinaryTree::findLCARecursion(Node *root, int num1, int num2)
  77. {
  78.     if (root->data == num1 || root->data == num2)
  79.         return root->data;
  80.     if (root->data > num1 && root->data > num2)
  81.         return findLCARecursion(root->left, num1, num2);
  82.     if (root->data < num1 && root->data < num2)
  83.         return findLCARecursion(root->right, num1, num2);
  84.     return root->data;
  85. }
  86.  
  87. int BinaryTree::findLCA(int num1, int num2)
  88. {
  89.     return findLCARecursion(root, num1, num2);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement