【问题标题】:Calculating binary tree nodes计算二叉树节点
【发布时间】:2020-12-12 01:14:36
【问题描述】:

感谢您抽出时间来查看我的问题。

我试图通过读取二进制数列表形式的路径来绘制树形图。我这样做的方法是首先创建一个包含 2 个列表的元组列表,正确的是来自 test_d 的路径,其顺序与它们在字典中的顺序相同(从第二个开始),左侧是与正确路径具有最多相同节点的路径。这样,我就可以告诉算法应该从哪个位置进行分支。

为了正确标记路径,将唯一整数分配给以前未访问过的节点,并且从访问过的路径中复制已访问节点的标签。所以第一条路径,不管它是什么,都会得到一个路径[1,2,3,4,5,6]。第二条路径是[7,8,9,10,11,12],因为两条路径的第一个节点不同,也就是说它们有不同的路径。对于第三条路径,左右路径的第一个节点相同(1和0),这意味着对于前两个节点,它们的路径相同,因此[7,8,13,14,15, 16]。

这是我的数据: 这是包含所有路径的字典:

test_d = {'d1':  [0, 1, 0, 1, 1, 0],
          'd2':  [1, 0, 0, 0, 0, 1],
          'd3':  [1, 0, 1, 1, 1, 1],
          'd4':  [1, 1, 1, 1, 1, 0],
          'd5':  [0, 1, 0, 1, 1, 1],
          'd6':  [0, 0, 0, 1, 1, 1],
          'd7':  [0, 0, 1, 1, 0, 1],
          'd8':  [0, 0, 0, 1, 0, 1],
          'd9':  [0, 0, 1, 0, 0, 1],
          'd10': [0, 0, 1, 1, 0, 0],
          'd11': [1, 0, 1, 1, 1, 0]}

这是包含字典中的路径和与它们最相似的路径的元组列表:

MAX = [([0, 1, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1]),
       ([1, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1]),
       ([1, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 0]),
       ([0, 1, 0, 1, 1, 0], [0, 1, 0, 1, 1, 1]),
       ([0, 1, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1]),
       ([0, 0, 0, 1, 1, 1], [0, 0, 1, 1, 0, 1]),
       ([0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1]),
       ([0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 0, 1]),
       ([0, 0, 1, 1, 0, 1], [0, 0, 1, 1, 0, 0]),
       ([1, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 0])]

这是我为获取唯一路径而编写的代码:

init_v = list(range(1, len(MAX[0][0])+1))
ind_d = {'i1': init_v}
same_path = True 
count = len(MAX[0][0])

for i in range(0, len(MAX)):
    temp = []
    left = MAX[i][0]
    right = MAX[i][1]

    for j in range(len(left)):
        if right[j] == left[j] and same_path==True:
            for k in MAX:
                if left == k[0]:
                    HALF = k[1]
                    break
            IND = MAX.index((left, HALF))
            temp.append(ind_d['i'+str(IND+1)][j])
        else:
            count += 1
            temp.append(count)
            same_path = False
        
    ind_d['i'+str(i+2)] = temp
    same_path=True

print(ind_d)

总结问题,这就是我的最终结果:

{'i1':  [1, 2, 3, 4, 5, 6],
 'i2':  [7, 8, 9, 10, 11, 12],
 'i3':  [7, 8, 13, 14, 15, 16],
 'i4':  [7, 17, 18, 19, 20, 21],
 'i5':  [1, 2, 3, 4, 5, 22],
 'i6':  [1, 23, 24, 25, 26, 27],
 'i7':  [1, 23, 28, 29, 30, 31],
 'i8':  [1, 23, 24, 25, 32, 33],
 'i9':  [1, 23, 24, 34, 35, 36], #correct: [1, 23, 28, 34, 35, 36]
 'i10': [1, 23, 24, 25, 32, 37], #correct: [1, 23, 28, 29, 30, 37]
 'i11': [1, 23, 24, 25, 32, 38]} #correct: [7, 8, 13, 14, 15, 38]

The tree with red nodes was plotted using the incorrect output. The tree with grey nodes is how the correct tree is supposed to look like

我知道这是很多文字,对此我深表歉意。提前谢谢大家!

【问题讨论】:

    标签: python tree


    【解决方案1】:

    为我的问题更正了代码:

    init_v = list(range(1, len(MAX[0][0])+1))
    ind_d = {'i1': init_v}
    same_path = True 
    count = len(MAX[0][0])
    
    for i in range(0, len(MAX)):
        temp = []
        left = MAX[i][0]
        right = MAX[i][1]
    
        for j in range(len(left)):
            if right[j] == left[j] and same_path==True:
                for k in range(len(MAX)):
                    if left == test_d['d1']:
                        IND = 0
                    elif left == MAX[k][1]:
                        IND = MAX.index(MAX[k]) + 1
                        break
                temp.append(ind_d['i'+str(IND+1)][j])
            else:
                count += 1
                temp.append(count)
                same_path = False
            
        ind_d['i'+str(i+2)] = temp
        same_path=True
    

    【讨论】:

      【解决方案2】:

      您匹配if right[j] == left[j] and same_path==True: 中访问的节点,但不跟踪已访问节点的左右路径。 i6i7 在节点 23 之后进行区分,但在通过 right[j] == left[j] 构造 i9 您的简单匹配时总是产生相同的路径。您应该使用已建立的二叉树遍历(请参阅enter link description here 了解一般介绍或enter link description here 了解具体的 Python 实现。

      【讨论】:

      • 我现在明白这个问题了。我做了一些相应的改变,现在一切都像魅力一样。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2020-02-21
      • 2012-10-04
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多