2016-04-04 18 views
2

我想在Python中使用字典和遞歸在BST(二進制搜索樹)的實現中更新節點中的值。但是,它不起作用。請賜教!更新二進制搜索樹中的數據

這裏是我使用的字典在Python BST實現:

tree = { 
    'right': None, 
    'data': [9124, 5.82, 5], 
    'left': { 
     'right': { 
      'right': None, 
      'data': [8298, 2.4, 6], 
      'left': None 
     }, 
     'data': [5549, 4.76, 5], 
     'left': None 
    } 
} 

在視覺上看起來是這樣的:

Visual look of the above dictionary

這裏是我試圖增加和更新每個中間值(價格)在'數據'中使用遞歸列出10%,但由於某種原因,它不工作我不知道:

def IncreaseByTen(tree): 
    if tree == None: 
     return 0 

    price = tree['data'][1] 

    IncreaseByTen(tree['left']) 
    price += (price * 0.1) 
    IncreaseByTen(tree['right']) 

回答

0

以下行只是改變只是局部變量price,而不是列表項:

price += (price * 0.1) 

您需要的值分配回列表項:

price = tree['data'][1] 
... 
price += (price * 0.1) 
tree['data'][1] = price # <---- 

,或者您可以使用*=

tree['data'][1] *= 1.1