Finding items for each level of a binary tree in python

What I'm trying to do is get each level of an element.

I am currently using this code:

re = {0: [b.keys()[0]]}
n = 1
for a in b:
    l = []
    for e in s[a]:
        l.append(e)
    if l != []:
        re.update({n: l})
    n += 1
print re

This gives me the following result:

{"0": ["a"], "1": ["b"], "2": ["i"], "3": ["c", "d"], "4": ["f", "g", "h"], "5": ["e", "l"]}

What am I doing wrong here?

+4
source share
3 answers

There are some problems here:

  • you use [s.keys()[0]]as a way to find the "root" of the tree. However, note that the dictionaries are unordered (yes, CPython-3.6 dictionaries use the insertion order, but this is considered an implementation detail), so there is no guarantee that it [s.keys()[0]]is actually the root; and
  • you do a single pass through the dictionary, but again, since the dictionaries are disordered, it is not guaranteed that the parent will be assigned already.

, . , , . :

nonroots = {c for cs in s.values() for c in cs}

, , , , , :

roots = [k for k in s if k not in nonroots]

0, :

levels[0] = roots

:

, , .

next_gen = [c for k in prev_gen for c in s.get(k,())]

, : , . , :

nonroots = {c for cs in s.values() for c in cs}
level = [k for k in s if k not in nonroots]
generation = 0
result = {}
while level:
    result[generation] = level
    generation += 1
    level = [c for k in level for c in s.get(k,())]

, result - , .

, , 0, , i, i-1 ( i > 0), , :

nonroots = {c for cs in s.values() for c in cs}
level = [k for k in s if k not in nonroots]
result = []
while level:
    result.append(level)
    level = [c for k in level for c in s.get(k,())]

:

>>> result
[['a'], ['b'], ['c', 'd'], ['i', 'e', 'l'], ['f', 'g', 'h']]

()

, (), . , :

def get_levels(tree, roots):
    result = []
    roots = list(roots)
    while roots:
        result.append(roots)
        roots = [c for k in roots for c in tree.get(k,())]
    return result

. , (s) :

>>> get_levels(s, ['a'])
[['a'], ['b'], ['c', 'd'], ['i', 'e', 'l'], ['f', 'g', 'h']]
>>> get_levels(s, ['c'])
[['c'], ['i']]
>>> get_levels(s, ['i','l'])
[['i', 'l']]
>>> get_levels(s, ['i','e'])
[['i', 'e'], ['f', 'g', 'h']]
+4

:

from collections import defaultdict
listing1 = defaultdict(list)
s = {"a": ["b"], "b": ["c", "d"], "c": ["i"], "d": ["e", "l"], "e": ["f", "g", "h"], "f": [], "g": [], "h": [], "i": [], "l": []}
def get_listing(current_node, count):
    global listing1 
    listing1[count].append(current_node)
    for i in s[current_node]:
        get_listing(i, count+1)

get_listing('a', 0)
print(dict(listing1))

:

{0: ['a'], 1: ['b'], 2: ['c', 'd'], 3: ['i', 'e', 'l'], 4: ['f', 'g', 'h']}

, , @WillemVanOnsem @quamrana, dict.keys() , {0: [s.keys()[0]]} root.

+2

, "d".

d = {"a": ["b"], "b": ["c", "d"], "c": ["i"], "d": ["e", "l"], "e": ["f", "g", "h"], "f": [], "g": [], "h": [], "i": [], "l": []}
keys=d.keys()
keys.sort()
def getKey(l):
  for key,value in d.items():
    if l == value:
      return key

val=dict()# to store level of each node
val['a']=0
for l in keys:
  for v in d.values():
    if l in v:
     key = getKey(v)
     val[l]=int(val.get(key,0))+1
     print(l,val[l])
0

Source: https://habr.com/ru/post/1689908/


All Articles