BST: O- Complexity:
For the following functions you are required to determine the O-Complexity of the function and discuss the worst case time complexity. Note that the 'set' being passed into the function stands for a set of numbers entered into a BST. You are required to determine the worst-case time complexity of your implementation of the following operations, and write your answer along with an explanation.
Your answers should be in big-O notation. If an operation involves two sets, then the time complexity should be in terms of n and m, the sizes of the two sets respectively, otherwise it should just be in terms of n, the size of the one set.
Set SetUnion (Set s1, Set s2) { // If first is empty, union will depict nodes of second only if (s1= NULL) { return s2; } } //If second is empty, union will depict nodes of first only else if (s2 == NULL) { return s1; } //Recursively go through, adding values s1-left SetUnion (s1->left, s2->left); s2->right = SetUnion (s1->right, s2->right); s1->data == s2->data; return s1; /** * Returns a new set representing the intersection of the two sets */ Set SetIntersection (Set s1, Set s2) { // Store s1 in temp, create 'result' for storing intersecting elements struct node *temp = s1; struct node *result = NULL; //Whilst not null, if the data is equal, store in 'result' while (temp != NULL) { if (temp->data == s2->data) { } result SetInsert (result, temp->data); //Set temp to right child if less than s2 node. else if(temp->data <= s2->data) { temp = temp->right; } //Set temp to left child if less than s2 node else { temp temp->left; } //Return intersecting values return result;
bool SetEquals (Set s1, Set s2) { } /** //If both are empty, they are equal if (s1 == NULL && s2 == NULL) { return true; } } //If only 1 is empty, they cannot be equal else if (s1 == NULL || s2 == NULL) { return false; } //Traverse in order, If data is not same, return false SetEquals(s1->left, s2->left); if (s1->data != s2->data) { return false; } SetEquals(s1->right, s2->right); //Else, return true return true; *Returns true if set s1 is a subset of set s2, and false otherwise */ bool SetSubset (Set s1, Set s2) { //If tree 1 is empty, it is subset of tree 2 if (s1 == NULL) { return true; } //If tree 2 is empty, tree 1 cannot possibly be a subset else if (s2 == NULL) { return false; } //If 2 trees are identical, s1 is subset else if (SetEquals(s1, s2) == true){ return true; } //Else, not a subset return false;
BST: O- Complexity: For the following functions you are required to determine the O-Complexity of the function and discu
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am