Skip to content

Commit 66e730b

Browse files
authored
Merge pull request #156 from kayjan/v0.15.3
Added: get_subtree method + docstring examples + update README
2 parents a6ae625 + 9ec4ad1 commit 66e730b

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.15.3] - 2024-01-08
8+
### Added
9+
- Tree Helper: `get_subtree` method to retrieve subtree.
10+
711
## [0.15.2] - 2024-01-08
812
### Added
913
- Tree Exporter: `hprint_tree` and `hyield_tree` to print and retrieve results for tree in horizontal orientation.
@@ -432,6 +436,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
432436
- Utility Iterator: Tree traversal methods.
433437
- Workflow To Do App: Tree use case with to-do list implementation.
434438

439+
[0.15.3]: https://github.com/kayjan/bigtree/compare/0.15.3...0.15.2
435440
[0.15.2]: https://github.com/kayjan/bigtree/compare/0.15.2...0.15.1
436441
[0.15.1]: https://github.com/kayjan/bigtree/compare/0.15.1...0.15.0
437442
[0.15.0]: https://github.com/kayjan/bigtree/compare/0.15.0...0.14.8

README.md

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ For **Tree** implementation, there are 9 main components.
6161
4. Find single child node based on name, user-defined condition
6262
6. [**🔧 Helper Function**](https://bigtree.readthedocs.io/en/latest/bigtree/tree/helper.html)
6363
1. Cloning tree to another `Node` type
64-
2. Prune tree
65-
3. Get difference between two trees
64+
2. Get subtree (smaller tree with different root)
65+
3. Prune tree (smaller tree with same root)
66+
4. Get difference between two trees
6667
7. [**📊 Plotting Tree**](https://bigtree.readthedocs.io/en/latest/bigtree/utils/plot.html)
6768
1. Enhanced Reingold Tilford Algorithm to retrieve (x, y) coordinates for a tree structure
6869
8. [**🔨 Exporting Tree**](https://bigtree.readthedocs.io/en/latest/bigtree/tree/export.html)
@@ -835,58 +836,80 @@ find_child_by_name(c, "c")
835836

836837
### Helper Utility
837838

838-
There following are helper functions for cloning tree to another `Node` type, pruning tree, and getting difference
839-
between two trees.
839+
There following are helper functions for
840+
1. Cloning tree to another `Node` type
841+
2. Getting subtree (smaller tree with different root)
842+
3. Pruning tree (smaller tree with same root)
843+
4. Getting difference between two trees
840844

841845
{emphasize-lines="6,18,38,43"}
842846
```python
843-
from bigtree import BaseNode, Node, clone_tree, prune_tree, get_tree_diff
847+
from bigtree import BaseNode, Node, clone_tree, get_subtree, get_tree_diff, prune_tree, str_to_tree
844848

845-
# Cloning tree from `BaseNode` to `Node` type
849+
# 1. Cloning tree from `BaseNode` to `Node` type
846850
root = BaseNode(name="a")
847851
b = BaseNode(name="b", parent=root)
848852
clone_tree(root, Node)
849853
# Node(/a, )
850854

851-
# Prune tree to only path a/b
852-
root = Node("a")
853-
b = Node("b", parent=root)
854-
c = Node("c", parent=root)
855-
root.show()
856-
# a
857-
# ├── b
858-
# └── c
855+
# Create a tree for future sections
856+
root = str_to_tree("""
857+
a
858+
├── b
859+
│ ├── d
860+
│ └── e
861+
└── c
862+
└── f
863+
""")
859864

865+
# 2. Getting subtree with root b
866+
root_subtree = get_subtree(root, "b")
867+
root_subtree.show()
868+
# b
869+
# ├── d
870+
# └── e
871+
872+
# 3.1 Prune tree to only path a/b
860873
root_pruned = prune_tree(root, "a/b")
861874
root_pruned.show()
862875
# a
863876
# └── b
877+
# ├── d
878+
# └── e
864879

865-
# Get difference between two trees
866-
root = Node("a")
867-
b = Node("b", parent=root)
868-
c = Node("c", parent=root)
869-
root.show()
870-
# a
871-
# ├── b
872-
# └── c
873-
874-
root_other = Node("a")
875-
b_other = Node("b", parent=root_other)
876-
root_other.show()
880+
# 3.1 Prune tree to exactly path a/b
881+
root_pruned = prune_tree(root, "a/b", exact=True)
882+
root_pruned.show()
877883
# a
878884
# └── b
879885

886+
# 4. Get difference between two trees
887+
root_other = str_to_tree("""
888+
a
889+
├── b
890+
│ └── d
891+
└── c
892+
└── g
893+
""")
894+
880895
tree_diff = get_tree_diff(root, root_other)
881896
tree_diff.show()
882897
# a
883-
# └── c (-)
898+
# ├── b
899+
# │ └── e (-)
900+
# └── c
901+
# ├── f (-)
902+
# └── g (+)
884903

885904
tree_diff = get_tree_diff(root, root_other, only_diff=False)
886905
tree_diff.show()
887906
# a
888907
# ├── b
889-
# └── c (-)
908+
# │ ├── d
909+
# │ └── e (-)
910+
# └── c
911+
# ├── f (-)
912+
# └── g (+)
890913
```
891914

892915
### Export Tree

bigtree/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.15.2"
1+
__version__ = "0.15.3"
22

33
from bigtree.binarytree.construct import list_to_binarytree
44
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag
@@ -35,7 +35,7 @@
3535
tree_to_pillow,
3636
yield_tree,
3737
)
38-
from bigtree.tree.helper import clone_tree, get_tree_diff, prune_tree
38+
from bigtree.tree.helper import clone_tree, get_subtree, get_tree_diff, prune_tree
3939
from bigtree.tree.modify import (
4040
copy_and_replace_nodes_from_tree_to_tree,
4141
copy_nodes,

bigtree/tree/helper.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from bigtree.utils.exceptions import NotFoundError
1111
from bigtree.utils.iterators import levelordergroup_iter
1212

13-
__all__ = ["clone_tree", "prune_tree", "get_tree_diff"]
13+
__all__ = ["clone_tree", "get_subtree", "prune_tree", "get_tree_diff"]
1414
BaseNodeT = TypeVar("BaseNodeT", bound=BaseNode)
1515
BinaryNodeT = TypeVar("BinaryNodeT", bound=BinaryNode)
1616
NodeT = TypeVar("NodeT", bound=Node)
@@ -61,6 +61,27 @@ def get_subtree(
6161
) -> NodeT:
6262
"""Get subtree based on node name or node path, and/or maximum depth of tree.
6363
64+
>>> from bigtree import Node, get_subtree
65+
>>> root = Node("a")
66+
>>> b = Node("b", parent=root)
67+
>>> c = Node("c", parent=b)
68+
>>> d = Node("d", parent=b)
69+
>>> e = Node("e", parent=root)
70+
>>> root.show()
71+
a
72+
├── b
73+
│ ├── c
74+
│ └── d
75+
└── e
76+
77+
Get subtree
78+
79+
>>> root_subtree = get_subtree(root, "b")
80+
>>> root_subtree.show()
81+
b
82+
├── c
83+
└── d
84+
6485
Args:
6586
tree (Node): existing tree
6687
node_name_or_path (str): node name or path to get subtree, defaults to None

0 commit comments

Comments
 (0)