Question

Please provide the big oh notation for running time and space complexity for the following functions...

Please provide the big oh notation for running time and space complexity for the following functions (available, into, out, size, and printAll):

int

SplayTreeInventory::available(string id){  

Node* temp=stmap->findSplay(id);

if(temp!=NULL)

return temp->value;

else

return -1;  

}

void

SplayTreeInventory::into(string id, int amount)

{

Node* temp=stmap->findSplay(id);

if(temp==NULL){

stmap->putSplay(id, amount);

}

else{

temp->key+=amount;

}

}

void

SplayTreeInventory::out(string id, int amount)

{

Node* temp=stmap->findSplay(id);

if(temp==NULL){

}

else{

if(temp->value<=amount)

stmap->erase(id);

else

temp->value-=amount;

}

}

int

SplayTreeInventory::size()

{

return stmap->size();

}

void

SplayTreeInventory::printAll()

{

printAllHelper(this->stmap->root);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int SplayTreeInventory::available(string id){  

Node* temp=stmap->findSplay(id); //constant

if(temp!=NULL) //constant

return temp->value; //constant

else

return -1; //constant

}

Constant time and space ----- O(1)

------------------------------------------

void SplayTreeInventory::into(string id, int amount){

Node* temp=stmap->findSplay(id); //same as findSplay

if(temp==NULL){ //constant

stmap->putSplay(id, amount); //same as putSplay

}

else{

temp->key+=amount; //constant

}

}

Time complexity = O(max(findSplay,putSplay))

Space complexity = O(max(findSplay,putSplay))

---------------------------------------------

void SplayTreeInventory::out(string id, int amount){
Node* temp=stmap->findSplay(id); //same as findsplay

if(temp==NULL){ //constant
}
else{
if(temp->value<=amount) //constant
stmap->erase(id); //same as erase
else
temp->value-=amount; //constant
}
}

Time Complexity = O(erase)
Space Complexity = O(erase)

-------------------------------------------------

int SplayTreeInventory::size(){
return stmap->size();
}
Time and space O(1)
----------------------------

void SplayTreeInventory::printAll(){
printAllHelper(this->stmap->root);
}
Time and Space are same as printHelper
----------------------------

Add a comment
Know the answer?
Add Answer to:
Please provide the big oh notation for running time and space complexity for the following functions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please provide the big oh notation for running time and space complexity for the following methods...

    Please provide the big oh notation for running time and space complexity for the following methods (splay, findSplay, putSplay, eraseSplay: void SplayTreeMap::splay(Node* x) {    if(x!=NULL){ while (x -> parent != NULL ) { Node *p = x -> parent; Node *g = p -> parent; if (g == NULL) zig(x); else if (g -> left == p && p -> left == x) zigZig(x); else if (g -> right == p && p -> right == x) zigZig(x); else...

  • Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms...

    Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. Show your work b) void func(int n) { for (int i = 0; i < n; i = i + 10) { for (int j = 0; j < i; ++i) { System.out.println("i = " + i); System.out.println("j = " + j);

  • What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) {...

    What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) { for (int i = 1; i <= heightOfTree(root); i++) { System.out.print("Level " + i + " : "); printSingleLevelRecursively(root, i); System.out.print("\n"); } } public int heightOfTree(Node root) { if (root != null)    return super.max(heightOfTree(root.l), heightOfTree(root.r)) + 1; return 0; } public void printSingleLevelRecursively(Node root, int level) { if (root == null) return; if (level == 1) System.out.print(root.key + " "); else if (level...

  • find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code...

    find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code 1: for (int i = n; i > 0; i /= 2) { for (int j = 1; j < n; j *= 2) {     for (int k = 0; k < n; k += 2) {               // constant number of operations here     } } } Code 2: Hint: Lecture Note 5, Page 7-8 void f(int n) { if (n...

  • 4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode...

    4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. howing your work is not required (although showing work may allow some partial t in the case your answer is wrong-don't spend a lot of time showing your work.). You MUST choose your answer from the following (not given in any particular order), each of which could be re-used (could be the answer for...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • Lab 6 Help

    I need help with this lab assignment. I use C++, thank you. Derive a MinHeap class from the BST class of your Lab 4 code.Define/Override only the Search/Insert/Delete methods in the new class.Write a new main for this lab which:Will only be a test program for the MinHeap.Declares and creates a MinHeap object as necessary.Declares and creates the Dollar objects as necessary.Inserts the 20 Dollar objects specified in Lab 4 in the same order.Performs all 4 traversals after the inserting...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

  • Give a big-Oh characterization, in terms of n,of the running time for each of the following...

    Give a big-Oh characterization, in terms of n,of the running time for each of the following code segments (use the drop-down): - public void func1(int n) { A. @(1). for (int i = n; i > 0; i--) { System.out.println(i); B. follogn). for (int j = 0; j <i; j++) System.out.println(j); c.e(n). System.out.println("Goodbye!"); D.@(nlogn). E.e(n). F.ein). public void func2 (int n) { for (int m=1; m <= n; m++) { system.out.println (m); i = n; while (i >0){ system.out.println(i); i...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT