0

How to traverse a threaded binary tree non recursively in O(n) without using a stack (just allowed to use constant extra space for temp variables, so we can't add visit flag to each node in the tree). I spent a good time thinking about it but it just doesn't seem to me to be doable unless we are going to traverse the memory locations that have the tree data. Let's say that we are using multiple array representation to implement pointers, then we can traverse the tree in O(n), does anyone have something else in mind?

Note This is not homework, just to save the energy of some keyboard strokes to write comments about homework!

K''
  • 5,020
  • 7
  • 33
  • 43
  • Have you looked at [Wikipedia](http://en.wikipedia.org/wiki/Threaded_binary_tree#Non_recursive_Inorder_traversal_for_a_Threaded_Binary_Tree)? – svick May 06 '12 at 20:03
  • @svick The second step in the algorithm is _Step-2: Put that left child in the list of visited nodes and make it your current node in consideration. Go to step-6._ I mentioned in the question that I need to do it with _constant extra space_ – K'' May 06 '12 at 20:13
  • Is my answer relevant to your question? If so, please mark it as one. If not, please tell why and I'll try to refine it. – adaszko May 07 '12 at 19:21

2 Answers2

5

Let's say that we have the following threaded tree representation in C:

typedef struct threaded_binary_tree {
    int value;

    // Flag that tells whether right points to a right child or to a next
    // node in inorder.
    bool right_is_child;

    struct threaded_binary_tree *left, *right;
} threaded_binary_tree;

Then, traversing it in O(1) memory may look like this:

void inorder(threaded_binary_tree *node)
{
    threaded_binary_tree *prev = NULL;

    // Ignore empty trees
    if (node == NULL)
        return;

    // First, go to the leftmost leaf of the tree
    while (node->left != NULL)
        node = node->left;

    while (node != NULL) {
        // Nodes are visited along going upward in the tree
        printf("%d\n", node->value);

        prev = node;
        node = node->right;

        if (prev->right_is_child) {
            // We're descending into tree

            if (node != NULL) {
                // Again, go to the leftmost leaf in this subtree
                while (node->left != NULL)
                    node = node->left;
            }
        }
        // else, we're climbing up in the tree
    }
}

Warning: I haven't run this code.

adaszko
  • 188
  • 5
0

This is the code written in Java:

public void inOrder() {
    Node<T> curr = root;
    boolean visited = false; //I haven't come across the node from which I came

    while (curr != null) {
        if (!visited && curr.left != null) { //Go to leftmost node
            curr = curr.left;
        } else {
            System.out.println(curr.head + " ");
            if (curr.right != null) { //I prioritize having childs than "threaded sons"
                curr = curr.right;
                visited = false;
            } else {
                curr = curr.rightThreaded;
                visited = true; //Means that I will come back to a node I've already looped, but now i'll print it, except if i'm at the last node
            }
        }
    }
}

Node is an inner class of ThreadedBinaryTree:

private static class Node<T> {
    private T head;
    private Node<T> left;
    private Node<T> right;
    private Node<T> leftThreaded;
    private Node<T> rightThreaded;

    public Node(T head, Node<T> leftThreaded, Node<T> rightThreaded) {
        this.head = head;
        this.leftThreaded = leftThreaded;
        this.rightThreaded = rightThreaded;
    }
}
gibarsin
  • 151
  • 4
  • 13