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
7
votes
1 answer

What type of tree traversal does the ast module use?

What type of tree traversal does ast use (specifically ast.NodeVisitor())? When I created a stack and pushed each node that was traversed into the stack the results seemed to be a 'breadth first' tree traversal. Meaning that the order was dependent…
baallezx
  • 471
  • 3
  • 14
7
votes
4 answers

checking subtrees using preorder and inorder strings

A book I'm reading claims that one way to check whether a binary tree B is a subtree of a binary tree A is to build the inorder and preorder strings (strings that represent the inorder and preorder traversal of each tree) of both trees, and check…
fo_x86
  • 2,583
  • 1
  • 30
  • 41
7
votes
14 answers

converting a binary search tree to doubly linked list

This question was asked in a recent coding interview. Q : Given a binary tree, write a program to convert it to a doubly linked list. The nodes in the doubly linked list are arranged in a sequence formed by a zig-zag level order traversal My…
akash
  • 1,801
  • 7
  • 24
  • 42
6
votes
2 answers

Javascript Tree Traversal Algorithm

I need help traversing a tree structure in a depth first fashion. I can't come up with an algorithm to do it properly. My input is this: [ ["A", "B", "C"], ["1", "2"], ["a", "b", "c", "d"] ] The output should take the form: [ …
Adam
  • 12,236
  • 9
  • 39
  • 44
6
votes
3 answers

Traversing a general tree structure starting from an arbitrary node in C#

I need tree traversal algorithms for arbitrary trees in both Depth-first and Breadth-first Traversal order. The tricky part is that I need to be able to start from an arbitrary node and continue until another specific node is traversed. Now, I can…
Kemal Erdogan
  • 1,060
  • 1
  • 9
  • 19
6
votes
1 answer

Generic traversal of a directed tree with Neo4J

I modelled a directed tree structure using the graph database Neo4J. So I have something like this: http://ouwarovite.net/YAPC/220px-Binary_tree.svg.png (not mandatory binary) Users of my database can add child nodes of existing nodes at will, so…
6
votes
4 answers

Returning Array from Recursive Binary Tree Search

Hi I've made a simple Binary Tree and added a pre-order traversal method. After throwing around some ideas I got stuck on finding a way to return each value from the traverse_pre() method in an array. class BST: def __init__(self, val): …
6
votes
4 answers

Number all occurring leaves in a tree from left to right in Haskell

The function type would be Tree a -> Tree (a, Int). I want to carry the count throughout the tree and number each occurring leaf accordingly. So far I have tried this: labelTree :: Tree a -> Tree (a, Int) labelTree (Leaf a) = Leaf (a,1) labelTree…
Sniper
  • 418
  • 3
  • 12
6
votes
1 answer

BFS in JavaScript using Recursion

It is easy to do DFS using recursion: function dfs(tree, fn, level) { fn(tree, level) tree.children.forEach(function(child){ dfs(child, fn, level + 1) }) } However every example I have seen of BFS uses a queue and is iterative rather than…
Lance
  • 75,200
  • 93
  • 289
  • 503
6
votes
1 answer

Understanding the logic in iterative Postorder traversal implementation on a Binary tree

I was trying to understand how it is so intuitive to implement postorder traversal using 2 stacks. How did someone come up with it, is it just an observation or some particular way of thinking which helps one come up with such methods. If yes then…
6
votes
3 answers

Functions to convert between depth first and breadth first traversals of a complete tree

Problem: Consider a complete k-ary tree with l levels, with nodes labelled by their rank in a breadth first traversal. Compute the list of labels in the order that they are traversed in the depth first traversal. For example, for a binary tree with…
sitiposit
  • 149
  • 1
  • 13
6
votes
1 answer

Lazy tree traversal iterator in Scala

If my tree is defined as such: case class Node(value: Int, children: Seq[Node]) but for the sake of the argument, let's say that accessing the children are expensive such that I want to traverse them only when I actuall need to. If a non-strict,…
lolski
  • 16,231
  • 7
  • 34
  • 49
6
votes
2 answers

explain the Haskell breadth first numbering code to traverse trees

I am reading this paper by Chris Okasaki; titled "Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design". A question is - how is the magic happening in the algorithm? There are some figures (e.g. figure 7 titled "threading the…
6
votes
6 answers

How to construct a binary tree from just the level order traversal string

Consider a binary tree with the following properties: An internal node (non-leaf node) has a value 1 if it has two children. A leaf node has a value 0 since it has no children. A level order traversal on the tree would generate a string of 1s and…
Jinson George
  • 139
  • 2
  • 11
6
votes
1 answer

C# minmax graph search

EDIT 3: Okay, so i got my code to work, but i'm facing a huge memory consumption problem if i'm using let's say 16 nodes and a search depth above 11. an soemone check the code and tell me how can i correct that memory leak? Here's the full…