Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Albero:
- def __init__(self,val):
- self.val = val
- self.sx = None
- self.dx = None
- #definizione NODI ALBERO
- a = Albero(26)
- b = Albero(1)
- c = Albero(55)
- d = Albero(3)
- e = Albero(6)
- #DEFINIZIONE LEGAME PADRE-FIGLIO
- a.sx = b
- a.dx = c
- b.sx = d
- b.dx = e
- def conta_valori(a):
- if a.sx == None and a.dx == None:
- return a.val
- c = a.val
- if a.sx != None:
- c += conta_valori(a.sx)
- if a.dx != None:
- c += conta_valori(a.dx)
- return c
- def conta_nodi(a):
- if a.sx == None and a.dx == None:
- return 1
- c = 1
- if a.sx != None:
- c += conta_nodi(a.sx)
- if a.dx != None:
- c += conta_nodi(a.dx)
- return c
- numero = conta_nodi(a)
- print(numero)
Advertisement
Add Comment
Please, Sign In to add comment