Skip to content

Commit ddcf3eb

Browse files
Updated issue Python-black
1 parent 96ae52d commit ddcf3eb

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

src/python/arvore_binaria_de_busca.py

+27-41
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def __init__(self, key):
1414
def recursive_search(node, key):
1515
if node is None:
1616
print(f"{key} was not found in the tree")
17-
return
17+
return None
1818
if node.key == key:
1919
print(f"{key} was found in the tree")
2020
return node
2121
if key > node.key:
22-
recursive_search(node.right, key)
22+
return recursive_search(node.right, key)
2323
else:
24-
recursive_search(node.left, key)
24+
return recursive_search(node.left, key)
2525

2626
def linear_search(node, key):
2727
while node is not None:
@@ -36,7 +36,7 @@ def linear_search(node, key):
3636
# Insertion Method
3737
def insert(node, key):
3838
if node is None:
39-
node = TreeNode(key)
39+
return TreeNode(key)
4040
else:
4141
if key < node.key:
4242
node.left = insert(node.left, key)
@@ -45,42 +45,37 @@ def insert(node, key):
4545
return node
4646

4747
# Printing Methods
48-
PRINT_TREE = ""
49-
5048
def pre_order(node):
51-
global PRINT_TREE
5249
if node is None:
53-
return
54-
PRINT_TREE += str(node.key) + ", "
55-
pre_order(node.left)
56-
pre_order(node.right)
50+
return []
51+
result = [node.key]
52+
result.extend(pre_order(node.left))
53+
result.extend(pre_order(node.right))
54+
return result
5755

5856
def in_order(node):
59-
global PRINT_TREE
6057
if node is None:
61-
return
62-
in_order(node.left)
63-
PRINT_TREE += str(node.key) + ", "
64-
in_order(node.right)
58+
return []
59+
result = []
60+
result.extend(in_order(node.left))
61+
result.append(node.key)
62+
result.extend(in_order(node.right))
63+
return result
6564

6665
def post_order(node):
67-
global PRINT_TREE
6866
if node is None:
69-
return
70-
post_order(node.left)
71-
post_order(node.right)
72-
PRINT_TREE += str(node.key) + ", "
67+
return []
68+
result = []
69+
result.extend(post_order(node.left))
70+
result.extend(post_order(node.right))
71+
result.append(node.key)
72+
return result
7373

7474
# Finding the Tree's Height
75-
def maximum(a, b):
76-
if a > b:
77-
return a
78-
return b
79-
8075
def tree_height(node):
8176
if node is None:
8277
return 0
83-
return 1 + maximum(tree_height(node.left), tree_height(node.right))
78+
return 1 + max(tree_height(node.left), tree_height(node.right))
8479

8580
# Deletion Methods
8681
def find_parent(node, ch):
@@ -117,15 +112,13 @@ def delete(node, ch):
117112
parent.right = substitute
118113
else:
119114
parent.left = substitute
120-
# Free(current)
121115
else:
122116
substitute = largest_on_left(current)
123117
current.key = substitute.key
124118
if substitute.left is not None:
125119
current.left = substitute.left
126120
else:
127121
current.left = None
128-
# Free(substitute)
129122
return True
130123

131124
if __name__ == "__main__":
@@ -140,9 +133,8 @@ def delete(node, ch):
140133
tree = insert(tree, 7)
141134
tree = insert(tree, 0)
142135

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
136+
result = recursive_search(tree, 6)
137+
if result is not None:
146138
print("Value found")
147139
else:
148140
print("Value not found")
@@ -156,15 +148,9 @@ def delete(node, ch):
156148
delete(tree, 3)
157149

158150
# 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}")
151+
print(f"PreOrder: {pre_order(tree)}")
152+
print(f"InOrder: {in_order(tree)}")
153+
print(f"PostOrder: {post_order(tree)}")
168154

169155
# Display the height of the tree after removing items
170156
print(f"Height: {tree_height(tree)}")

0 commit comments

Comments
 (0)