|
4 | 4 | 2015
|
5 | 5 | """
|
6 | 6 |
|
7 |
| - |
8 |
| -class Arvore: |
9 |
| - def __init__(self, chave): |
10 |
| - self.chave = chave |
11 |
| - self.esquerda = None |
12 |
| - self.direita = None |
13 |
| - |
14 |
| - |
15 |
| -# Metodos de Busca |
16 |
| -def busca_recursiva(no, chave): |
17 |
| - if no is None: |
18 |
| - print(f"{chave} nao foi encontrado na arvore") |
| 7 | +class TreeNode: |
| 8 | + def __init__(self, key): |
| 9 | + self.key = key |
| 10 | + self.left = None |
| 11 | + self.right = None |
| 12 | + |
| 13 | +# Search Methods |
| 14 | +def recursive_search(node, key): |
| 15 | + if node is None: |
| 16 | + print(f"{key} was not found in the tree") |
19 | 17 | return
|
20 |
| - if no.chave == chave: |
21 |
| - print(f"{chave} foi encontrado na arvore") |
22 |
| - return no |
23 |
| - if chave > no.chave: |
24 |
| - busca_recursiva(no.direita, chave) |
| 18 | + if node.key == key: |
| 19 | + print(f"{key} was found in the tree") |
| 20 | + return node |
| 21 | + if key > node.key: |
| 22 | + recursive_search(node.right, key) |
25 | 23 | else:
|
26 |
| - busca_recursiva(no.esquerda, chave) |
27 |
| - |
28 |
| - |
29 |
| -def busca_linear(no, chave): |
30 |
| - while no is not None: |
31 |
| - if no.chave == chave: |
32 |
| - return no |
33 |
| - elif chave > no.chave: |
34 |
| - no = no.direita |
| 24 | + recursive_search(node.left, key) |
| 25 | + |
| 26 | +def linear_search(node, key): |
| 27 | + while node is not None: |
| 28 | + if node.key == key: |
| 29 | + return node |
| 30 | + elif key > node.key: |
| 31 | + node = node.right |
35 | 32 | else:
|
36 |
| - no = no.esquerda |
| 33 | + node = node.left |
37 | 34 | return None
|
38 | 35 |
|
39 |
| - |
40 |
| -# Metodo de Insercao |
41 |
| -def insere(no, chave): |
42 |
| - if no is None: |
43 |
| - no = Arvore(chave) |
| 36 | +# Insertion Method |
| 37 | +def insert(node, key): |
| 38 | + if node is None: |
| 39 | + node = TreeNode(key) |
44 | 40 | else:
|
45 |
| - if chave < no.chave: |
46 |
| - no.esquerda = insere(no.esquerda, chave) |
| 41 | + if key < node.key: |
| 42 | + node.left = insert(node.left, key) |
47 | 43 | else:
|
48 |
| - no.direita = insere(no.direita, chave) |
49 |
| - return no |
50 |
| - |
| 44 | + node.right = insert(node.right, key) |
| 45 | + return node |
51 | 46 |
|
52 |
| -# Metodos de Impressao |
53 |
| -IMPRIME_ARVORE = "" |
| 47 | +# Printing Methods |
| 48 | +PRINT_TREE = "" |
54 | 49 |
|
55 |
| - |
56 |
| -def pre_ordem(no): |
57 |
| - global IMPRIME_ARVORE |
58 |
| - if no is None: |
| 50 | +def pre_order(node): |
| 51 | + global PRINT_TREE |
| 52 | + if node is None: |
59 | 53 | return
|
60 |
| - IMPRIME_ARVORE += str(no.chave) + ", " |
61 |
| - pre_ordem(no.esquerda) |
62 |
| - pre_ordem(no.direita) |
63 |
| - |
| 54 | + PRINT_TREE += str(node.key) + ", " |
| 55 | + pre_order(node.left) |
| 56 | + pre_order(node.right) |
64 | 57 |
|
65 |
| -def em_ordem(no): |
66 |
| - global IMPRIME_ARVORE |
67 |
| - if no is None: |
| 58 | +def in_order(node): |
| 59 | + global PRINT_TREE |
| 60 | + if node is None: |
68 | 61 | return
|
69 |
| - em_ordem(no.esquerda) |
70 |
| - IMPRIME_ARVORE += str(no.chave) + ", " |
71 |
| - em_ordem(no.direita) |
| 62 | + in_order(node.left) |
| 63 | + PRINT_TREE += str(node.key) + ", " |
| 64 | + in_order(node.right) |
72 | 65 |
|
73 |
| - |
74 |
| -def pos_ordem(no): |
75 |
| - global IMPRIME_ARVORE |
76 |
| - if no is None: |
| 66 | +def post_order(node): |
| 67 | + global PRINT_TREE |
| 68 | + if node is None: |
77 | 69 | return
|
78 |
| - pos_ordem(no.esquerda) |
79 |
| - pos_ordem(no.direita) |
80 |
| - IMPRIME_ARVORE += str(no.chave) + ", " |
81 |
| - |
| 70 | + post_order(node.left) |
| 71 | + post_order(node.right) |
| 72 | + PRINT_TREE += str(node.key) + ", " |
82 | 73 |
|
83 |
| -# Acha a Altura da Arvore |
84 |
| -def maximo(a, b): |
| 74 | +# Finding the Tree's Height |
| 75 | +def maximum(a, b): |
85 | 76 | if a > b:
|
86 | 77 | return a
|
87 | 78 | return b
|
88 | 79 |
|
89 |
| - |
90 |
| -def altura(no): |
91 |
| - if no is None: |
| 80 | +def tree_height(node): |
| 81 | + if node is None: |
92 | 82 | return 0
|
93 |
| - return 1 + maximo(altura(no.esquerda), altura(no.direita)) |
94 |
| - |
95 |
| - |
96 |
| -# Metodos de Exclusao |
97 |
| -def busca_no_pai(no, ch): |
98 |
| - no_pai = no |
99 |
| - while no is not None: |
100 |
| - if no.chave == ch: |
101 |
| - return no_pai |
102 |
| - no_pai = no |
103 |
| - if no.chave < ch: |
104 |
| - no = no.direita |
| 83 | + return 1 + maximum(tree_height(node.left), tree_height(node.right)) |
| 84 | + |
| 85 | +# Deletion Methods |
| 86 | +def find_parent(node, ch): |
| 87 | + parent_node = node |
| 88 | + while node is not None: |
| 89 | + if node.key == ch: |
| 90 | + return parent_node |
| 91 | + parent_node = node |
| 92 | + if node.key < ch: |
| 93 | + node = node.right |
105 | 94 | else:
|
106 |
| - no = no.esquerda |
107 |
| - return no_pai |
108 |
| - |
109 |
| - |
110 |
| -def maiorAesquerda(no): |
111 |
| - no = no.esquerda |
112 |
| - while no.direita is not None: |
113 |
| - no = no.direita |
114 |
| - return no |
115 |
| - |
116 |
| - |
117 |
| -def exclui(no, ch): |
118 |
| - atual = busca_linear(no, ch) |
119 |
| - if atual is None: |
| 95 | + node = node.left |
| 96 | + return parent_node |
| 97 | + |
| 98 | +def largest_on_left(node): |
| 99 | + node = node.left |
| 100 | + while node.right is not None: |
| 101 | + node = node.right |
| 102 | + return node |
| 103 | + |
| 104 | +def delete(node, ch): |
| 105 | + current = linear_search(node, ch) |
| 106 | + if current is None: |
120 | 107 | return False
|
121 |
| - noPai = busca_no_pai(no, ch) |
122 |
| - if atual.esquerda is None or atual.direita is None: |
123 |
| - if atual.esquerda is None: |
124 |
| - substituto = atual.direita |
| 108 | + parent = find_parent(node, ch) |
| 109 | + if current.left is None or current.right is None: |
| 110 | + if current.left is None: |
| 111 | + substitute = current.right |
125 | 112 | else:
|
126 |
| - substituto = atual.esquerda |
127 |
| - if noPai is None: |
128 |
| - no = substituto |
129 |
| - elif ch > noPai.chave: |
130 |
| - noPai.direita = substituto |
| 113 | + substitute = current.left |
| 114 | + if parent is None: |
| 115 | + node = substitute |
| 116 | + elif ch > parent.key: |
| 117 | + parent.right = substitute |
131 | 118 | else:
|
132 |
| - noPai.esquerda = substituto |
133 |
| - # AQUI DA FREE(ATUAL) |
| 119 | + parent.left = substitute |
| 120 | + # Free(current) |
134 | 121 | else:
|
135 |
| - substituto = maiorAesquerda(atual) |
136 |
| - atual.chave = substituto.chave |
137 |
| - if substituto.esquerda is not None: |
138 |
| - atual.esquerda = substituto.esquerda |
| 122 | + substitute = largest_on_left(current) |
| 123 | + current.key = substitute.key |
| 124 | + if substitute.left is not None: |
| 125 | + current.left = substitute.left |
139 | 126 | else:
|
140 |
| - atual.esquerda = None |
141 |
| - # FREE(substituto) |
| 127 | + current.left = None |
| 128 | + # Free(substitute) |
142 | 129 | return True
|
143 | 130 |
|
144 |
| - |
145 | 131 | if __name__ == "__main__":
|
146 |
| - arvore = Arvore(3) # Cria arvore (raiz) |
147 |
| - # Insere varios valores na arvore |
148 |
| - arvore = insere(arvore, 2) |
149 |
| - arvore = insere(arvore, 1) |
150 |
| - arvore = insere(arvore, 4) |
151 |
| - arvore = insere(arvore, 6) |
152 |
| - arvore = insere(arvore, 8) |
153 |
| - arvore = insere(arvore, 5) |
154 |
| - arvore = insere(arvore, 7) |
155 |
| - arvore = insere(arvore, 0) |
156 |
| - |
157 |
| - busca_recursiva(arvore, 6) # Busca que imprime na propria funcao |
158 |
| - |
159 |
| - if busca_linear(arvore, 6) is not None: # Retorna o NO ou None se nao encontrou |
160 |
| - print("Valor encontrado") |
| 132 | + tree = TreeNode(3) # Create a tree (root) |
| 133 | + # Insert several values into the tree |
| 134 | + tree = insert(tree, 2) |
| 135 | + tree = insert(tree, 1) |
| 136 | + tree = insert(tree, 4) |
| 137 | + tree = insert(tree, 6) |
| 138 | + tree = insert(tree, 8) |
| 139 | + tree = insert(tree, 5) |
| 140 | + tree = insert(tree, 7) |
| 141 | + tree = insert(tree, 0) |
| 142 | + |
| 143 | + recursive_search(tree, 6) # Search that prints within the function |
| 144 | + |
| 145 | + if linear_search(tree, 6) is not None: # Returns the NODE or None if not found |
| 146 | + print("Value found") |
161 | 147 | else:
|
162 |
| - print("Valor nao encontrado") |
163 |
| - |
164 |
| - print(f"Altura: {altura(arvore)}") |
165 |
| - |
166 |
| - # Exclui varios valores |
167 |
| - exclui(arvore, 7) |
168 |
| - exclui(arvore, 5) |
169 |
| - exclui(arvore, 8) |
170 |
| - exclui(arvore, 3) |
171 |
| - |
172 |
| - # Chama os metodos de impressao |
173 |
| - IMPRIME = "" |
174 |
| - pre_ordem(arvore) |
175 |
| - print(f"PreOrdem: {IMPRIME}") |
176 |
| - IMPRIME = "" |
177 |
| - em_ordem(arvore) |
178 |
| - print(f"EmOrdem: {IMPRIME}") |
179 |
| - IMPRIME = "" |
180 |
| - pos_ordem(arvore) |
181 |
| - print(f"PosOrdem: {IMPRIME}") |
182 |
| - |
183 |
| - # Mostra a altura da arvore apos remover os itens |
184 |
| - print(f"Altura: {altura(arvore)}") |
| 148 | + print("Value not found") |
| 149 | + |
| 150 | + print(f"Height: {tree_height(tree)}") |
| 151 | + |
| 152 | + # Delete various values |
| 153 | + delete(tree, 7) |
| 154 | + delete(tree, 5) |
| 155 | + delete(tree, 8) |
| 156 | + delete(tree, 3) |
| 157 | + |
| 158 | + # Call printing methods |
| 159 | + PRINT_TREE = "" |
| 160 | + pre_order(tree) |
| 161 | + print(f"PreOrder: {PRINT_TREE}") |
| 162 | + PRINT_TREE = "" |
| 163 | + in_order(tree) |
| 164 | + print(f"InOrder: {PRINT_TREE}") |
| 165 | + PRINT_TREE = "" |
| 166 | + post_order(tree) |
| 167 | + print(f"PostOrder: {PRINT_TREE}") |
| 168 | + |
| 169 | + # Display the height of the tree after removing items |
| 170 | + print(f"Height: {tree_height(tree)}") |
0 commit comments