Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stackless pre-order traversal in a binary tree

Tags:

algorithm

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

like image 323
smilingbuddha Avatar asked Jan 23 '12 17:01

smilingbuddha


People also ask

What is pre-order traversal of binary tree?

Preorder traversal of binary tree is a traversal method, where the root node is visited first, then left subtree and then the right sub tree. Unlike array and linked lists, linear data structures, we have several ways of traversing binary trees due to their hierarchical nature.

What is pre-order traversal process?

Definition: Process all nodes of a tree by processing the root, then recursively processing all subtrees. Also known as prefix traversal.

Is preorder DFS or BFS?

No, pre-order traversal is actually a form of Depth-First-Search (DFS) traversal. There are three different forms of DFS, namely: Pre-Order. In-Order.


1 Answers

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
like image 96
interjay Avatar answered Oct 03 '22 04:10

interjay