Add 'time' order to tree.nodes()
See original GitHub issueIt is sometimes useful to iterate over nodes in the tree in ‘time’ order, where we guarantee that the time of each node visited is >= the time of the previous one. Add this to the options in the order
argument.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Insertion in a Binary Tree in level order - GeeksforGeeks
Given a binary tree and a key, insert the key into the binary tree at the first position available in level order.
Read more >Complexity of Inserting N Numbers into a Binary Search Tree
When we insert a new node, we first check the value of the root node. If the value of the new node is...
Read more >Binary Tree Insert without ordering - Stack Overflow
If you want it to randomly pick, add a call to srand outside this function. srand(time(NULL));. Then in this function, call else if...
Read more >Chapter 12: Binary Search Trees
A binary search tree is a binary tree with a ... The ordering is: the left subtree, the right subtree, the current node....
Read more >TreeNodeCollection.Add Method (System.Windows.Forms)
The root tree nodes display customer names, and the child tree nodes display the order numbers assigned to each customer. In this example,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thought I’d spend an evening looking at this to see what it’s like working with the codebase and as it had some friendly labels! I’ve implemented your trivial implementation above and an additional one using a priority queue from the
heapq
module. Code at https://github.com/benjeffery/tskit/commit/5feba1b4db5574436613e091b54470765f5e741dTo make it a fairer comparison between the methods, the order of traversal is the reverse of the one specified as this lets the priority queue method start at the root. Maybe there is a use case I am unaware of that strongly motivates the original order, but (unless
sample_lists=True
) you’d have to first traverse to get the leaves then work backwards.I generated some trees to traverse with
Code for the plot at https://gist.github.com/benjeffery/8c06f75effdc020b09a7ae0e2ab99246
So the priority queue method is pretty slow - this is due to it not being quite the right algorithm as we are not taking advantage of the fact that we only have need for a monotonic priority queue. In this case to doing something like a radix heap (e.g. https://github.com/iwiwi/radix-heap) would be better, but I couldn’t find anything to quickly drop into python.
msprime.simulate(n)
and then traversed over the first 20 trees.But do we need to? The trivial method is faster than some of the existing methods, about a ~70-80% perf hit compared to
levelorder
and usesnum_nodes * 2 * 8bytes
of additional memory to do the sort.Anyway, that’s how far I got for now, happy to document and then make a PR if needed.
Closed in #399 and #400