簡體   English   中英

如何對二叉樹中給定值下的所有節點求和?

[英]How can I sum all nodes under a given value in binary search tree?

我的作業需要我對BST中給定值下的所有數字求和。 但是,我不知道該怎么做。 感謝任何幫助。

class BinarySearchTree:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
    def search(self, find_data):
        if self.data == find_data:
            return self
        elif find_data < self.data and self.left != None:
            return self.left.search(find_data)
        elif find_data > self.data and self.right != None:
            return self.right.search(find_data)
        else:
            return None 
    def get_left(self):
        return self.left
    def get_right(self):
        return self.right
    def set_left(self, tree):
        self.left = tree
    def set_right(self, tree):
        self.right = tree
    def set_data(self, data):
        self.data = data
    def get_data(self):
        return self.data

def create_new_bst(lst):
    #creates a new tree with root node 55, and then inserts all the
    #remaining values in order into the BST


def sum_beneath(t, value):
    # don't know how to do


t = create_new_bst([55, 24, 8, 51, 25, 72, 78])
result = sum_beneath(t, 72)
print('Sum beneath 72 =', result)# should show 'Sum beneath 72 = 78'

我是BST的新手,所以我真的不知道如何開始和做這個問題。

def insert(self, new_data):#can I just call this function in 'create_new_bst'?
       if self.data:
            if new_data < self.data:
                if self.left is None:
                    self.left = BinarySearchTree(new_data)
                else:
                    self.left.insert(new_data)
            elif new_data > self.data:
                if self.right is None:
                    self.right = BinarySearchTree(new_data)
                else:
                    self.right.insert(new_data)
        else:
            self.data = data

好的,因為這是一個練習,所以我不會填寫所有內容,但是我會盡力讓您了解應該如何做:

您需要以一種簡單的方法來創建樹:

def create_new_bst(lst):
    tree = BinarySearchTree(tree[0])
    # And then, using the insert method, which is correct, add your nodes in the tree
    return tree

首先,您需要找到根目錄為72子樹

# Replace the pass with the appropriate code

def find_subtree(tree, value):
    if value == tree.data: # This is found yeah !
         pass
    if value > tree.data: # Ok, this is not our data, we should look recursively in one of the children (I will not tell you which one). Maybe we can use find_subtree reccursively?
         pass
    if value < tree.data: # Same as above, but maybe we should look in the other child
         pass
    raise ValueError("Not found value " + str(value)) # Nothing has been found.

現在,您使用my_tree = find_subtree(t, 72)找到了樹,您應該將左樹(如果存在)和右樹(如果存在)相加

def sum_beneath(t, value):
    my_tree = find_subtree(t, value)
    s = 0
    if my_tree.left is not None:
         s += my_tree.left.sum_tree()
    if my_tree.right is not None:
         s += my_tree.right.sum_tree()
    return s

讓我們定義sum_tree方法(在類中)! :)

def sum_tree(self):
    ans =  self.data
    # Add the left sum reccursively
    # Add the right sum reccursively
    return ans

我希望這將幫助您理解BST的概念。 如果您需要幫助,請隨時評論

找到特定值下的節點總和是一個非常有趣的問題,我們可以想到諸如搜索和穿越問題之類的東西,可以有多種方式來實現,但是我可以想到這樣的事-

  1. 對節點進行二進制搜索。
  2. 進行(有序,后序或預排序)遍歷,並保存返回節點的值的結果,即將它們求和。

我能想到的O時間復雜度應該是這樣的-

對於BST中的第n個節點, log(n)應該是搜索時間,對於橫向(按順序,按后順序或按順序),應該為mn ,其中(m是節點)

因此,總數為(log(n)+ m-n)〜O(M)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM