Questions tagged [traversal]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
33 votes
5 answers
2k views

How to perform a depth-first preorder traversal of an expression?

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, ...
Mr.Wizard's user avatar
  • 270k
28 votes
7 answers
2k views

How to perform a breadth-first traversal of an expression?

Mathematica provides functions that perform a depth-first traversal, or which use such a traversal, including: Scan, Count, <...
Mr.Wizard's user avatar
  • 270k
16 votes
2 answers
2k views

Visiting nodes in a tree bottom up

I have a problem that can be reduced to this one: Suppose I have an unbalanced (almost random) tree. The leaves of the tree have one property already set, and all vertex (including the leaves) have ...
Dr. belisarius's user avatar
14 votes
3 answers
752 views

Determining all possible traversals of a tree

I have a list: B={423, {{53, {39, 65, 423}}, {66, {67, 81, 423}}, {424, {25, 40, 423}}}}; This list can be visualized as a tree using ...
Andrew's user avatar
  • 10.4k
12 votes
1 answer
428 views

Position with Except returns first item {0} and last item {}. Why?

Here is a simple list: l={1, 0, 2, 1, 0, 1, 0, 1, 2, 0, 2, 0, 1, 0, 1, 0, 0} To get the position of non-zero elements I use ...
jerome's user avatar
  • 547
9 votes
1 answer
431 views

How to perform a depth-first in-order traversal of an expression?

To complement my earlier questions: How to perform a breadth-first traversal of an expression? How to perform a depth-first preorder traversal of an expression? I would now like to ask: how to ...
Mr.Wizard's user avatar
  • 270k
5 votes
3 answers
356 views

a function to extract all subexpressions from an expression

Can somebody help me with a function f such that it extract all sub expressions (i.e. looking down the expression tree) from any expression given to it and also a ...
user13892's user avatar
  • 9,113
5 votes
1 answer
218 views

How to make a postordered ReplaceAll that operates just once per subtree?

The answers to this question provide code for a function I'll call postOrderReplaceAll to replace from the inside-out: ...
Seth's user avatar
  • 434
5 votes
2 answers
155 views

How to mark leaves of polynomial based on its position in tree

I've been struggling with this for a few days, and so I thought I might ask this here. Part of the problem lies in the fact that I do not even know the mathematical solution, which runs the risk of ...
QuantumDot's user avatar
  • 19.5k
4 votes
1 answer
308 views

Forcing optimal solution for FindShortestTour

I use FindShortestTour as explained here and it works nicely for a small number of cities. However when I try this with as little as 16 cities, the given tour isn't ...
Aerus's user avatar
  • 141
3 votes
1 answer
214 views

why Level does not sort output according to their levels

Consider this example: Level[a + f[x, y^n], {0, Infinity}] (*{a, x, y, n, y^n, f[x, y^n], a + f[x, y^n]}*) Why the output is sorted in this way. why not ...
Basheer Algohi's user avatar
3 votes
5 answers
470 views

Take the next element in a nested list

My problem is the following: Consider a list of elements. To better understanding my following explanations, I would take this example: ...
Doedalos's user avatar
  • 616