简体   繁体   English

计算二叉树节点

[英]Calculating binary tree nodes

Thank you for taking some time to look at my problem.感谢您花一些时间来查看我的问题。

I am trying to plot a tree graph by reading paths that are in the form of a list of binary numbers.我试图通过读取二进制数列表形式的路径来 plot 一个树形图。 The way I am doing this is first I create a list of tuples that contain 2 lists, the right ones are paths from test_d in the same order they are in the dictionary (starting from the 2nd one), left ones are the paths that have the most number of same nodes with the right paths.我这样做的方式是首先我创建一个包含 2 个列表的元组列表,右侧是来自test_d的路径,其顺序与它们在字典中的顺序相同(从第二个开始),左侧是具有具有正确路径的相同节点数量最多。 This way, I could tell the algorithm from which place the branching should happen.这样,我可以告诉算法应该从哪个位置进行分支。

To label the paths properly, unique integers are assigned to previously unvisited nodes, and for the visited nodes labels are copied from the visited paths. label 的路径正确,唯一的整数被分配给以前未访问的节点,访问节点的标签从访问的路径中复制。 So the first path, no matter what it is, gets a path [1,2,3,4,5,6].所以第一条路径,不管它是什么,都会得到一个路径[1,2,3,4,5,6]。 The second path is [7,8,9,10,11,12], because the first nodes of the two paths are different, which means that they have different paths.第二条路径是[7,8,9,10,11,12],因为两条路径的第一个节点不同,也就是说它们有不同的路径。 For the third path, the first nodes of the left and right paths are the same (1 and 0), which means that for the first two nodes, their path is the same, therefore [7,8,13,14,15,16].对于第三条路径,左右路径的第一个节点相同(1和0),这意味着对于前两个节点,它们的路径相同,因此[7,8,13,14,15, 16]。

Here is my data: This is the dictionary containing all the paths:这是我的数据:这是包含所有路径的字典:

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]}

And this is the list of tuples containing paths from dictionary and paths that are the most similar to them:这是包含字典中的路径和与它们最相似的路径的元组列表:

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])]

This is the code I wrote to obtain unique paths:这是我为获取唯一路径而编写的代码:

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)

To summarize the problem, this is how my end results looks like:总结一下这个问题,这就是我的最终结果:

{'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.带有红色节点的树是使用不正确的 output 绘制的。 The tree with grey nodes is how the correct tree is supposed to look like带有灰色节点的树是正确的树应该是什么样子

I understand that this is a lot of text, and I apologize for that.我知道这是很多文字,对此我深表歉意。 Thank you everyone in advance!提前谢谢大家!

Corrected code for my problem:更正了我的问题的代码:

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

You match the nodes visited in if right[j] == left[j] and same_path==True: but do not track left or right path from already visited nodes. if right[j] == left[j] and same_path==True:但不跟踪已访问节点的左路径或右路径。 i6 and i7 differentiate after node 23 but in constructing i9 your simple match via right[j] == left[j] always yields the same path. i6i7在节点23之后进行区分,但在构造i9时,您通过right[j] == left[j]进行的简单匹配总是产生相同的路径。 You should use established binary tree traversals (see enter link description here for a general intro or enter link description here for a concrete Python implementation.您应该使用已建立的二叉树遍历(请参阅在此处输入链接描述以获得一般介绍,或在此处输入链接描述以获得具体的 Python 实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM