Skip to content

Commit

Permalink
😉
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Jul 11, 2024
1 parent 5fd33fd commit 90a44bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
52 changes: 25 additions & 27 deletions solutions/beecrowd/1201/1201.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@

add_space_before_number = True


def _remove_node(tree, number):
if not tree:
return tree
if number < tree.number:
tree.left = _remove_node(tree.left, number)
elif number > tree.number:
tree.right = _remove_node(tree.right, number)
else:
if not tree.left:
return tree.right
elif not tree.right:
return tree.left
else:
successor_parent = tree
successor = tree.right
while successor.left:
successor_parent = successor
successor = successor.left
tree.number = successor.number
if successor_parent.left == successor:
successor_parent.left = successor.right
else:
successor_parent.right = successor.right
return tree
class Node:
def __init__(self, number=None, left=None, right=None):
self.number = number
Expand Down Expand Up @@ -76,32 +99,7 @@ def postfix_print(self):
print()

def remove(self, number):
_remove(self, number)

def _remove(tree, number):
if not tree:
return tree
if number < tree.number:
tree.left = _remove(tree.left, number)
elif number > tree.number:
tree.right = _remove(tree.right, number)
else:
if not tree.left:
return tree.right
elif not tree.right:
return tree.left
else:
successor_parent = tree
successor = tree.right
while successor.left:
successor_parent = successor
successor = successor.left
tree.number = successor.number
if successor_parent.left is successor:
successor_parent.left = successor.right
else:
successor_parent.right = successor.right
return tree
_remove_node(self, number)


def main():
Expand Down
2 changes: 1 addition & 1 deletion solutions/beecrowd/1201/in.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ I 3
P 3
P 2
I 2
p 2
P 2
P 30
P 1
I 1
Expand Down
14 changes: 7 additions & 7 deletions solutions/beecrowd/1201/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@
30 existe
41 nao existe
41 existe
2 3 4 5 6 7 15 16 17 20 20 25 30 35 37 38 39 40 41
20 20 15 5 3 2 4 7 6 16 17 30 25 35 40 39 38 37 41
2 4 3 6 7 5 17 16 15 20 25 37 38 39 41 40 35 30 20
2 3 4 5 6 7 15 16 17 20 20 25 35 37 38 39 40 41
20 15 5 3 2 4 7 6 16 17 25 35 40 39 38 37 41
2 4 3 6 7 5 20 17 16 15 37 38 41 39 35 25 20
2 5 6 15 16 17 20 20 30 35 37 38 39 41
20 20 15 5 2 6 16 17 30 35 41 39 38 37
2 6 5 17 16 15 20 37 38 39 41 35 30 20
2 5 6 15 16 17 20 20 35 37 38 39 41
20 15 5 2 6 16 17 35 41 39 38 37
2 6 5 17 16 15 37 38 39 41 35 20
40 nao existe
40 existe
2 3 4 4 5 5 6 7 7 11 15 15 16 17 20 20 23 24 25 25 28 35 35 37 38 39 40 41 42
2 5 6 11 15 15 16 17 20 23 24 25 28 35 37 38 39 40 41 42

0 comments on commit 90a44bc

Please sign in to comment.