davegimo

visite

Mar 25th, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 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.  
  9. a = Albero(4)
  10. b = Albero(1)
  11. c = Albero(2)
  12. d = Albero(6)
  13.  
  14. a.dx = d
  15. a.sx = b
  16. b.dx = c
  17.  
  18.  
  19. def inorder(a,l):
  20.    
  21.     if a.sx != None:
  22.         inorder(a.sx,l)
  23.  
  24.     l.append(a.val)
  25.  
  26.     if a.dx != None:
  27.         inorder(a.dx,l)
  28.  
  29.  
  30.  
  31. def preorder(a):
  32.  
  33.     print(a.val)
  34.  
  35.     if a.sx != None:
  36.         inorder(a.sx)
  37.  
  38.  
  39.     if a.dx != None:
  40.         inorder(a.dx)
  41.  
  42. def postorder(a):
  43.  
  44.     if a.sx != None:
  45.         inorder(a.sx)
  46.  
  47.  
  48.     if a.dx != None:
  49.         inorder(a.dx)
  50.  
  51.     print(a.val)
  52.  
  53. l = []
  54. inorder(a,l)
  55.  
Advertisement
Add Comment
Please, Sign In to add comment