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
2 answers

Constructively manipulating any value/object within a JSON tree of unknown depth

I have a JSON tree that contains nodes and children - the format is: jsonObject = { id:nodeid_1, children: [ { id:nodeid_2, children:[] }, { id:nodeid_3, children:[ { id:nodeid_4, children:[] }, { …
Dan Smith
  • 33
  • 1
  • 3
3
votes
1 answer

How to create an array from this result set (nested categories stored in databased with traversal model)?

Based on this question: Getting a modified preorder tree traversal model (nested set) into a
    The logic bellow is used to build an ordered list, but how to do the same with an array? I want to build a nested array. // bootstrap loop $result =…
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
3
votes
1 answer

Traversing an HTML structure searching for attribute

I have a traverse object containing two methods up() and down(), the goals of these methods are to loop upward or downward through the html looking for the first occurrence of specific dataset-attribute and if found return that element. The…
Jordan Davis
  • 1,485
  • 7
  • 21
  • 40
3
votes
1 answer

Non-recursive breadth-first traversal without a queue

In a generic tree represented by nodes having pointers to parent, siblings, and firs/last children, as in: class Tnode { def data Tnode parent = null Tnode first_child = null, last_child = null Tnode prev_sibling = null,…
Basel Shishani
  • 7,735
  • 6
  • 50
  • 67
3
votes
1 answer

How to iterate over a Tree in Scalaz

The Scalaz Tree class proves seemingly very useful `Zipper' functionality via TreeLoc (Javadoc). However, it's not apparent to me how to easily iterate through a tree (e.g. to find the `k-th' node in a tree containing a total of n>k nodes) without…
NietzscheanAI
  • 966
  • 6
  • 16
3
votes
1 answer

How to construct a tree with it's BFS and DFS traversal

I have the BFS and DFS traversal of a tree. How can I reconstruct the tree from these traversals? For example: BFS Traversal : 4 3 5 1 2 8 7 6 DFS Traversal : 4 3 1 7 2 6 5 8 then the tree would be like bellow: 4 / \ 3 5 …
3
votes
2 answers

How to find an element in deeply nested data structure?

I have this structure (which it is the result of parsing JSON response): [{"a" {"b" 1 "c" 2} "children" [{"a" {"b" 3 "c" 4} "children" []}]} {"a" {"b" 5 "c" 6} "children" []} {"a" {"b" 7 "c" 8} "children" [{"a" {"b" 9 "c" 10}…
Chiron
  • 20,081
  • 17
  • 81
  • 133
3
votes
1 answer

prolog traverse nonstandard tree left to right

I need to implement a predicate trav(Tree,List) that performs a left to right tree traversal; Where: Tree is defined by the structure node(left,right), where left and right can be either another node or any Prolog data item. List is the list of…
Zast
  • 492
  • 2
  • 7
  • 22
3
votes
2 answers

How do I output the preorder traversal of a tree given the inorder and postorder tranversal?

Given the code for outputing the postorder traversal of a tree when I have the preorder and the inorder traversal in an integer array. How do I similarly get the preorder with the inorder and postorder array given? void postorder( int preorder[],…
user342580
  • 261
  • 4
  • 10
3
votes
1 answer

Build all Hamiltonian paths from an edge list

I'm having trouble finding a way to build a tree path from a list of related tuples? I only want a list of every path where each node is visited once, aka hamiltonian path. I keep getting close but missing some path. For example, let's say we…
yekta
  • 3,363
  • 3
  • 35
  • 50
3
votes
2 answers

Find all elements with an 'id' attribute on the DOM

Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script
Jay
  • 493
  • 5
  • 14
3
votes
2 answers

How do I find the depth/level of a node in a non-binary tree?

I have a tree structure where each node can have essentially unlimited children, it's modelling the comments for a blog. I'm trying to figure out, given the ID of a specific comment, at what depth/level that comment lies in the tree. I was following…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
3
votes
4 answers

How to modify preorder tree traversal algorithm to handle nodes with multiple parents?

I've been searching for a while now and can't seem to find an alternative solution. I need the tree traversal algorithm in such a way that a node can have more than 1 parent, if it's possible (found a great article here: Storing Hierarchical Data in…
poldo
  • 73
  • 6
3
votes
1 answer

Go tree traversal, trying to understand the code

I'm looking at this page on Rosettacode.org about tree traversal. I'm looking at the Go implementation, I'm fairly new to Go which is why I'd like your help. Right at the beginning of the file, a struct is created. That's ok it makes sense so far.…
Francis
  • 919
  • 3
  • 14
  • 23
3
votes
1 answer

Postorder, preorder, inorder traversals, Binary Search Tree

Consider the following tree structure. A / \ B C / \ \ D E F In what order will the nodes be visited using a postorder, a preorder, and an inorder traversal?
user3500147
  • 125
  • 1
  • 11