-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_print_tree.py
More file actions
42 lines (37 loc) · 1.17 KB
/
example_print_tree.py
File metadata and controls
42 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python3
#-*- coding: utf-8 -*-
class Wezel:
potomkowie = None
atrybut = None
wartosc = None
klasa = None
def wyswietlDrzewo(drzewo,przesuniecie):
if drzewo.atrybut!=None:
print(" "*przesuniecie,end="")
if drzewo.wartosc: print(drzewo.wartosc,end=" -> ")
print("Atrybut",drzewo.atrybut)
for p in drzewo.potomkowie:
wyswietlDrzewo(p,przesuniecie+4)
else:
print(" "*przesuniecie,end="")
print(drzewo.wartosc, "->" ,drzewo.klasa)
def buduj(test):
w = Wezel()
w.potomkowie = []
klucz, element = test.popitem()
w.atrybut = klucz
for t in element:
if isinstance(element[t], (dict)):
nowyWezel=buduj(element[t])
nowyWezel.wartosc = t
w.potomkowie.append(nowyWezel)
else:
nowyWezel = Wezel()
nowyWezel.wartosc = t
nowyWezel.klasa = element[t]
w.potomkowie.append(nowyWezel)
return w
test = {1:{"n":"tak", "m":{2:{"a":"nie", "b":"tak"}}, "o":"nie"}} #testowe dane
# test = {1: {'old': 'down', 'mid': {2: {'yes': 'down', 'no': 'up'}}, 'new': 'up'}}
w = buduj(test)
wyswietlDrzewo(w,0)