-
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.
Merge pull request #8 from m4tveevm/dev
Completing last part of DataStructure task with RB-Tree model
- Loading branch information
Showing
28 changed files
with
863 additions
and
37 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
[tool.black] | ||
line-length = 79 | ||
target-version = ['py37'] | ||
include = '\.pyi?$' | ||
exclude = ''' | ||
/( | ||
.git | ||
| __pycache__ | ||
| docs/source/conf.py | ||
| old | ||
| build | ||
| dist | ||
| .venv | ||
)/ | ||
''' |
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,8 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup( | ||
name="algo_labs", | ||
version="0.1.0", | ||
packages=find_packages(where="src"), | ||
package_dir={"": "src"}, | ||
) |
File renamed without changes.
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
Empty file.
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
File renamed without changes.
File renamed without changes.
Empty file.
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
Empty file.
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,38 @@ | ||
from .traversal import TraversalStrategy | ||
|
||
|
||
class BinaryTree: | ||
def __init__(self, root=None): | ||
self.root = root | ||
|
||
def traverse(self, strategy: TraversalStrategy): | ||
return strategy.traverse(self.root) | ||
|
||
def delete(self, value): | ||
self.root = self._delete_rec(self.root, value) | ||
|
||
def _delete_rec(self, node, value): | ||
if node is None: | ||
return None | ||
if value < node.get_value(): | ||
node.set_left(self._delete_rec(node.get_left(), value)) | ||
elif value > node.get_value(): | ||
node.set_right(self._delete_rec(node.get_right(), value)) | ||
else: | ||
if node.get_left() is None: | ||
return node.get_right() | ||
elif node.get_right() is None: | ||
return node.get_left() | ||
temp = self._min_value_node(node.get_right()) | ||
node.set_value(temp.get_value()) | ||
node.set_right( | ||
self._delete_rec(node.get_right(), temp.get_value()) | ||
) | ||
return node | ||
|
||
@staticmethod | ||
def _min_value_node(node): | ||
current = node | ||
while current.get_left() is not None: | ||
current = current.get_left() | ||
return current |
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,63 @@ | ||
from src.tree.binary_tree import BinaryTree | ||
from src.tree.parser import TreeParser | ||
from src.tree.red_black_tree import RedBlackTree | ||
from src.tree.traversal import ( | ||
BreadthFirstTraversal, | ||
InOrderTraversal, | ||
PostOrderTraversal, | ||
PreOrderTraversal, | ||
) | ||
|
||
|
||
def read_tree_from_file(file_name): | ||
with open(file_name, "r") as file: | ||
tree_string = file.read().strip() | ||
parser = TreeParser(tree_string) | ||
root = parser.parse() | ||
return root | ||
|
||
|
||
def build_red_black_tree(bin_root): | ||
tree = RedBlackTree() | ||
|
||
def insert_node(node): | ||
if node: | ||
try: | ||
value = int(node.get_value()) | ||
except ValueError: | ||
value = node.get_value() | ||
tree.insert(value) | ||
insert_node(node.get_left()) | ||
insert_node(node.get_right()) | ||
|
||
insert_node(bin_root) | ||
return tree | ||
|
||
|
||
def demonstrate_traversals(tree): | ||
strategies = { | ||
"Обход в ширину": BreadthFirstTraversal(), | ||
"Обход в глубину (прямой)": PreOrderTraversal(), | ||
"Обход в глубину (симметричный)": InOrderTraversal(), | ||
"Обход в глубину (обратный)": PostOrderTraversal(), | ||
} | ||
|
||
for name, strategy in strategies.items(): | ||
result = tree.traverse(strategy) | ||
print(f"{name}: {' '.join(map(str, result))}") | ||
|
||
|
||
if __name__ == "__main__": | ||
filename = "tree.txt" | ||
binary_root = read_tree_from_file(filename) | ||
if not binary_root: | ||
print("Не удалось построить дерево.") | ||
exit(1) | ||
binary_tree = BinaryTree(binary_root) | ||
print("Построено двоичное дерево.") | ||
demonstrate_traversals(binary_tree) | ||
|
||
print("\nПостроение красно-черного дерева из двоичного дерева...") | ||
rb_tree = build_red_black_tree(binary_root) | ||
print("Вывод узлов красно-черного дерева:") | ||
demonstrate_traversals(rb_tree) |
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,52 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class TreeNode(ABC): | ||
@abstractmethod | ||
def get_value(self): | ||
pass | ||
|
||
@abstractmethod | ||
def set_value(self, value): | ||
pass | ||
|
||
@abstractmethod | ||
def get_left(self): | ||
pass | ||
|
||
@abstractmethod | ||
def get_right(self): | ||
pass | ||
|
||
@abstractmethod | ||
def set_left(self, node): | ||
pass | ||
|
||
@abstractmethod | ||
def set_right(self, node): | ||
pass | ||
|
||
|
||
class BinaryTreeNode(TreeNode): | ||
def __init__(self, value): | ||
self._value = value | ||
self._left = None | ||
self._right = None | ||
|
||
def get_value(self): | ||
return self._value | ||
|
||
def set_value(self, value): | ||
self._value = value | ||
|
||
def get_left(self): | ||
return self._left | ||
|
||
def get_right(self): | ||
return self._right | ||
|
||
def set_left(self, node): | ||
self._left = node | ||
|
||
def set_right(self, node): | ||
self._right = node |
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,34 @@ | ||
from .nodes import BinaryTreeNode | ||
|
||
|
||
class TreeParser: | ||
def __init__(self, line): | ||
self.tokens = line.replace("(", " ( ").replace(")", " ) ").split() | ||
self.index = 0 | ||
|
||
def parse(self): | ||
if self.index >= len(self.tokens): | ||
return None | ||
|
||
token = self.tokens[self.index] | ||
if token == "(": | ||
self.index += 1 | ||
if self.index >= len(self.tokens): | ||
raise Exception("Ожидалось значение после '('") | ||
value = self.tokens[self.index] | ||
self.index += 1 | ||
node = BinaryTreeNode(value) | ||
node.set_left(self.parse()) | ||
node.set_right(self.parse()) | ||
if ( | ||
self.index >= len(self.tokens) | ||
or self.tokens[self.index] != ")" | ||
): | ||
raise Exception("Ожидалась ')'") | ||
self.index += 1 | ||
return node | ||
elif token == ")": | ||
return None | ||
else: | ||
self.index += 1 | ||
return BinaryTreeNode(token) |
Oops, something went wrong.