-
Notifications
You must be signed in to change notification settings - Fork 29
Method `baltic.tree.fixHangingNodes`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The fixHangingNodes()
method in the BALTIC tree
class is used to remove internal nodes that have no children. This method is crucial for maintaining a valid tree structure, especially after operations that might leave some nodes childless, such as pruning or subtree extraction.
def fixHangingNodes(self)
This method doesn't take any parameters.
- Iteratively identifies internal nodes (instances of the
node
class) that have no children. - For each identified hanging node:
- Removes the node from its parent's list of children.
- Removes the node from the tree's
Objects
list.
- Repeats this process until no more hanging nodes are found in the tree.
- Cleaning up the tree structure after pruning operations.
- Ensuring tree validity after subtree extraction or other manipulations.
- Preparing the tree for visualization or further analysis.
- Removing artifacts that might have been introduced by complex tree operations.
# Assuming we have a tree that might have hanging nodes
# First, let's check if we have any hanging nodes
hanging_nodes = [node for node in tree.Objects if node.is_node() and not node.children]
print(f"Number of hanging nodes before fixing: {len(hanging_nodes)}")
# Now, let's fix the hanging nodes
tree.fixHangingNodes()
# Check again for hanging nodes
hanging_nodes_after = [node for node in tree.Objects if node.is_node() and not node.children]
print(f"Number of hanging nodes after fixing: {len(hanging_nodes_after)}")
# Verify the tree structure
print(f"Total number of objects in tree: {len(tree.Objects)}")
print(f"Number of internal nodes: {len(tree.getInternal())}")
print(f"Number of leaves: {len(tree.getExternal())}")
- This method modifies the tree structure in-place. It does not return a new tree object.
- Hanging nodes can occur after operations like
reduceTree()
,subtree()
, or manual tree manipulations. - The method ensures that all remaining internal nodes have at least one child, maintaining a valid tree structure.
- After calling this method, you may want to update other tree properties or rerun analyses that depend on the tree structure.
- This method is often called internally by other BALTIC functions to ensure tree integrity after complex operations.
- While it's generally safe to call this method at any time, it's most useful after operations that might have altered the tree structure.
- For very large trees, this method might have a noticeable performance impact if there are many hanging nodes to remove.
Wiki written with the assistance of claude.ai 3.5 "Sonnet".
- Core
baltic
classes:-
Class
tree
- Tree Construction and Manipulation methods
- Tree Analysis methods
- Tree Conversion and Output methods
- Tree Visualization
- Utility Methods
- Class
leaf
- Class
node
- Class
clade
- Class
reticulation
-
Class