-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0ba728c
Showing
70 changed files
with
4,782 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Arthur Prado De Fazio | ||
# NUSP: 9298614 | ||
# Esta implementação foi fortemente baseada no pseudo-código disponível | ||
# na tese de mestrado de Yan Couto | ||
|
||
class Node: | ||
def __init__(self, parent=None, depth=0, val=None): | ||
self.parent = parent | ||
self.depth = depth | ||
self.val = val | ||
if depth == 0: # é raiz | ||
self.jump = self | ||
else: | ||
self.add_leaf() | ||
|
||
def add_leaf(self): | ||
parent = self.parent | ||
if parent.jump.depth != 0 and parent.depth - parent.jump.depth == parent.jump.depth - parent.jump.jump.depth: | ||
self.jump = parent.jump.jump | ||
else: | ||
self.jump = parent | ||
|
||
def level_ancestor(self, ancestor): | ||
ancestor_depth = self.depth - ancestor | ||
while self.depth != ancestor_depth: | ||
if self.jump.depth >= ancestor_depth: | ||
self = self.jump | ||
else: | ||
self = self.parent | ||
return self | ||
|
||
def lowest_common_ancestor(self, relative): | ||
if self.depth > relative.depth: | ||
self, relative = relative, self | ||
relative = relative.level_ancestor(relative.depth - self.depth) | ||
if self is relative: | ||
return self | ||
while self.parent is not relative.parent: | ||
if self.jump is not relative.jump: | ||
self = self.jump | ||
relative = relative.jump | ||
else: | ||
self = self.parent | ||
relative = relative.parent | ||
return self.parent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Arthur Prado De Fazio | ||
# NUSP: 9298614 | ||
# Esta implementação foi fortemente baseada no pseudo-código disponível | ||
# na tese de mestrado de Yan Couto | ||
|
||
import Node as nd | ||
|
||
class Deque: | ||
def __init__(self, first=None, last=None): | ||
if first is None: | ||
first = nd.Node() | ||
last = first | ||
self.first = first | ||
self.last = last | ||
|
||
def swap(self): | ||
return Deque(self.last, self.first) | ||
|
||
def push_front(self, val): | ||
node = nd.Node(self.first, self.first.depth + 1, val) | ||
if self.first.depth == 0: | ||
return Deque(node, node) | ||
return Deque(node, self.last) | ||
|
||
def push_back(self, val): | ||
return self.swap().push_front(val).swap() | ||
|
||
def pop_front(self): | ||
if self.first is self.last: | ||
return Deque() | ||
elif self.first.lowest_common_ancestor(self.last) is self.first: | ||
return Deque(self.last.level_ancestor(self.last.depth - self.first.depth - 1), self.last) | ||
return Deque(self.first.parent, self.last) | ||
|
||
def pop_back(self): | ||
return self.swap().pop_front().swap() | ||
|
||
def front(self): | ||
return self.first.val | ||
|
||
def back(self): | ||
return self.last.val | ||
|
||
def kth(self, k): | ||
if self.first.val is None: | ||
print("Erro! Deque vazia!") | ||
return | ||
mid = self.first.lowest_common_ancestor(self.last) | ||
l1 = self.first.depth - mid.depth | ||
l2 = self.last.depth - mid.depth | ||
if k <= 0 or k - 1 > l1 + l2: | ||
print("Erro, não há", k, "-ésimo elemento") | ||
return | ||
if k - 1 <= l1: | ||
return self.first.level_ancestor(k - 1).val | ||
return self.last.level_ancestor(l2 + l1 + 1 - k).val | ||
|
||
def print(self): | ||
if self.first.depth == 0: | ||
print("\n", end = '') | ||
return | ||
mid = self.first.lowest_common_ancestor(self.last) | ||
node = self.first | ||
while node is not mid: | ||
print(node.val, end = ' ') | ||
node = node.parent | ||
node = self.last | ||
second_half = [] | ||
while node is not mid: | ||
second_half.append(node.val) | ||
node = node.parent | ||
second_half.append(mid.val) | ||
for val in reversed(second_half): | ||
print(val, end = ' ') | ||
print("\n", end = '') |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Arthur Prado De Fazio | ||
# NUSP: 9298614 | ||
# Esta implementação foi fortemente baseada no pseudo-código disponível | ||
# na tese de mestrado de Yan Couto | ||
|
||
import PersistentDeque as pd | ||
import sys | ||
|
||
def main(): | ||
l = sys.stdin.readline() | ||
while len(l) > 0: | ||
if l.find('Deque') != -1: | ||
part1 = l[:l.find('Deque')] | ||
part2 = l[l.find('Deque'):] | ||
l = part1 + 'pd.' + part2 | ||
print(">>>", l, end = '') | ||
exec(l) | ||
l = sys.stdin.readline() | ||
|
||
def command_finder(l, parentheses_index): | ||
i = parentheses_index | ||
while i >= 0: | ||
if l[i] == '.': | ||
break | ||
i -= 1 | ||
if i == -1: | ||
return l[0:parentheses_index] | ||
return l[i:parentheses_index] | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
p0 = Deque() | ||
p1 = p0.push_front(1) | ||
p1.print() | ||
p1.kth(0) | ||
print(p1.kth(1)) | ||
p1.kth(2) | ||
p2 = p1.push_back(2) | ||
p2.print() | ||
print(p2.front()) | ||
print(p2.back()) | ||
p3 = p2.push_front(3) | ||
p3.print() | ||
print(p3.kth(1)) | ||
print(p3.kth(2)) | ||
print(p3.kth(3)) | ||
p3.kth(4) | ||
p1.print() # p1 segue inalterada | ||
p2.print() # p2 segue inalterada | ||
p4 = p3.push_front('-1') | ||
p4.print() | ||
p5 = p4.push_back('oi') | ||
p5.print() | ||
print(p5.front()) | ||
print(p5.back()) | ||
print(p5.kth(-1)) | ||
print(p5.kth(1)) | ||
print(p5.kth(2)) | ||
print(p5.kth(3)) | ||
print(p5.kth(4)) | ||
print(p5.kth(5)) | ||
p6 = p4.push_back('olá') | ||
p6.print() | ||
p7 = p6.push_back('hello') | ||
p7.print() | ||
print(p7.kth(5)) | ||
print(p7.back()) | ||
print(p7.kth(6)) | ||
p4.print() # p4 segue inalterada | ||
p5.print() # p5 também | ||
p6.print() # p6 também | ||
p8 = p1.pop_back() | ||
p8.print() | ||
p7.print() | ||
p9 = p7.pop_front() | ||
p9.print() | ||
p10 = p9.pop_front() | ||
p10.print() | ||
print(p10.kth(1)) | ||
print(p10.kth(2)) | ||
print(p10.kth(3)) | ||
print(p10.kth(4)) | ||
print(p10.kth(5)) | ||
print(p10.front()) | ||
print(p10.back()) | ||
p11 = p10.pop_front() | ||
p11.print() | ||
print(p11.front()) | ||
print(p11.back()) | ||
print(p11.kth(1)) | ||
print(p11.kth(2)) | ||
print(p11.kth(3)) | ||
p12 = p11.pop_back() | ||
p12.print() | ||
p7.print() # p7 segue igual | ||
p13 = p7.pop_back() | ||
p13.print() | ||
p14 = p13.pop_back() | ||
p14.print() | ||
p15 = p14.pop_back() | ||
p15.print() | ||
print(p15.kth(1)) | ||
print(p15.kth(2)) | ||
print(p15.kth(3)) | ||
p16 = p15.push_front("bonjour") | ||
p17 = p16.push_front("hallo") | ||
p17.print() | ||
p18 = p17.pop_back() | ||
p18.print() | ||
print(p18.kth(1)) | ||
print(p18.kth(2)) | ||
print(p18.kth(3)) | ||
p19 = p18.pop_back() | ||
p19.print() | ||
p20 = p19.pop_back() | ||
p20.print() | ||
print(p20.kth(1)) | ||
print(p20.kth(2)) | ||
p21 = p20.pop_front() | ||
p21.print() | ||
p22 = p21.pop_front() | ||
p22.print() | ||
print(p22.front()) | ||
print(p22.back()) | ||
print(p22.kth(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
d0 = Deque() | ||
d1 = d0.push_front(1) | ||
print(d1.kth(1)) | ||
d2 = d1.push_front(2) | ||
d3 = d2.push_front(3) | ||
d4 = d3.pop_back() | ||
d5 = d4.push_back(4) | ||
print(d5.kth(3)) | ||
d6 = d2.push_back(5) | ||
d7 = d6.push_back(6) | ||
d8 = d7.push_back(7) | ||
d8.print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
class Node: | ||
def __init__(self, key=None, left=None, right=None): | ||
self.key = key | ||
self.left = left | ||
self.right = right | ||
|
||
def node_copy(self): | ||
return Node(self.key, self.left, self.right) | ||
|
||
class BST: | ||
def __init__(self): | ||
self.root = None | ||
|
||
def search(self, x): | ||
return self._search(self.root, x) | ||
|
||
def _search(self, node, x): | ||
if node is None: | ||
return False | ||
if node.key == x: | ||
return True | ||
if x < node.key: | ||
return self._search(node.left, x) | ||
return self._search(node.right, x) | ||
|
||
def insert(self, x): | ||
if self.search(x): | ||
return self | ||
new_tree = BST() | ||
new_tree.root = new_tree._insert(self.root, x) | ||
return new_tree | ||
|
||
def _insert(self, node, x): | ||
if node is None: | ||
return Node(x, None, None) | ||
new_node = node.node_copy() | ||
if x < node.key: | ||
new_node.left = self._insert(node.left, x) | ||
else: | ||
new_node.right = self._insert(node.right, x) | ||
return new_node | ||
|
||
def min(self): | ||
if self.root is None: | ||
return None | ||
return self._min(self.root) | ||
|
||
def _min(self, node): | ||
if node.left is None: | ||
return node.key | ||
return self._min(node.left) | ||
|
||
def delete_min(self): | ||
if self.root is None: | ||
return self | ||
new_tree = BST() | ||
new_tree.root = new_tree._delete_min(self.root) | ||
return new_tree | ||
|
||
def _delete_min(self, node): | ||
if node.left is None: | ||
return node.right | ||
new_node = node.node_copy() | ||
new_node.left = self._delete_min(node.left) | ||
return new_node | ||
|
||
def delete(self, x): | ||
if not self.search(x): | ||
return self | ||
new_tree = BST() | ||
new_tree.root = new_tree._delete(self.root, x) | ||
return new_tree | ||
|
||
def _delete(self, node, x): | ||
if node.key == x: | ||
if node.right is None: | ||
return node.left | ||
return Node(self._min(node.right), node.left, | ||
self._delete_min(node.right)) | ||
|
||
new_node = node.node_copy() | ||
if x < node.key: | ||
new_node.left = self._delete(node.left, x) | ||
else: | ||
new_node.right = self._delete(node.right, x) | ||
return new_node | ||
|
||
def print(self): | ||
if self.root is None: | ||
print() | ||
return | ||
self._print(self.root, 0) | ||
|
||
# A função abaixo foi estudada e obtida em https://www.geeksforgeeks.org/print-binary-tree-2-dimensions/ | ||
def _print(self, node, space): | ||
if node is None: | ||
return | ||
space += 10 | ||
self._print(node.right, space) | ||
print() | ||
for i in range(10, space): | ||
print(end=' ') | ||
print(node.key) | ||
self._print(node.left, space) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import ABB as abb | ||
import sys | ||
|
||
def main(): | ||
l = sys.stdin.readline() | ||
while len(l) > 0: | ||
print(">>>", l, end = '') | ||
index_abb = l.find("BST") | ||
if index_abb != -1: | ||
exec(l[:index_abb] + "abb." + l[index_abb:]) | ||
else: | ||
exec(l) | ||
l = sys.stdin.readline() | ||
|
||
main() |
Oops, something went wrong.