Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:06:38+00:00 2026-05-28T13:06:38+00:00

Is it possible to perform iterative * pre-order * traversal on a binary tree

  • 0

Is it possible to perform iterative *pre-order* traversal on a binary tree without using node-stacks or “visited” flags?

As far as I know, such approaches usually require the nodes in the tree to have pointers to their parents. Now, to be sure, I know how to perform pre-order traversal using parent-pointers and visited-flags thus eliminating any requirement of stacks of nodes for iterative traversal.

But, I was wondering if visited-flags are really necessary. They would occupy a lot of memory if the tree has a lot of nodes. Also, having them would not make much sense if many pre-order tree traversals of a binary-tree are going on simultaneously in parallel.

If it is possible to perform this, some pseudo-code or better a short C++ code sample would be really useful.

EDIT: I specifically do not want to use recursion for pre-order traversal. The context for my question is that I have an octree (which is like a binary tree) which I have constructed on the GPU. I want to launch many threads, each of which does a tree-traversal independently and in parallel.

Firstly, CUDA does not support recursion.
Seoncdly, the concept of visited flags applies only for a single traversal. Since many traversals are going on simultaneously , having visited-flags field in the node data structure is of no use. They would make sense only on the CPU where all independent tree traversals are/can be serialised. To be more specific, after every tree-traversal we can set the visited-flags to false before performing another pre-order tree-traversal

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:06:39+00:00Added an answer on May 28, 2026 at 1:06 pm

    You can use this algorithm, which only needs parent pointers and no additional storage:

    For an inner node, the next node in a pre-order traversal is its leftmost child.

    For a leaf node: Keep going upwards in the tree until you are coming from the left child of a node with two children. That node’s right child will then be the next node to traverse.

    function nextNode(node):
        # inner node: return leftmost child
        if node.left != null:
            return node.left
        if node.right != null:
            return node.right
    
        # leaf node
        while (node.parent != null)
            if node == node.parent.left and node.parent.right != null:
                return node.parent.right
            node = node.parent
    
        return null  #no more nodes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it even possible to perform a cross-domain POST Ajax request without using proxy?
In SQL Server is it possible to perform table partition without having to recode
can i perform select single node from IXMLDOMElement ,if its possible give me one
Is it possible to perform multi-variate regression in Python using NumPy? The documentation here
Is it even possible to perform address (physical, not e-mail) validation? It seems like
Is it possible to perform a named-group match in Perl's regex syntax as with
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example
What are possible reasons why NHibernate does not perform delete operation? public bool Delete(MyType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.