递归二进制搜索树插入

时间:2017-10-28 06:25:51

标签: python algorithm tree binary-search-tree

当我尝试在树中插入多个值时,我遇到了错误。我希望能够在树的各个层面上填充多个可用的叶子。这是我的代码:

tree = {
    'key' : 'root',
    'left': {
        'key': 'something',
        'left': None, 
        'right': {
            'key': 'something',
            'left': {
                'key': 'somthing', 
                'left': None, 
                'right': None
            }, 'right': {
                'key': 'something',  
                'left': None, 
                'right': None
            }
        }
    }, 'right': {
        'key': 'something',
        'left': {
            'key': 'Something',
            'left':
            {
                'key': 'something', 
                'left': None, 
                'right': None
            }, 'right': None
        }, 'right': {
            'key': 'something',  
            'left': None, 
            'right': None
        }
    }
}

def insert(tree, k):
    if tree['key'] == None:
        tree['key'] = k
    else:
        if tree['key'] > k:
            if tree['left'] == None:
                tree['left'] = k
            else:
                insert(tree['left'], k)

        if tree['key'] < k:
            if tree['right'] == None:
                 tree['right'] = k
            else:
                insert(tree['right'], k)

insert(tree, "hello")
insert(tree, "data science")

我得到的错误如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-114-d826085e4673> in <module>()
     71 
     72 insert(tree, "hello")
---> 73 insert(tree, "data science")
     74 #insert(tree, "jerry")
     75 #insert(tree, "apple")

<ipython-input-114-d826085e4673> in insert(tree, k)
     59                 tree['left'] = k
     60             else:
---> 61                 insert(tree['left'], k)
     62 
     63         if tree['key'] < k:

<ipython-input-114-d826085e4673> in insert(tree, k)
     59                 tree['left'] = k
     60             else:
---> 61                 insert(tree['left'], k)
     62 
     63         if tree['key'] < k:

<ipython-input-114-d826085e4673> in insert(tree, k)
     52 
     53 def insert(tree, k):
---> 54     if tree['key'] == None:
     55         tree['key'] = k
     56     else:

TypeError: string indices must be integers

我不太明白上面的错误。我相信我将树键的值与另一个值进行比较,但我不知道它为什么要求整数。任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:2)

您的基本情况应该是{"key" : k, "left" : None, "right" : None},而不仅仅是k

由于您将树中的每个节点都定义为字典,因此您必须始终尊重此数据结构。

否则,在第一次插入后,字符串"hello"将成为一个已经不正确的节点。然后,当您尝试进行第二次插入时,将在&#34;节点上调用insert函数。这真的是一个字符串。

def insert(tree, k):
    if tree['key'] == None:
        tree['key'] = {"key":k, "left":None, "right":None }
    else:
      # should insert on left
        if tree['key'] > k:
            if tree['left'] == None:
                tree['left'] = {"key":k, "left":None, "right":None }
            else:
                insert(tree['left'], k)
      # should insert on right
        if tree['key'] < k:
            if tree['right'] == None:
                 tree['right'] = {"key":k, "left":None, "right":None }
            else:
                insert(tree['right'], k)