question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add 'time' order to tree.nodes()

See original GitHub issue

It 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:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
benjefferycommented, Oct 24, 2019

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/5feba1b4db5574436613e091b54470765f5e741d

To 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 msprime.simulate(n) and then traversed over the first 20 trees. time 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.

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 uses num_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.

0reactions
jeromekellehercommented, Nov 22, 2019

Closed in #399 and #400

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found