Questions tagged [tree-traversal]

The process of visiting each node in a tree based on certain criterion.

The process of visiting each node in a tree based on certain criterion.

Use this tag for programming questions related to traversal of all types of trees.

See also:

832 questions
3
votes
8 answers

What is wrong with my Preorder traversal?

I am trying to solve this problem https://oj.leetcode.com/problems/binary-tree-preorder-traversal/ , i.e. preorder traversal with recursive slution. EDIT: The whole code: import java.util.ArrayList; import java.util.List; public class Solution { …
sammy333
  • 1,384
  • 6
  • 21
  • 39
3
votes
3 answers

Embedding a JavaScript snippet in a Java class method

I have a java program that opens, reads, and write multiple files. it also contains complex logic formatting. Now I wrote on jsfiddle an easy javascript here to do some tree traversals and parsing for me, and it is much easier than implement in…
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
3
votes
1 answer

How to build a list of all branches in a tree?

Have a problem with trees in Haskell. There is a tree: data Tree a b = Leaf a | Branch (b,Tree a b) (b,Tree a b) deriving(Eq, Show) tree = Branch ("A",Branch ("C",Leaf 3) ("D",Branch …
3
votes
1 answer

Having a static_visitor modify a Boost recursive variant while traversing it?

I am using Boost's variant type extensively to build trees. More precisely, I parse a tree from a grammar using Boost's Qi, and then I traverse the tree to annotate each node with an integer - at least that is what I want to do. I just realized that…
Jérémie
  • 1,353
  • 9
  • 19
3
votes
2 answers

How does iterative deepening affect time complexity?

I have a tree traversal algorithm which generally operates in O(bm) where b is the branching factor and m is the max depth. Using iterative deepening, this algorithm is run over and over again, m times at increasing depth: O(bm) = b⁰ + b¹ + b² +…
3
votes
1 answer

Understanding javascript DOM core ideas: nodeList vs HTMLElement objects

I have been working towards understanding the DOM very thoroughly. At the moment i'm making my way trough traversing the DOM tree and i seem to be finding some inconsistencies. On a nodeList i can use an index to iterate trough the list On a…
kevinius
  • 4,232
  • 7
  • 48
  • 79
3
votes
5 answers

Print all paths from root to leaf in a Binary tree

Here is the code I wrote to print all paths of a Binary tree from root to leaf: public static void printRootToLeaf(Node1 root, List list) { if(root == null) { return; } list.add(root.data); if(root.left == null &&…
Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
3
votes
2 answers

How does the recursion preorder traversal algorithm go back to parent?

public static void preorder(Node root) { if(root == null) return; root.printValue(); preorder(root.getLeft()); preorder(root.getRight()); } I have tried to go through this function numerous times but i still can't figure out how…
3
votes
3 answers

Convert Level Order Traversal to Inorder Traversal of a complete binary tree

Given the level order traversal of a complete binary tree in an array, how to store the inorder traversal of the said tree in the given array, without building up the tree. This is what I came up with. void recurse (int *inp, int size_array, int…
3
votes
2 answers

Refactor recursive modifications of tree

How can I refactor the following code: class ModifyTree{ public void doActionsOnTree(Tree tree) { rAction1(tree.getRoot()); rAction2(tree.getRoot()); } private void action1(Node node) { // do something with…
WojciechKo
  • 1,511
  • 18
  • 35
3
votes
1 answer

traversing in a tree

I'm working on a TreeDecomposition where each node in a tree can have more than one vertices from the graph. Now, i'm trying to find the first node which contains a vertex u from the root of tree. int Tree::traversing(Node* node, int u){ …
user322
  • 103
  • 8
3
votes
2 answers

set consolidation for merging and flattening a tree structure

I've got a set of data like this: data = { 1: {"root": [2], "leaf": [10, 11, 12], }, 2: {"root": [1,3], "leaf": [13, 14, 15], }, 3: { "root": [2], "leaf": [16, 17], …
monkut
  • 42,176
  • 24
  • 124
  • 155
3
votes
2 answers

Postorder traversal of nested dom elements selected by jQuery

This should be a nice little puzzle, and hopefully solvable by jQuery. Here is a self explanatory jsFiddle. Note that I am looking for a generic solution to traverse the elements of interest in the dom based on where they are in the dom tree. I…
Aras
  • 5,878
  • 9
  • 49
  • 76
3
votes
2 answers

twalk without globals

I am trying to traverse a binary tree using twalk() with #define _GNU_SOURCE /* Expose declaration of tdestroy() */ #include #include #include #include void *root = NULL; …
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
3
votes
1 answer

jquery :not selector not working in next() method

what is the next best thing to use when you want to select the next li item, but not the one that has someClassName. The not selector returns an empty array! or is this a case off using filter?
  • pickle
Richard
  • 4,516
  • 11
  • 60
  • 87