Question

In: Computer Science

[C++] Find the Recurrence Relation equations for the following functions: Please explain how you got these...

[C++] Find the Recurrence Relation equations for the following functions:

Please explain how you got these equations.

BSTClass::Node* BSTClass::createNode(const string x) {

   Node* newNode = new Node;

   newNode->data = x;
   newNode->left = nullptr;
   newNode->right = nullptr;

   return newNode;
}

void BSTClass::insertNode(const string x) {

   insertNodeUtil(x, root);
}

void BSTClass::insertNodeUtil(const string data, Node* subTreePtr) {

   if (root == nullptr) {
       root = createNode(data);
   }
   else if (data <= subTreePtr->data) {
       if (subTreePtr->left == nullptr) {
           subTreePtr->left = createNode(data);
       }
       else {
           insertNodeUtil(data, subTreePtr->left);
       }
   }
   else {
       if (subTreePtr->right == nullptr) {
           subTreePtr->right = createNode(data);
       }
       else {
           insertNodeUtil(data, subTreePtr->right);
       }

   }
}

void BSTClass::printInOrder() {

   printInOrderUtil(root);
   cout << endl;

}

void BSTClass::printInOrderUtil(Node* subTreePtr) {

   if (root == nullptr) {
       cout << "The TREE is Empty ..." << endl;
   }
   else {
       if (subTreePtr->left != nullptr) {
           printInOrderUtil(subTreePtr->left);
       }

       cout << subTreePtr->data << " ";

       if (subTreePtr->right != nullptr) {
           printInOrderUtil(subTreePtr->right);
       }
   }

}


bool BSTClass::findKey(const string key) {

   return findKeyUtil(key, root);
}

bool BSTClass::findKeyUtil(const string key, Node* subTreePtr) {

   if (root == nullptr) {
       cout << "Key (" << key << ") is NOT found, it is an empty tree ..." << endl;
       return false;
   }

   if (subTreePtr == nullptr) {
       cout << "Key (" << key << ") is NOT found" << endl;
       return false;
   }
   else if (subTreePtr->data == key) {
       cout << "Key (" << key << ") is FOUND" << endl;
       return true;
   }
   else if (key < subTreePtr->data) {
       return findKeyUtil(key, subTreePtr->left);
   }
   else {   
       return findKeyUtil(key, subTreePtr->right);
   }
}


int BSTClass::treeHeight() {

   if (root == nullptr)
       return 0;
   else
       return treeHeightUtil(root);

}


int BSTClass::treeHeightUtil(Node* subTreePtr) {

   if (subTreePtr == nullptr) {
       return 0;
   }
   else {
       int lHeight = treeHeightUtil(subTreePtr->left);
       int rHeight = treeHeightUtil(subTreePtr->right);

       return (lHeight >= rHeight ? lHeight + 1 : rHeight + 1);
   }
}

Solutions

Expert Solution

1.) BSTClass::Node* BSTClass::createNode(const string x).

This function will work in constant time, i.e., O(1).

2.) void BSTClass::insertNodeUtil(const string data, Node* subTreePtr)

This function is inserting the element either in the left part or right part of the tree or to the root. After we get the location, where we want to insert the element in the tree, it takes constant time to insert at that location.

So, the recurrence equation will be: T(n) = T(n/2) + Θ(1).

So the solution is Θ(Logn)

3.) void BSTClass::insertNode(const string x)

Since, this function is just calling the function insertNodeUtil(data, tree), the recurrence equation will be same.

i.e., T(n) = T(n/2) + Θ(1).

4.) void BSTClass::printInOrderUtil(Node* subTreePtr)

This function is first calling the left part of the tree and then the right part of the tree.

So, the recurrence equation will be: T(n) = 2*T(n/2) + Θ(1).

So the solution is Θ(n)

5.) Recurrence equation for function printInOrder() will be same as function printInOrderUtil().

6.) bool BSTClass::findKeyUtil(const string key, Node* subTreePtr)

This function will first compare the key to the root of the tree. Then, it will either go to the left part of tree or to the right part of the tree.

So, the recurrence equation will be: T(n) = T(n/2) + Θ(1).

So the solution is Θ(Logn)

7.) Recurrence equation for function findKey() will be same as function findKeyUtil().

8.)  int BSTClass::treeHeightUtil(Node* subTreePtr)

As the algorithm, visit each node of the tree exactly once.

This function is first calling the left part of the tree and then the right part of the tree.

So, the recurrence equation will be: T(n) = 2*T(n/2) + Θ(1).

So the solution is Θ(n)

9.) Recurrence equation for function treeHeight() will be same as function treeHeightUtil()


Related Solutions

Use generating functions to solve the following recurrence relation: an = 2an−1 + 3n , n...
Use generating functions to solve the following recurrence relation: an = 2an−1 + 3n , n ≥ 1 a0 = 2
a) Find the recurrence relation for the number of ways to arrange flags on an n...
a) Find the recurrence relation for the number of ways to arrange flags on an n foot flagpole with 1 foot high red flags, 2 feet high white flags and 1 foot high blue flags. b) solve the recurrence relation of part a
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
Find and solve a recurrence relation for the number of ways to stack n poker
Find and solve a recurrence relation for the number of ways to stack n poker chips using red, white and blue chips such that no two red chips are together. Use your solution to compute the number of ways to stack 15 poker chips.
find a recurrence relation for the number of bit strings of length n that contain the...
find a recurrence relation for the number of bit strings of length n that contain the string 10. What are the initial conditions? How many bit strings of length eight contain the string 10
Characterize each of the following recurrence equations using the master method (assuming that T(n) = c...
Characterize each of the following recurrence equations using the master method (assuming that T(n) = c for n ≤ d, for constants c > 0 and d ≥ 1). a. T(n) = 2T(n/2) + √n b. T(n) = 8T(n/2) + n2 c. T(n) = 16T(n/2) + n4 d. T(n) = 7T(n/3) + n e. T(n) = 9T(n/3) + n3.1
Find a recurrence relation for the number of binary strings of length n which do not...
Find a recurrence relation for the number of binary strings of length n which do not contain the substring 010
find a recurrence relation for the number of bit strings of length n that contain two...
find a recurrence relation for the number of bit strings of length n that contain two consecutive 1s. What are the initial conditions? How many bit strings of length eight contain two consecutive 1s
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm....
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm. T(n) = T(n-1)+1 , if n> 0 1 if n = 0
Find the intensity of 22.0 dB sound. Can you explain how you got it and what...
Find the intensity of 22.0 dB sound. Can you explain how you got it and what is dB? sorry its confusing and I keeping getting it wrong, I think the units would be Hz but I am not sure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT