简体   繁体   English

从给定的输入集实现 n 叉树

[英]Implementing n-ary tree from a given set of inputs

I am trying to create a Tree from given inputs.我正在尝试从给定的输入创建一棵树。 One root will be there including child nodes and sub child nodes.一个根将在那里,包括子节点和子子节点。 I can implement the Tree where I can add a child node to a specific master node (where I already know the root).我可以实现树,在那里我可以将子节点添加到特定的主节点(我已经知道根节点)。 But, I am trying to figure out that what is the recommended way to implement the tree where we have to first find the root node from the given input set.但是,我试图弄清楚实现树的推荐方法是什么,我们必须首先从给定的输入集中找到根节点。 As an, example:举个例子:

There are n lines,
Value in each line represent the master node of that line number.
4 ( n = 4, next n lines)

1 ( 1 is a master node of 1, here line number is 1)
1 (1 is a master node of 2, here line number is 2)
2 (2 is a master node of 3, here line number is 3)
1 (1 is a master node of 4, here line number is 4)

So the tree should look like,所以这棵树应该看起来像,

   1
 / | 
2  4
|
3

Here, we can see that root node is 1. But before knowing all the input values, we can not guess the root node.在这里,我们可以看到根节点为 1。但是在知道所有输入值之前,我们无法猜测根节点。 Before implementing the tree, should I first find out the root node from the inputs?在实现树之前,我应该首先从输入中找出根节点吗? Or, is there any another way?或者,还有其他方法吗?

Below code is to add a node into the tree:下面的代码是在树中添加一个节点:

class Node : 

    # Utility function to create a new tree node 
    def __init__(self ,key): 
        self.key = key 
        self.child = [] 

def printNodeLevelWise(root): 
    if root is None: 
        return

    queue = [] 
    queue.append(root) 

    while(len(queue) >0): 

        n = len(queue) 
        while(n > 0) : 

            # Dequeue an item from queue and print it 
            p = queue[0] 
            queue.pop(0) 
            print (p.key,) 

            # Enqueue all children of the dequeued item 
            for index, value in enumerate(p.child): 
                queue.append(value) 

            n -= 1
        print ("") 


root = Node(1) 
root.child.append(Node(2)) 
root.child.append(Node(4)) 
root.child[0].child.append(Node(3)) 
printNodeLevelWise(root) 

Above code will give implement this tree:上面的代码将实现这棵树:

   1
 / | 
2  4
|
3

But, what is the recommended way to implement the same from the given input?但是,从给定的输入中实现相同的推荐方法是什么?

Assuming that:假如说:

  • with "master" you mean "parent", and “主人”的意思是“父母”,并且
  • when a line has a self-reference -- such that i occurs on line i of the list of masters -- it is the root当一行有一个自引用时——这样i出现在主列表的第i行——它是根

...then you could proceed like this: ...然后你可以这样继续:

nodes = {} # dictionary of all nodes keyed by their value
root = None
n = int(input()) # get the number of nodes

for linenum in range(1, n+1):
    parentnum = int(input()) # get the "master" for this line
    # create the involved nodes that do not yet exist:
    if not linenum in nodes:
        nodes[linenum] = Node(linenum)
    if not parentnum in nodes:
        nodes[parentnum] = Node(parentnum)
    if parentnum == linenum: # a self-reference indicates the root
        root = nodes[parentnum]
    else: # set the parent-child relationship
        nodes[parentnum].child.append(nodes[linenum])

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

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