Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

python - How to find the average of a long list of objects within a binary search tree?

So I am attempting to create a binary search tree where it sorts out a file of around 30000 students into a more organized method. The long file hold the students' names, SSNs (not real of course), emails, and ages. I am wanting to find the average ages of these students, but I am running into some troubles. I am writing the code within the Retrieve method of the overall class for the BST (code is below), so everytime a new "student" is retrieved from the file, it states that me attempting to add the age into the variable age is type None. There is also a Size method that I am not including here that does calculate the total size of the file correctly. Would anyone happen to know of a way I might be able to correct this error?

def Retrieve(self, item): # returns the item if found, None otherwise
    xAge = 0
    avg = 0
    if self.Exists(item) is False:
        self.mRetrieveFailures += 1
        return None
    x = self.RetrieveR(item, self.mRoot)
    xAge += int(x)
    avg = xAge / self.Size()
    #print(avg)
    return x

def RetrieveR(self, item, current):
    age = 0
    x = None
    y = None
    if current is None:
        return None
    if item == current.mItem:
        age = current.mItem
        return current.mItem
    elif item < current.mItem:
        x = self.RetrieveR(item, current.mL)
        age += x        
    elif item > current.mItem:
        y = self.RetrieveR(item, current.mR)
        age += y
    if x is None and y is None:
        return None
    else:
        if x is None:
            return y
        else:
            return x
    return age
question from:https://stackoverflow.com/questions/65862304/how-to-find-the-average-of-a-long-list-of-objects-within-a-binary-search-tree

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...