How to efficiently build a tree from a flat structure?

I have a bunch of objects in a flat structure. These objects have an ID and a ParentID property so they can be arranged in trees. They are in no particular order. Each ParentID property does not necessarily matches with an ID in the structure. Therefore their could be several trees emerging from these objects. … Read more

How to find the lowest common ancestor of two nodes in any binary tree?

The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as – struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort – Consider this binary tree : The inorder … Read more

Rolling or sliding window iterator?

I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. Default Python iteration can be considered a special case, where the window length is 1. I’m currently using the following code. Does anyone have a more Pythonic, less verbose, or more efficient method for doing this? def rolling_window(seq, window_size): it = iter(seq) win … Read more

What is the fastest substring search algorithm?

OK, so I don’t sound like an idiot I’m going to state the problem/requirements more explicitly: Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be computed. Function should return a pointer to the first match, or NULL if no match is found. … Read more