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

Traversing through all nodes of a binary tree in Java

Let's say I have a simple binary tree node class, like so: public class BinaryTreeNode { public String identifier = ""; public BinaryTreeNode parent = null; public BinaryTreeNode left = null; public BinaryTreeNode right = null; …
RectangleEquals
  • 1,825
  • 2
  • 25
  • 44
18
votes
8 answers

How to do in-order traversal of a BST without recursion or stack but using parent pointers?

Is it possible to do an iterative in-order-traversal on a BST whose node has a parent pointer (the parent of the root is null) without using a visited flag or a stack? I googled and didn't find a reply. The point is, how can I know - at a certain…
OmarOthman
  • 1,718
  • 2
  • 19
  • 36
17
votes
1 answer

Post-order Graph Traversal?

Given the directed graph below, how did we achieve the post-order traversal? DFS Visiting order in Pre-order traversal: 1 2 5 4 6 3 Visiting order in Post-order traversal: 4 6 5 2 1 3
33ted
  • 689
  • 1
  • 5
  • 14
17
votes
6 answers

print level order traversal of binary tree in Zigzag manner

I have to print the nodes of a binary tree using level order traversal but in spiral form i.e nodes at different level should be printed in spiral form. for eg: If the tree look like: The output should be 10 5 20 25 15 6 4. The algorithm i used is…
poorvank
  • 7,524
  • 19
  • 60
  • 102
16
votes
2 answers

Depth First Traversal on BeautifulSoup Parse Tree

Is there a way to do a DFT on a BeautifulSoup parse tree? I'm trying to do something like starting at the root, usually , get all the child elements and then for each child element get their children, etc until I hit a terminal node at which point…
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
16
votes
2 answers

Catamorphism and tree-traversing in Haskell

I am impatient, looking forward to understanding catamorphism related to this SO question :) I have only practiced the beginning of Real World Haskell tutorial. So, Maybe I'm gonna ask for way too much right now, if it was the case, just tell me the…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
16
votes
4 answers

Postorder Traversal

In-order tree traversal obviously has application; getting the contents in order. Preorder traversal seems really useful for creating a copy of the tree. Is there a common use for postorder traversal of a binary tree?
Dean J
  • 39,360
  • 16
  • 67
  • 93
15
votes
5 answers

Tree traversal algorithm for directory structures with a lot of files

When recursively traversing through a directory structure, what is the most efficient algorithm to use if you have more files than directories? I notice that when using depth-first traversal, it seems to take longer when there are a lot of files in…
oninea
  • 1,423
  • 4
  • 14
  • 15
14
votes
7 answers

Parallel tree traversal in C#

I need to traverse a tree quickly, and I would like to do it in parallel. I'd rather use the parallel extensions than manually spin up a bunch of threads. My current code looks something like this: public void Traverse(Node root) { var nodeQueue…
14
votes
2 answers

Real world pre/post-order tree traversal examples

I understand pre-order, in-order, and post-order tree traversal algorithms just fine. (Reference). I understand a few uses: in-order for traversing binary search trees in order, pre-order for cloning a tree. But I can't for the life of me come up…
Plutor
  • 2,867
  • 2
  • 25
  • 29
14
votes
1 answer

beautiful soup get children that are Tags (not Navigable Strings) from a Tag

Beautiful soup documentation provides attributes .contents and .children to access the children of a given tag (a list and an iterable respectively), and includes both Navigable Strings and Tags. I want only the children of type Tag. I'm currently…
Anov
  • 2,272
  • 2
  • 20
  • 26
13
votes
3 answers

cannot cast to java.lang.Comparable

Although this question has already been asked but I have an implementation specific doubt. I am trying to print the top view of the binary tree and following is the complete code for it: import java.util.*; class Node{ int data; Node…
pkenil96
  • 173
  • 1
  • 1
  • 7
13
votes
3 answers

How do I query objects of all children of a node with Django mptt?

I am trying to get the objects of all the children of a given node on Django with django-mppt I have a model designed as shown below, the classes/categories (node) with the same indent level defines siblings, inner indents are children. The objects…
user5170375
12
votes
2 answers

Breadth First Search Traversal VS Pre-order Traversal VS Depth First Search Traversal

For a binary tree, is Breadth First Search traversal (BFS) the same as Pre-order traversal? I am a little bit confused by these two different types of traversals. Can anyone please explain this to me? Additionally, how does Pre-order traversal…
Liu
  • 413
  • 4
  • 11
12
votes
3 answers

How to convert a tree structure to a Stream of nodes in java

I want to convert a tree in a Java8 stream of nodes. Here is a tree of nodes storing data which can be selected: public class SelectTree { private D data; private boolean selected = false; private SelectTree parent; private final…
kwisatz
  • 1,266
  • 3
  • 16
  • 36
1
2
3
55 56