davegimo

vanessa_lezione_2

Apr 19th, 2022
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 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.  
  8. def somma_nodi(a):
  9.  
  10.     if a is None:
  11.         return 0
  12.  
  13.     if a.sx is None and a.dx is None:
  14.         return a.val
  15.  
  16.  
  17.     somma = a.val
  18.  
  19.     if a.sx is not None:
  20.         somma += somma_nodi(a.sx)
  21.  
  22.     if a.dx is not None:
  23.         somma += somma_nodi(a.dx)
  24.  
  25.  
  26.     return somma
  27.  
  28.  
  29.  
  30.  
  31. def conta(a):
  32.  
  33.     if a is None:
  34.         return 0
  35.  
  36.     if a.sx is None and a.dx is None:  
  37.         return 1
  38.  
  39.  
  40.     conteggio = 1
  41.  
  42.     if a.sx is not None:
  43.         conteggio += conta(a.sx)
  44.  
  45.     if a.dx is not None:
  46.         conteggio += conta(a.dx)
  47.  
  48.  
  49.     return conteggio
  50.  
  51.  
  52. def somma_foglie(a):
  53.  
  54.     if a is None:
  55.         return 0
  56.  
  57.     if a.sx is None and a.dx is None:
  58.         return a.val
  59.  
  60.     somma = 0
  61.  
  62.     if a.sx is not None:
  63.         somma += somma_foglie(a.sx)
  64.  
  65.     if a.dx is not None:
  66.         somma += somma_foglie(a.dx)
  67.  
  68.  
  69.     return somma
  70.  
  71.  
  72.  
  73. def conta_foglie(a):
  74.  
  75.     if a is None:
  76.         return 0
  77.  
  78.     if a.sx is None and a.dx is None:
  79.  
  80.         return 1
  81.  
  82.     somma = 0
  83.  
  84.     if a.sx is not None:
  85.         somma += conta_foglie(a.sx)
  86.  
  87.     if a.dx is not None:
  88.         somma += conta_foglie(a.dx)
  89.  
  90.  
  91.     return somma
  92.  
  93.  
  94. #Dato un albero A, restituire True se il sott.SX ha più nodi foglia del sott.DX, False altrimenti
  95. def verifica(a):
  96.     conta_foglie_sx = conta_foglie(a.sx)
  97.     conta_foglie_dx = conta_foglie(a.dx)
  98.  
  99.     if conta_foglie_sx > conta_foglie_dx:
  100.         return True
  101.     else:
  102.         return False
  103.  
  104.  
  105. ################################################################################
  106.  
  107.  
  108. a = Albero(1)
  109. b = Albero(2)
  110. c = Albero(3)
  111. d = Albero(7)
  112. e = Albero(8)
  113. f = Albero(11)
  114.  
  115. a.sx = b
  116. a.dx = c
  117.  
  118. b.sx = d
  119. b.dx = e
  120.  
  121. c.dx = f
  122.  
  123. print("Somma nodi = " + str(somma_nodi(a)))
  124. print("Conta nodi = " + str(conta(a)))
  125. print("Somma foglie = " + str(somma_foglie(a)))
  126. print("Conta foglie = " + str(conta_foglie(a)))
  127.  
  128. print(verifica(a))
  129.  
  130.  
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment