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
4
votes
3 answers

Build a Tree Out of File Path, Is My Logic Correct?

I am trying to build a tree and I would like to link parent nodes to children based on a filepath like structure such as the one below, where The World is the root: The World The World/Asia The World/Asia/Afghanistan The…
audiFanatic
  • 2,296
  • 8
  • 40
  • 56
4
votes
1 answer

Boost.Filesystem-like Library for D

Has anybody written a D library similar to Boost.Filesystem providing iterators/ranges that can do for example file system directory tree traversal?
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
4
votes
1 answer

What type of traversal should be used to find the sum of a binary tree?

The following question appeared on a test my instructor gave a couple of years ago. He provided an answer, but did not provide a satisfactory explanation. I have googled this topic, but I didn't find much. I was hoping that the community could…
Ian
  • 348
  • 1
  • 2
  • 11
4
votes
3 answers

Steepest Ascent Hill Climbing vs Best First Search

I am trying to solve a problem using different algorithms and Steepest Ascent Hill Climbing (SAHC) and Best First Search are two of these algorithms that I need to implement. According to Wikipedia: "In steepest ascent hill climbing all successors…
karancan
  • 2,152
  • 4
  • 23
  • 35
4
votes
2 answers

SQL tree traversal down to the root and back up one

I have the following table (ObjectStates) and want to get the root as well as the first-child of the root: ID Title ParentID 1 Draft null 2 Green null 3 Red null 4 Foo 1 5 Bar 4 6 Some1 1 7 …
Dennis G
  • 21,405
  • 19
  • 96
  • 133
4
votes
5 answers

Level Order tree Traversal for a generic tree, displaying the tree level by level

I would like to display the tree structure level by level. My current code does a BFS or Level Order Traversal but I cannot get the output to display the tree structure like a tree See current output and Expected output. My idea was to use some kind…
Sameer
  • 757
  • 1
  • 14
  • 35
4
votes
1 answer

Why pre-order traversal is favored for cloning a tree?

I know the definition of pre-order traversal and want to understand why pre-order traversal strategy is favored for cloning a tree ? I mean why it is preferred more than the other traversal mechanisms like in order traversal and post order…
Geek
  • 26,489
  • 43
  • 149
  • 227
4
votes
3 answers

How to track the progress of a tree traversal?

I have a tree. It has a flat bottom. We're only interested in the bottom-most leaves, but this is roughly how many leaves there are at the bottom... 2 x 1600 x 1600 x 10 x 4 x 1600 x 10 x 4 That's ~13,107,200,000,000 leaves? Because of the size…
John Mee
  • 50,179
  • 34
  • 152
  • 186
4
votes
3 answers

Jackson json : traversing a json tree node by node

I have numerous text files containing json data and I'm using new ObjectMapper().readTree() method in Jackson json parser to parse the json data to DOM trees. Let's say now I now have two DOM trees - t1 and t2. Each tree will have many children…
athreya86
  • 98
  • 1
  • 1
  • 8
3
votes
1 answer

Traversing along Upper & Lower Siblings efficiently

Trying to build a plugin effect that will some what look better than this when its ready. Note the numbers 0, 1, 2. They are simple indication of level the element currently is in. 0 denoting the top level and 2 the least. Check the demo here.…
Starx
  • 77,474
  • 47
  • 185
  • 261
3
votes
1 answer

InOrder tree traversal

How can I implement InOrder traversal on this kind of tree? I need to print the operators too (like 3-2-1). I have these classes: public class BinaryOperator extends Value { private Value firstOperand; private Value secondOperand; …
user219882
  • 15,274
  • 23
  • 93
  • 138
3
votes
7 answers

How to find the number of nodes at level k of a binary tree without using Breadth-first order traversal?

Given this binary tree (actually, the binary tree can be random and dynamic, this is just an example...): See link for the binary tree image: binary tree example This are the given facts: All nodes are connected to their father so that we can…
FaTaL_E
  • 107
  • 1
  • 11
3
votes
3 answers

Can every recursive process be transformed into an iterative process?

I was reading the book, Structure and Interpretation of Computer Programs, where in it tells about the distinction between a recursive procedure and recursive process, and similarly between iterative procedure and iterative process. So, a recursive…
abhinav
  • 221
  • 1
  • 3
  • 13
3
votes
2 answers

balanced binary tree visit with a twist

Looking for an in-place O(N) algorithm which prints the node pair(while traversing the tree) like below for a given balanced binary tree: a b c d e f g Output: bc, de, ef, fg Where 'a' is the…
user883276
  • 31
  • 2
3
votes
1 answer

Handling duplicates when constructing binary trees

I came across the following question in leetcode - https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ I was able to write an algorithm below (find the preorder and post order traversals and save them. Then reconstruct the tree from…