davegimo

sommaalbero

Mar 24th, 2022
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. class Albero:
  2.     def __init__(self,val):
  3.         self.val = val
  4.         self.sx = None
  5.         self.dx = None
  6.  
  7. #definizione NODI ALBERO
  8. a = Albero(26)
  9. b = Albero(1)
  10. c = Albero(55)
  11. d = Albero(3)
  12. e = Albero(6)
  13.  
  14. #DEFINIZIONE LEGAME PADRE-FIGLIO
  15. a.sx = b
  16. a.dx = c
  17.  
  18. b.sx = d
  19. b.dx = e
  20.  
  21. def conta_valori(a):
  22.  
  23.     if a.sx == None and a.dx == None:
  24.         return a.val
  25.  
  26.  
  27.     c = a.val
  28.  
  29.     if a.sx != None:
  30.         c += conta_valori(a.sx)
  31.  
  32.  
  33.     if a.dx != None:
  34.         c += conta_valori(a.dx)
  35.  
  36.  
  37.     return c
  38.  
  39. def conta_nodi(a):
  40.  
  41.     if a.sx == None and a.dx == None:
  42.         return 1
  43.  
  44.  
  45.     c = 1
  46.  
  47.     if a.sx != None:
  48.         c += conta_nodi(a.sx)
  49.  
  50.  
  51.     if a.dx != None:
  52.         c += conta_nodi(a.dx)
  53.  
  54.  
  55.     return c
  56.  
  57.  
  58.  
  59. numero = conta_nodi(a)
  60. print(numero)
  61.  
Advertisement
Add Comment
Please, Sign In to add comment