New answers tagged

2

Review Well written API, but I am not happy with this one: traverse({ preorder, postorder, levelorder }) preorder and postorder can be combined, but they are always mutually exclusive with levelorder. traverse({ preorder, postorder, levelorder }) { if (levelorder) { // levelorder .. } else { // preorder conditional and/or postorder ...


1

def cycle_dfs(self, current_node, start_node, graph, current_path): ... if len(current_path) >= 3: last_three_vertices = current_path[-3:] previous_three_faces = [set(self.faces_containing_arcs[vertex]) for vertex in last_three_vertices] intersection_all = set....


3

Class Definition First of all, I would separate the class definition from the actual use case. The challenge wants you to walk nodes with integer data, but a next challenge may ask you to use a string instead. Don't let the use case stipulate the class definition. Let's make a generic class to make the tree reusable for more scenarios. public class ...


3

This is overall well-thought-out, well-organized, and well-written. There's at least one bug, and one oversight that I would consider a bug: In traverse your recursive call is passing the wrong second parameter: } else if (mode == Node.Traversal.DepthFirst) { callback(this); this.children.forEach(n => n.traverse(callback, false)); } should read }...


2

Review Your code is compact and well organised. Reusing traverse and reduce allows for easy extensibility. Your tree could be a graph, or even worse a cyclic one: traverse could iterate to infinity. By adding additional methods such as descendants and ancestors you could guard that the structure remains a tree. When adding a node, it cannot have a parent, ...


Top 50 recent answers are included