Construct a tree structure from list of string paths

Follow an implementation of naive implementation of a visitable tree: class Tree<T> implements Visitable<T> { // NB: LinkedHashSet preserves insertion order private final Set<Tree> children = new LinkedHashSet<Tree>(); private final T data; Tree(T data) { this.data = data; } void accept(Visitor<T> visitor) { visitor.visitData(this, data); for (Tree child : children) { Visitor<T> childVisitor = visitor.visitTree(child); … Read more

What are the differences between segment trees, interval trees, binary indexed trees and range trees?

All these data structures are used for solving different problems: Segment tree stores intervals, and optimized for “which of these intervals contains a given point” queries. Interval tree stores intervals as well, but optimized for “which of these intervals overlap with a given interval” queries. It can also be used for point queries – similar … Read more

How do I print out a tree structure?

The trick is to pass a string as the indent and to treat the last child specially: class Node { public void PrintPretty(string indent, bool last) { Console.Write(indent); if (last) { Console.Write(“\\-“); indent += ” “; } else { Console.Write(“|-“); indent += “| “; } Console.WriteLine(Name); for (int i = 0; i < Children.Count; i++) … Read more