binary-tree-yaml 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: binary_tree_yaml
3
+ Version: 1.0.0
4
+ Summary: A Binary Tree package with YAML integration
5
+ Author: Shlok
6
+ Requires-Python: >=3.6
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: pyyaml
9
+
10
+ # Binary Tree YAML Package
11
+
12
+ This is a Python package for creating Binary Trees and parsing YAML files and generating a tree from the information in the YAML file.
13
+
14
+ ## Features
15
+ * **Binary Tree Operations**: Create trees, add/delete/edit nodes, and print tree structures.
16
+ * **YAML Integration**: Build trees from YAML files and export tree data to YAML.
17
+
18
+
19
+ ### Prerequisites
20
+ * Python 3.7+
21
+ * pip (must be installed in the system)
22
+
23
+ ## Installation
24
+ ### Installation Steps (via pip from PyPI)
25
+ 1. Go to cmd or terminal.
26
+ 2. Install the package using pip from PyPI:
27
+ `pip install binary_tree_yaml`
28
+
29
+
30
+
31
+
32
+ ### Installation Steps (Manual Install from the package using pip)
33
+ 1. Navigate to the `Task_1` directory.
34
+ 2. Install the package using pip. The `pyproject.toml` file handles the configuration automatically:
35
+ `pip install .`
@@ -0,0 +1,26 @@
1
+ # Binary Tree YAML Package
2
+
3
+ This is a Python package for creating Binary Trees and parsing YAML files and generating a tree from the information in the YAML file.
4
+
5
+ ## Features
6
+ * **Binary Tree Operations**: Create trees, add/delete/edit nodes, and print tree structures.
7
+ * **YAML Integration**: Build trees from YAML files and export tree data to YAML.
8
+
9
+
10
+ ### Prerequisites
11
+ * Python 3.7+
12
+ * pip (must be installed in the system)
13
+
14
+ ## Installation
15
+ ### Installation Steps (via pip from PyPI)
16
+ 1. Go to cmd or terminal.
17
+ 2. Install the package using pip from PyPI:
18
+ `pip install binary_tree_yaml`
19
+
20
+
21
+
22
+
23
+ ### Installation Steps (Manual Install from the package using pip)
24
+ 1. Navigate to the `Task_1` directory.
25
+ 2. Install the package using pip. The `pyproject.toml` file handles the configuration automatically:
26
+ `pip install .`
@@ -0,0 +1,2 @@
1
+ from .node import Node, print_tree, add_node_by_path, delete_node,create_tree
2
+ from .yaml_utils import build_tree_from_yaml, tree_to_yaml
@@ -0,0 +1,98 @@
1
+ class Node:
2
+ def __init__(self, value):
3
+ self.value = value
4
+ self.left = None
5
+ self.right = None
6
+
7
+ def __repr__(self):
8
+ return f"Node({self.value})"
9
+
10
+ # Defining the required helper functions
11
+
12
+ def create_tree(root):
13
+ '''Creates a new binary tree by initializing the root node.'''
14
+ return Node(root)
15
+
16
+ def print_tree(node, level=0, prefix="Root:"):
17
+ """Prints the tree visually as shown in sample output."""
18
+ if node is not None:
19
+ print(" " * (level * 4) + f"{prefix}{node.value}")
20
+ if node.left or node.right:
21
+ # Using specific prefixes for Left Child (L---) and Right Child (R---)
22
+ print_tree(node.left, level + 1, "L---")
23
+ print_tree(node.right, level + 1, "R---")
24
+
25
+ def add_node_by_path(root, path, value):
26
+ """Adds a node following a path 'L', 'R', 'LL' ,etc."""
27
+ current = root
28
+ for i, char in enumerate(path):
29
+ is_last = (i == len(path) - 1)
30
+
31
+ if char.upper() == 'L':
32
+ if is_last:
33
+ current.left = Node(value)
34
+ elif current.left:
35
+ current = current.left
36
+ else:
37
+ # Create intermediate node if missing (optional behavior)
38
+ print(f"Path broken at {char}, creating intermediate node.")
39
+ current.left = Node(None)
40
+ current = current.left
41
+
42
+ elif char.upper() == 'R':
43
+ if is_last:
44
+ current.right = Node(value)
45
+ elif current.right:
46
+ current = current.right
47
+ else:
48
+ current.right = Node(None)
49
+ current = current.right
50
+
51
+ def delete_node(root, key):
52
+ """Deletes a node with a specific value."""
53
+ if not root:
54
+ return root
55
+
56
+ if key < root.value:
57
+ root.left = delete_node(root.left, key)
58
+ elif key > root.value:
59
+ root.right = delete_node(root.right, key)
60
+ else:
61
+ # Node with only one child or no child
62
+ if root.left is None:
63
+ return root.right
64
+ elif root.right is None:
65
+ return root.left
66
+
67
+ # Node with two children: Get the inorder successor
68
+ temp = _min_value_node(root.right)
69
+ root.value = temp.value
70
+ root.right = delete_node(root.right, temp.value)
71
+
72
+ return root
73
+
74
+ def _min_value_node(node):
75
+ current = node
76
+ while current.left is not None:
77
+ current = current.left
78
+ return current
79
+
80
+ def edit_node_value(root, old_value, new_value):
81
+ """Searches for a node with 'old_value' and updates it to 'new_value'.
82
+ Returns True if the node was found and edited, False otherwise."""
83
+
84
+ if root is None:
85
+ return False
86
+
87
+ # Check if current node is the target node
88
+ if root.value == old_value:
89
+ root.value = new_value
90
+ return True
91
+
92
+ # Recurse to left and right child
93
+ found_left = edit_node_value(root.left, old_value, new_value)
94
+ if found_left:
95
+ return True
96
+
97
+ return edit_node_value(root.right, old_value, new_value)
98
+
@@ -0,0 +1,44 @@
1
+ import yaml
2
+ from .node import Node
3
+
4
+ def _dict_to_node(data):
5
+ if not data or 'value' not in data:
6
+ return None
7
+
8
+ node = Node(data['value'])
9
+
10
+ if 'left' in data:
11
+ node.left = _dict_to_node(data['left'])
12
+ if 'right' in data:
13
+ node.right = _dict_to_node(data['right'])
14
+
15
+ return node
16
+
17
+ def build_tree_from_yaml(file_path):
18
+ """Parse a YAML file and generate a tree"""
19
+ try:
20
+ with open(file_path, 'r') as file:
21
+ data = yaml.safe_load(file)
22
+ return _dict_to_node(data)
23
+ except Exception as e:
24
+ print(f"Error reading YAML: {e}")
25
+ return None
26
+
27
+
28
+
29
+ def _node_to_dict(node):
30
+ if not node:
31
+ return None
32
+
33
+ result = {'value': node.value}
34
+ if node.left:
35
+ result['left'] = _node_to_dict(node.left)
36
+ if node.right:
37
+ result['right'] = _node_to_dict(node.right)
38
+ return result
39
+
40
+ def tree_to_yaml(root, file_path):
41
+ """Writes the data in a binary tree into a YAML file"""
42
+ data = _node_to_dict(root)
43
+ with open(file_path, 'w') as file:
44
+ yaml.dump(data, file, default_flow_style=False, sort_keys=False)
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: binary_tree_yaml
3
+ Version: 1.0.0
4
+ Summary: A Binary Tree package with YAML integration
5
+ Author: Shlok
6
+ Requires-Python: >=3.6
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: pyyaml
9
+
10
+ # Binary Tree YAML Package
11
+
12
+ This is a Python package for creating Binary Trees and parsing YAML files and generating a tree from the information in the YAML file.
13
+
14
+ ## Features
15
+ * **Binary Tree Operations**: Create trees, add/delete/edit nodes, and print tree structures.
16
+ * **YAML Integration**: Build trees from YAML files and export tree data to YAML.
17
+
18
+
19
+ ### Prerequisites
20
+ * Python 3.7+
21
+ * pip (must be installed in the system)
22
+
23
+ ## Installation
24
+ ### Installation Steps (via pip from PyPI)
25
+ 1. Go to cmd or terminal.
26
+ 2. Install the package using pip from PyPI:
27
+ `pip install binary_tree_yaml`
28
+
29
+
30
+
31
+
32
+ ### Installation Steps (Manual Install from the package using pip)
33
+ 1. Navigate to the `Task_1` directory.
34
+ 2. Install the package using pip. The `pyproject.toml` file handles the configuration automatically:
35
+ `pip install .`
@@ -0,0 +1,11 @@
1
+ README.md
2
+ pyproject.toml
3
+ binary_tree_yaml.egg-info/PKG-INFO
4
+ binary_tree_yaml.egg-info/SOURCES.txt
5
+ binary_tree_yaml.egg-info/dependency_links.txt
6
+ binary_tree_yaml.egg-info/requires.txt
7
+ binary_tree_yaml.egg-info/top_level.txt
8
+ binary_tree_yaml/binary_tree_yaml/__init__.py
9
+ binary_tree_yaml/binary_tree_yaml/node.py
10
+ binary_tree_yaml/binary_tree_yaml/yaml_utils.py
11
+ tests/tests.py
@@ -0,0 +1 @@
1
+ binary_tree_yaml
@@ -0,0 +1,16 @@
1
+ [build-system]
2
+ requires = ["setuptools>=42", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "binary_tree_yaml"
7
+ version = "1.0.0"
8
+ description = "A Binary Tree package with YAML integration"
9
+ readme = "README.md"
10
+ authors = [
11
+ { name = "Shlok" }
12
+ ]
13
+ requires-python = ">=3.6"
14
+ dependencies = [
15
+ "pyyaml"
16
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,53 @@
1
+ import os
2
+ from binary_tree_yaml import Node, print_tree, add_node_by_path, build_tree_from_yaml,create_tree
3
+
4
+ def create_sample_yaml():
5
+ """Creates the test.yaml file required for the second part of the test."""
6
+ yaml_content = """value: 10
7
+ left:
8
+ value: 5
9
+ left:
10
+ value: 3
11
+ right:
12
+ value: 15
13
+ right:
14
+ value: 18
15
+ left:
16
+ value: 7
17
+ """
18
+
19
+ with open("test.yaml", "w") as f:
20
+ f.write(yaml_content)
21
+
22
+ if __name__ == "__main__":
23
+
24
+ # Binary Tree implementation
25
+ root = create_tree(10)
26
+ print("Initial tree:")
27
+ print_tree(root)
28
+
29
+ print("\nAdding nodes:")
30
+ add_node_by_path(root, "L", 5)
31
+ add_node_by_path(root, "R", 15)
32
+ add_node_by_path(root, "LL", 3)
33
+ add_node_by_path(root, "LR", 7)
34
+ add_node_by_path(root, "RL", 12)
35
+ add_node_by_path(root, "RR", 18)
36
+
37
+ print("\nTree after additions:")
38
+ print_tree(root)
39
+
40
+ # YAML Integration implementation
41
+ create_sample_yaml()
42
+ yaml_file = "test.yaml"
43
+
44
+ print(f"\nBuilding tree from '{yaml_file}':")
45
+ yaml_tree_root = build_tree_from_yaml(yaml_file)
46
+
47
+ if yaml_tree_root:
48
+ print("\nTree built from YAML:")
49
+ print_tree(yaml_tree_root)
50
+
51
+ # Cleanup
52
+ if os.path.exists("test.yaml"):
53
+ os.remove("test.yaml")