davegimo

lezione2

Apr 29th, 2022
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. class Albero:
  2.     def __init__(self, v): #costruttore
  3.         self.val = v
  4.         self.sx = None
  5.         self.dx = None
  6.  
  7.  
  8.  
  9. def cerca(a,valore):
  10.  
  11.     if a is None:
  12.         return False
  13.  
  14.     if a.val == valore:
  15.         return True
  16.  
  17.     if a.sx is None and a.dx is None:
  18.         return False
  19.  
  20.    
  21.  
  22.     cerca_sx = False
  23.     cerca_dx = False
  24.  
  25.     if a.sx is not None:
  26.         cerca_sx = cerca(a.sx,valore)
  27.  
  28.     if a.dx is not None:
  29.         cerca_dx = cerca(a.dx,valore)
  30.  
  31.     if cerca_sx is True or cerca_dx is True:
  32.         return True
  33.  
  34.     return False
  35.  
  36.  
  37. #scrivere funzione controlla_somma(A,B) dove
  38. #dati due alberi A e B ritorno true se la somma dei loro nodi coincide
  39. def controlla_somma(A,B):
  40.     somma_A = somma(A)
  41.     somma_B = somma(B)
  42.  
  43.     if somma_A == somma_B:
  44.         return True
  45.  
  46.     return False
  47.  
  48. def somma(a):
  49.     if a is None:
  50.         return 0
  51.  
  52.     if a.sx is None and a.dx is None:
  53.         return a.val
  54.  
  55.     s = a.val
  56.  
  57.     if a.sx is not None:
  58.         s += somma(a.sx)
  59.  
  60.     if a.dx is not None:
  61.         s += somma(a.dx)
  62.  
  63.     return s
  64.  
  65.  
  66. #Dato un albero, definire funzione somma_liv(a,livello)
  67. #sommare tutti i nodi il cui livello รจ liv
  68.  
  69. def somma_liv(a,livello):
  70.     return somma_con_livello(a,livello,0)
  71.  
  72.  
  73. def somma_con_livello(a,livello,livello_corrente):
  74.  
  75.     if a is None:
  76.         return 0
  77.  
  78.     if livello == livello_corrente:
  79.         return a.val
  80.    
  81.     if a.sx is None and a.dx is None:
  82.         return 0
  83.  
  84.     somma = 0
  85.  
  86.     if a.sx is not None:
  87.         somma += somma_con_livello(a.sx,livello,livello_corrente + 1)
  88.  
  89.     if a.dx is not None:
  90.         somma += somma_con_livello(a.dx,livello,livello_corrente + 1)
  91.  
  92.  
  93.     return somma
  94.    
  95.    
  96.  
  97. #Definire la funziona somma_con_livello_pari
  98.  
  99. ###main
  100.  
  101. a = Albero(8)
  102. b = Albero(3)
  103. c = Albero(10)
  104. d = Albero(1)
  105. e = Albero(6)
  106. f = Albero(14)
  107. g = Albero(4)
  108. h = Albero(7)
  109. i = Albero(13)
  110.  
  111.  
  112. a.sx = b
  113. a.dx = c
  114. c.dx = f
  115. b.sx = d
  116. b.dx = e
  117. e.sx = g
  118. e.dx = h
  119. f.sx = i
  120.  
  121. #flag = cerca(a,20)
  122. #print(flag)
  123.  
  124. x = Albero(70)
  125. print(somma_liv(a,3))
  126. #print(controlla_somma(a,x))
  127.  
Advertisement
Add Comment
Please, Sign In to add comment