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

Understanding Wikipedia code for a generic depth-first tree search?

I was brushing up on different tree traversal methods and ended up reading the following Wikipedia article. As expected, there are three methods of depth first traversal for a binary tree: Preorder traversal Postorder traversal Inorder…
batbrat
  • 5,155
  • 3
  • 32
  • 38
4
votes
1 answer

How to do a Level Order Traversal?

I'm trying to do a linear order traversal on a binary tree but having trouble getting the correct output. Basically I have created a queue and start by enqueuing the root, then until the queue is empty I dequeue the first element and add its…
SharkTiles
  • 585
  • 3
  • 9
  • 22
4
votes
6 answers

PreOrder and PostOrder traversal by modifying morris traversal

The morris traversal works great for InOrder traversal with O(n) time and O(1) space. Is it possible to just by changing a few things achieve PreOrder and PostOrder traversal using the same algorithm.
shreyasva
  • 13,126
  • 25
  • 78
  • 101
4
votes
2 answers

string-append to files in a directory with Scheme

Please note first of all that this is a homework question so I'm not looking for straight code or anything like that, just for someone to maybe help me with my logic. The assignment is in DrRacket. The question asks: Given a FileSystem, which we've…
Mark Soric
  • 1,381
  • 2
  • 9
  • 8
4
votes
1 answer

For every node of a tree, find the nearest ancestor node such that val[node] is coprime to val[ancestor]

For a shorter ver., only read the paragraphs that immediately follow the BOLD sentences and it reduces to only 3 paragraphs. Problem Statement : Given a tree with N nodes rooted at node 1. Each node is associated with a value. Determine the closest…
4
votes
1 answer

solution for tree traversal with python

I am handling with a new problem that relates to tree traverse methods. I have a binary tree with conditional search option. I want to parse an input of type string and traverse tree based on this parsed string. the conditions are a bit complicated…
Amax
  • 75
  • 1
  • 10
4
votes
3 answers

What makes a tree traversal pre-order or in-order?

Why is a tree traversal via root, left and right called pre-order? Shouldn't that be in-order, because the root is always first? It does not makes sense to me why it is called that way, because the root is always the first element.
4
votes
2 answers

How to recognize some binary search tree traversal belongs to post-order or in-order?

If the pre-order of a binary search tree is [P, A, R, S], How to recognize [R, S, A, P] belongs to in-order or post-order? If is post-order how to find out is (Left, Right, Root) or (Right, Left, Root)?
4
votes
4 answers

Vertical Order Traversal in using Level Order Concept of Binary Tree

I want to traverse a Binary Tree vertically. And I found a working code in Geeks for Geeks in C++. I want to convert it into C# but I am unable to do so. Please guide me. Below is my attempt: // we always need the address of the Root Node come what…
Unbreakable
  • 7,776
  • 24
  • 90
  • 171
4
votes
4 answers

Create directory tree in Perl that would comply with Fancytree expected JSON format

How to create directory tree in Perl to comply with Fancytree expected JSON format? This is the Perl part I came up with, that traverses through given path: sub get_tree { my ($gpath) = @_; my %r; use File::Find; my $c = sub { …
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
4
votes
3 answers

How to print a binary tree in as a structure of nodes in Python

I have a python code to convert a string mathematical expression into a binary tree and order the nodes of the tree so the left child will be always smaller than the right child. I want to print the binary tree in the following order. For example…
SriniShine
  • 1,089
  • 5
  • 26
  • 46
4
votes
2 answers

How to reverse walk a tree structure

I have XML parsed as JSON. I want to build a React component tree by traversing the JSON and calling React.createElement on each node. The third argument to React.createElement is an array of child React elements. This means that I have to walk down…
maxhallinan
  • 1,259
  • 2
  • 14
  • 28
4
votes
2 answers

Is the Euler Tour algorithm essentially the same as Pre-order traversal?

I'm trying to learn about the Euler Tour algorithm and why it's popular for tree traversal. However, I'm failing to see the difference between a Euler Tour and a Pre-order traversal of a tree. Let's say you have the tree: A / \ B E …
Bob
  • 166
  • 3
  • 15
4
votes
3 answers

Median of BST in O(logn) time complexity

I came across solution given at http://discuss.joelonsoftware.com/default.asp?interview.11.780597.8 using Morris InOrder traversal using which we can find the median in O(n) time. But is it possible to achieve the same using O(logn) time? The same…
Harish
  • 7,589
  • 10
  • 36
  • 47
4
votes
2 answers

How to remove children of tree nodes with single child

I have an array for pre-order traversal of a tree (node values are depth values). All I want to do is to minimize tree by removing the children of internal nodes having only one child. As an example (a tree with max depth = 3) problem visualized…
guezara
  • 43
  • 5