Advertisement
myselffran

Untitled

Jun 28th, 2022
953
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jun 28 14:25:43 2022
  4.  
  5. @author: Wilson Francisco Brito DTS-03
  6. """
  7.  
  8.  
  9. class Node():
  10.    
  11.     def __init__(self, value):
  12.         self.value = value
  13.         self.left  = None
  14.         self.right  = None
  15.        
  16.        
  17.     def set_left(self, left_value):
  18.         self.left = Node(left_value)
  19.         return self
  20.        
  21.     def set_right(self, right_value):
  22.         self.right = Node(right_value)
  23.         return self
  24.        
  25.        
  26. class Arbol:
  27.    
  28.     def __init__(self, root):
  29.         self.root = Node(root)
  30.        
  31.     def value_check(self, pointer, search_value):
  32.         found = False
  33.         if pointer.left != None:
  34.             #print('None')
  35.             if pointer.left.value == search_value:
  36.                 print('Encontrado left')
  37.                 found = True
  38.         else:
  39.             pass
  40.        
  41.         if pointer.right != None:
  42.             if pointer.right.value == search_value:
  43.                 print('Encontrado right')
  44.                 found = True
  45.         else:
  46.             pass
  47.         return found
  48.    
  49.     def sides(self, pointer):
  50.         return [pointer.left, pointer.right]
  51.        
  52.     def search(self, search_value):
  53.        
  54.         if search_value == self.root.value:
  55.             print('Encontrado')
  56.             return
  57.         else:
  58.             pointer = self.root
  59.             succes = False
  60.             self.value_check(pointer, search_value)
  61.            
  62.             sides = self.sides(pointer)
  63.            
  64.            
  65.             # for side in sides:
  66.             #     self.value_check(side, search_value)
  67.            
  68.             # sides = [self.sides(x) for x in sides]
  69.             # sides = [x for xs in sides for x in xs] # Para no tener lista de listas
  70.            
  71.            
  72.             # for side in sides:
  73.             #     self.value_check(side, search_value)
  74.            
  75.             #self.value_check(pointer.left, search_value)
  76.             #self.value_check(pointer.right, search_value)
  77.             #self.value_check(pointer.left.left, search_value)
  78.            
  79.             while succes != True:
  80.                 for side in sides:
  81.                     succes = self.value_check(side, search_value)
  82.                     if succes: break
  83.                
  84.                 if succes == False:
  85.                     sides = [self.sides(x) for x in sides]
  86.                     sides = [x for xs in sides for x in xs]
  87.            
  88.  
  89.        
  90. a = Arbol(0)
  91.  
  92. a.root.set_left(1)
  93. a.root.set_right(2)
  94.  
  95. a.root.left.set_left(3)
  96. a.root.left.set_right(4)
  97.  
  98. a.root.right.set_left(5)
  99. a.root.right.set_right(6)
  100.  
  101. a.root.left.left.set_right(8)
  102.  
  103.  
  104.  
  105. print(a.root.value)
  106. print(a.root.left.value)
  107. print(a.root.left.left.value)
  108. print(a.root.left.right.value)
  109.  
  110. print(a.root.right.value)
  111.  
  112. a.search(8)
Advertisement
RAW Paste Data Copied
Advertisement