How to deep copy a Binary Tree?

try class Node { private String value; private Node left; private Node right; public Node(String value, Node left, Node right) { this.value = value; … } Node copy() { Node left = null; Node right = null; if (this.left != null) { left = this.left.copy(); } if (this.right != null) { right = this.right.copy(); } … Read more

Create a tree from a list of strings containing paths of files – javascript

You can create this structure using forEach method to loop each path and split it to array on /, then you can also use reduce method to create nested objects. let paths = [“About.vue”,”Categories/Index.vue”,”Categories/Demo.vue”,”Categories/Flavors.vue”,”Categories/Types/Index.vue”,”Categories/Types/Other.vue”]; let result = []; let level = {result}; paths.forEach(path => { path.split(“https://stackoverflow.com/”).reduce((r, name, i, a) => { if(!r[name]) { r[name] = … Read more

Iterative tree walking

If you are doing a breadth first search the natural implementation is to push nodes into a queue, not to use recursion. If you are doing a depth first search then recursion is the most natural way to code the traversal. However, unless your compiler optimizes tail recursion into iteration, your recursive implementation will be … Read more

Convert array of paths into UL list

Edit: Changed to cater for updated question. I’m using an array index of __title to hold the page title. As long as you never have a directory in your tree called __title this should be fine. You’re free to change this sentinel value to anything you wish however. I have also changed it so the … Read more