【问题标题】:error in updating weight in a tree from leaf to root将树中的权重从叶子更新到根时出错
【发布时间】:2015-08-21 11:32:28
【问题描述】:

我有一棵 json 树

{"reply": 0, "id": 1, "children": [{"reply": 1, "id": 2, "children": [{"reply": 1, "id": 3, "children": [{"reply": 1, "id": 4, "children": []}]}]}, {"reply": 0, "id": 5, "children": []}]}

我必须分配从叶子到根的权重:

  1. 所有叶节点的权重为 0.1

  2. 如果一个节点有一个孩子:那么该节点的权重变为0.1*其单个孩子的权重

  3. 如果一个节点有两个或多个子节点,那么该节点的权重是:

    其child1的重量*child2的重量*....childn*0.1*0.2

    例如输出是节点的最终权重:

    4: 0.1, 5: 0.1, 3: 0.1*0.1, 2: (0.1*0.1*0.1) 1: 0.1*0.2(0.1*0.1*0.1*0.1)`

我正在使用 python 中的代码

我在更新权重时遇到 keyerror 2:

Traceback 例外(最近一次调用最后一次): 文件“cc1.py”,第 42 行,在 重量[par]=重量[par]*weight[child_of[par][i]] 关键错误:2

我有三个字典:

import json
from collections import deque
def generate_children(tree):
    queue = deque()
    queue.append((tree, None))
    while queue:
       node, parent = queue.pop()
       children = []
    for child in node['children']:
        queue.append((child, node['id']))
        children.append(child['id'])
    parent_of[node['id']]=parent
    child_of[node['id']]=children
    no_child_of[node['id']]=len(child_of[node['id']])
    yield node['id'], parent, children

f=open('tree_json','r')
for line in f:
    tree=json.loads(line)
    parent_of={}
    child_of={}
    no_child_of={}
    weight={}
    q = list(generate_children(tree))
    #assigning weights to leaf
    for c in no_child_of.keys():
        if no_child_of[c]==0:
            weight[c]=0.1
    # assigning weight to the parent
    for w in weight.keys():
        par=parent_of[w]
        n=len(child_of[par])
        if n > 1:
            weight[par]=0.2
            for i in range(n):
                weight[par]=weight[par]*weight[child_of[par][i]]

【问题讨论】:

  • 您能否编辑您的问题以包含nodeparentchildren 的最小示例,这将使我们能够重新创建您的密钥错误。
  • @Martin Evans:当它试图更新根的权重并且它的一个孩子的权重未知时,问题就出现了。请帮忙,我卡住了
  • Martin 要求您提供minimal, complete and verifiable example。到目前为止,您提供的代码不完整(您没有显示 nodeparentchildren 实际上 ),这意味着我们无法运行它来重现您的问题
  • 哦,还有 - 当你“得到一个keyerror”时,一个异常被抛出,你应该看到一个带有行号的堆栈跟踪。请显示异常和堆栈跟踪,如果不清楚,请指出代码中与行号对应的行。
  • @Martin Evans:给出带有输入的完整代码。请帮忙

标签: python json algorithm tree


【解决方案1】:

我认为在您的代码中,您正在尝试访问尚未分配的节点的权重。

检查以下伪代码。希望它能解决你的目的。 我假设你知道根元素。

for each node in tree
    weight[node] = 0;

current_node = root;
while(weight[root] = 0){
   if(no_child_of(current_node) = 0){
       weight[current_node] = 0.1;
       current_node = parent_of[current_node];
   }else{
      boolean all_child_weighted = true;
      total_weight = 1;
      for each (child[i] of current_node){
         if(weight[child_of[current_node][i]] = 0){
             all_child_weighted = false;
             current_node = child_of[current_node][i];  
             break; 
         }else{
             total_weight = total_weight * weight[child_of[current_node][i]];
         } 
      }   
      if(all_child_weighted){
          if(no_child_of(current_node) = 1){
              weight[current_node] = 0.1*weight[child_of[current_node][1]];
          }else{
              weight[current_node] = total_weight * 0.1 * 0.2;
          }
          current_node = parent_of(current_node);
      }
   }
}

【讨论】:

  • @Somabatra: root 可以有任意数量的孩子。在您给出的伪代码中,root 似乎只有两个孩子
  • 如果根有 n 个子节点,请编辑。这考虑了两个孩子的根
  • 谢谢。将尝试代码并让您知道。再次感谢
  • 这不起作用,因为它从不为叶节点分配权重。在第一个 If 条件中,它检查根的数量是否为零。
猜你喜欢
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
相关资源
最近更新 更多