Comments (3)
I used the node predecessor
to iterate over the tree nodes, starting from the leaf to the root:
node = tree.get_node(node_id)
while node.predecessor:
if your_condition:
return node.identifier
node = tree[node.predecessor]
from treelib.
An alternative answer to @yuvalshi0 is:
- Get the depth of the two nodes
- Start at the deeper of the two nodes, and keep going up until at the same depth as the other node
- When at the same depth, if the nodes are not the same, then continue moving both up one parent and check if they are the same node
Execution time will depend on the depth of desired nodes. There are better alternatives out there. Search lower common ancestor graph as a start.
Example function
def get_common_frame(tree, node_1: str, node_2: str) -> str:
"""gets the lowest common ancester between two nodes.
Args:
tree: treelib tree object
node_1 (str): the ID of the first node (first and second has no meaning)
node_2 (str): the ID of the second node
Returns:
str: the identifier of the lowest common ancestor node
"""
# check to see which node is deeper
if tree.depth(node_1) > tree.depth(node_1):
# node 1 is further down tree
lower_node = node_1
higher_node = node_2
else:
# node 2 is further down tree
lower_node = node_2
higher_node = node_1
# go until lower node has same depth as higher node
while tree.depth(lower_node) != tree.depth(higher_node):
lower_node = tree.parent(lower_node).identifier
# move both up one level until lower node equals higher node
while lower_node != higher_node:
lower_node = tree.parent(lower_node).identifier
higher_node = tree.parent(higher_node).identifier
# return
return lower_node
Edited: To improve clarity
from treelib.
thanks for answers
from treelib.
Related Issues (20)
- Create nodes with children instead of the parent HOT 2
- Create Tree object from Json HOT 3
- Cannot understand how to use 'successors' HOT 1
- Any method to add weight to edges? HOT 1
- how to replace a node with a new tree HOT 2
- how to insert a node in a tree? HOT 2
- Possible memory leak due to deepcopy ?
- Modifying a node's identifier with the . and = operator breaks the tree
- Update python version support! HOT 2
- Get truncated tree at a given depth
- Finding the nodes between two nodes HOT 1
- Problem exporting to JSON and dict
- potential bug for data parameter in Node
- Give treelib a performance improvement and persistence using leveldb/rocksdb HOT 1
- Treelib not working any longer on Google colab or https://jupyter.org/ HOT 3
- Render tree expanding only ancestors of given node
- tree.show prints binary literal to stdout HOT 4
- [Win] pip install treelib not working | Unable to download treelib library
- get error when do Huffman Coding
- change the keys in _nodes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from treelib.