1

I have written this code that does the job i want it to. What I want to do is figure out the big O notation for the code. So I want to learn how to calculate the time complexity and end up with a big O notation result. How is it done in lay mans terms?

"""

def treetocode(hTree):

    code = dict()
    
    def getCode(hNode, currentcode=""):

        if (hNode == None): return
        if (hNode.left == None and hNode.right == None):
            code[hNode.char] = currentcode
        getCode(hNode.left, currentcode + "0")
        getCode(hNode.right, currentcode + "1")
        if hNode.char == None:
            return None
        else:
            print('Character = {}  :  Freq = {} --- Huffman code {}'.format(hNode.char, hNode.freq, currentcode))

    getCode(hTree)
    return code

"""

4

1 Answer 1

2

Time complexity is unrelated to the language you're using, unless your implementation causes the code to behave differently than the algorithm you have in mind. Since you came up with the algorithm, you can go over the usual steps used to figure out time complexity.

Run the program in your mind. How many times is the function called for every node of the tree? (or whatever structure you have).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.