c++,binary-search-tree,tree-traversal

This seems pretty much OK. You can't really improve the complexity of what you did, since the number of operations is linear. A few implementation points: Counting is a non-modifying operation, so you might want to consider consting some stuff. You've basically implemented on your own something similar to what...

python,python-3.x,file-io,tree-traversal

generateTable() returns table unchanged; you passed in an empty dictionary, and the function does nothing else but return that same object. The nested codeTable() function is not used; nothing ever actually calls it. Even if you unindented the codeTable(node, '') call to be outside of the nested function, that function...

java,binary-search-tree,priority-queue,tree-traversal

Exception in thread "main" java.lang.ClassCastException: Node cannot be cast to java.lang.Comparable means you must implement java.lang.Comparable interface in your Node class (something like: public class Node implements Comparable { //... }) ) for your Node objects to be comparable to other nodes....

java,oop,generics,interface,tree-traversal

So, here is your flattenInOrder method: public List<T> flattenInOrder(final Tree<T> tree) { final EitherOr<T, Triple<Tree<T>>> EitherOr = tree.get(); if (EitherOr.isLeft()) { return Collections.singletonList(EitherOr.ifLeft(this.ifLeftFunction)); } return EitherOr.ifRight(this.ifRightFunction); } Quite simple, assuming that: ifLeftFunction yields a single element (since EitherOr<T, Triple<Tree<T>>> has a single T elem' if it s "left") ... and:...

java,search,binary-tree,tree-traversal,preorder

public Node search(int val) { Node target = this; if(target.getVal() == val) return this; else if(target.getRight() == null && target.getLeft() == null) return null; if(target.getRight() != null) { return target.getRight().search(id); //here lies the problem } if(target.getLeft() != null) { return target.getLeft().search(id); } return null; } The problem in your code...

algorithm,binary-tree,tree-traversal

This is the first algorithm that comes to mind. Assuming that you have an array that stores the values in nodes node_value[NODE_NUM], where NODE_NUM is the number of nodes in your tree, and you store index of childs of each node with the arrays left[NODE_NUM] and right[NODE_NUM], and your root...

algorithm,data-structures,tree,tree-traversal,ternary-search-tree

You can think of a ternary search tree as a hierarchy of different binary search trees - the black lines connect together nodes in the same BST, and the dotted lines link different BSTs together. You can list off all the words in a TST by doing a modified inorder...

python,algorithm,tree,tree-traversal

class heap: def __init__(self,the_heap): self.heap = the_heap def getChildren(self,value): n = self.heap.index(value) return self.heap[2*n+1],self.heap[2*n+2] # i think ... def getParent(self,value): n = self.heap.index(value) if n == 0: return None return self.heap[math.floor(n-1/2.0) ] # i think ... def traverse(self): #do your traversal here just visit each node in the order you...

javascript,jquery,dom,tree-traversal

You can query all the elements with an id attribute by using the following jQuery selector: $("[id]") You can also just use plain JavaScript using querySelectorAll: document.querySelectorAll("[id]") If you want to find non-empty id attributes, use this selector instead: '[id]:not([id]="")' ...

c++,boost,tree-traversal,boost-variant,static-visitor

You can very much modify objects taken by reference: void operator()(int) const { } void operator()(s_node& b) const { b.value = 10.0; } See it Live On Coliru, output: Before: s_node {5, s_node {7, 42 /*value: -1*/} /*value: -1*/} After: s_node {5, s_node {7, 42 /*value: -1*/} /*value: 10*/} Full...

c,recursion,tree,tree-traversal

Passing around a int[] variable is really almost like passing a int* around. The first invocation of the recursive function passes the real int[] which is nothing more than an address in memory, that's the same address used in every recursive call. Basically if you place a debug print, eg...

javascript,recursion,tree-traversal

This line: return customize.call(this[prop], conf[prop]); occurs inside a for loop, so you are returning before each item has been iterated over. Your return statement should be outside the loop....

My Prolog is slightly rusty, but I believe that this should do it: node(Left, Right). trav(node(Left, Right), L) :- trav(Left, L1), trav(Right, L2), append(L1, L2, L). trav(X, [X]) :- X \= node(A, B). Intuitively, trav(Left, L1) says traverse the left subtree, storing each element in L1. trav(Right, L2) says traverse...

java,graph,paintcomponent,tree-traversal

I found the solution: just repaint component after each time insert or delete a node in tree

tree,lisp,common-lisp,tree-traversal,postorder

I just figured that you have to call tree-depth not postorder. Though the answer basically stays the same: As soon as you use trace to get the call-tree: (trace tree-depth) (tree-depth '(1 (2 (4) (6)) (5 (7) (8)))) 0: (TREE-DEPTH (1 (2 (4) (6)) (5 (7) (8)))) 1: (TREE-DEPTH (2...

c++,tree,binary-tree,tree-traversal

Essentially, it's the same answer as your last post. right_old = findoldestn(n->right); left_old = findoldestn(n->left); then figure out the oldest one between left/right and current, and return that value. And that can be put in place with res = (right_old->age > left_old->age ? right_old : left_old); finalRet = (res->age >...

c,tree,binary-search-tree,tree-traversal

The tree structure you are ending up is something like below 10 \ 20 / 15 The order of traversal will be as below: In-order: 10 15 20 Pre-order: 10 20 15 Post-order: 15 20 10 BST does maintain the property that elements in the lef-subtree are smaller than the...

c#,data-structures,tree,tree-traversal

I'll take a crack at it. This assumes a link to the parent and that you can retrieve the number of children on a node and access a child by index. static TreeNode Clone(TreeNode root) { var currentOriginal = root; var currentCloned = Copy(root, null); var clonedRoot = currentCloned; while...

php,arrays,recursion,tree,tree-traversal

Very interesting task. See my solution below: categories array $categoriesrRaw = Array ( '0' => Array ( 'category_id' => 1, 'category_name' => 'Home & Garden', 'parent_id' => 0, 'level' => 1, ), '1' => Array ( 'category_id' => 2, 'category_name' => 'Kitchen & Dining', 'parent_id' => 1, 'level' => 2,...

python,algorithm,tree,tree-traversal

What you need to do is add the root of the pre-ordered list to the tree, and removing it from preorder list. Split the in order list as you are doing, then passing both the left and right branches recursively. Keep adding the first element of pre-order to the left...

algorithm,binary-search-tree,tree-traversal,range-query

The concrete solution depends on the definition of a subtree. Consider the following BST: 5 3 2 4 8 - 9 And we want to find the subtrees in the range [4,8]. It is obvious that the 4 node belongs to the output. But what about the other half tree?...

algorithm,binary-tree,tree-traversal

In the attached code, printGivenLevel() is O(n) indeed for worst case. The *complexity function) of printGivenLevel() is: T(n) = T(left) + T(right) + O(1) where left = size of left subtree right = size of right subtree In worst case, for each node in the tree there is at most...

The inorder traversal will continue to be well defined only if you explicitly partition the children set into left children and right children. To see this, note that the inorder traversal actually enumerates nodes in the order in which they will appear if we flatten the tree (or equivalently, the...