2

I was reading through Introduction to algorithms i came across this problem about In-order Traversal of binary search tree without using a stack or recursion. Hint says to assume that testing of pointers for equality is a legitimate operation.I'm stuck finding the solution to this problem. Please give me some direction. I'm not looking for the code. Just give me the right direction.

Exact duplicate here

Community
  • 1
  • 1
Bazooka
  • 1,428
  • 4
  • 15
  • 24
  • possible duplicate of [Print binary tree in BFS fashion with O(1) space](http://stackoverflow.com/questions/8039097/print-binary-tree-in-bfs-fashion-with-o1-space) – amit Jan 25 '12 at 08:16
  • Maybe not a dupe, in here the OP asks for inorder, not BFS. I revert my claim. – amit Jan 25 '12 at 08:18
  • Thanks for the link anyways. There is another problem in the same book that asks to traverse a binary tree using only O(1) additional space. – Bazooka Jan 25 '12 at 08:23
  • Probably it all just depends on the representation of the tree. Is it mentioned somewhere the exact links you store for a node. If you store the father node, you will be able to go back your way (the only operation that you will have trouble thinking of if you implement ordinary while). If you don't store the father nodes, you will loose the structure of the tree and will not be able to go deeper in the tree. – Boris Strandjev Jan 25 '12 at 09:36
  • http://www.perlmonks.org/?node_id=600456 this here proved to be useful. – Bazooka Jan 25 '12 at 11:37
  • possible duplicate of [Binary search tree traversal that compares two pointers for equality](http://stackoverflow.com/questions/2340370/binary-search-tree-traversal-that-compares-two-pointers-for-equality) – jfs Jan 25 '12 at 18:53
  • possible duplicate of [Iterating over a Binary Tree with O(1) Auxiliary Space](http://stackoverflow.com/questions/791052/iterating-over-a-binary-tree-with-o1-auxiliary-space) – outis Jan 26 '12 at 04:46

3 Answers3

0

No stack nor recursion means you have to use pointers. Not giving you code nor the exact answer, since you asked not to :)

Think about how to explore the tree without using recursion: what would you need to do? What pointer(s) do you need to keep? Can a tree node have a pointer to the parent?

Hope it helps.

Savino Sguera
  • 3,522
  • 21
  • 20
  • i wrote the following pseudo code http://codepad.org/61KQNYKy Please tell me how can i improve it – Bazooka Jan 25 '12 at 14:39
0

We need a Threaded Binary Tree to do in-order Traversal without recursion / stack. Wiki says 'A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be null point to the inorder predecessor of the node'

So you are given a normal Binary Tree , Convert it into a Threaded Binary Tree which can be done using Morris Traversal. What you are going to do in Morris Traversal is to connect each node with its in-order successor. So while visiting a node ,Search for its in-order predecessor and let it be Pred. then make Pred->right=Current node and we have to revert back the changes too. You can better refer this http://www.geeksforgeeks.org/archives/6358 for a great explanation.

user1159517
  • 5,390
  • 8
  • 30
  • 47
0

Hello Parminder i have implemented your question in java.Please check it once

class InorderWithoutRecursion {
    public static void morrisTraversal(TreeNode root) {

        TreeNode current,pre;

    if(root == null)
        return; 

    current = root;
    while(current != null){
        if(current.left == null){
            System.out.println(current.data);
            current = current.right;
        }
        else {
      /* Find the inorder predecessor of current */
            pre = current.left;
            while(pre.right != null && pre.right != current)
            pre = pre.right;

      /* Make current as right child of its inorder predecessor */
        if(pre.right == null){
            pre.right = current;
            current = current.left;
        }

      /* Revert the changes made in if part to restore the original
        tree i.e., fix the right child of predecssor */
        else {
            pre.right = null;
            System.out.println(current.data);
            current = current.right;
        }
      } 
  } 
}
 public static void main(String[] args) {
        int[] nodes_flattened = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        TreeNode root = TreeNode.createMinimalBST(nodes_flattened);
        morrisTraversal(root);
    }
}

For TreeNode class below code will help you..

public class TreeNode {
    public int data;      
    public TreeNode left;    
    public TreeNode right; 
    public TreeNode parent;

    public TreeNode(int d) {
        data = d;
    }

    public void setLeftChild(TreeNode left) {
        this.left = left;
        if (left != null) {
            left.parent = this;
        }
    }

    public void setRightChild(TreeNode right) {
        this.right = right;
        if (right != null) {
            right.parent = this;
        }
    }

    public void insertInOrder(int d) {
        if (d <= data) {
            if (left == null) {
                setLeftChild(new TreeNode(d));
            } else {
                left.insertInOrder(d);
            }
        } else {
            if (right == null) {
                setRightChild(new TreeNode(d));
            } else {
                right.insertInOrder(d);
            }
        }
    }

    public boolean isBST() {
        if (left != null) {
            if (data < left.data || !left.isBST()) {
                return false;
            }
        }

        if (right != null) {
            if (data >= right.data || !right.isBST()) {
                return false;
            }
        }       

        return true;
    }

    public int height() {
        int leftHeight = left != null ? left.height() : 0;
        int rightHeight = right != null ? right.height() : 0;
        return 1 + Math.max(leftHeight, rightHeight);
    }

    public TreeNode find(int d) {
        if (d == data) {
            return this;
        } else if (d <= data) {
            return left != null ? left.find(d) : null;
        } else if (d > data) {
            return right != null ? right.find(d) : null;
        }
        return null;
    }

    private static TreeNode createMinimalBST(int arr[], int start, int end){
        if (end < start) {
            return null;
        }
        int mid = (start + end) / 2;
        TreeNode n = new TreeNode(arr[mid]);
        n.setLeftChild(createMinimalBST(arr, start, mid - 1));
        n.setRightChild(createMinimalBST(arr, mid + 1, end));
        return n;
    }

    public static TreeNode createMinimalBST(int array[]) {
        return createMinimalBST(array, 0, array.length - 1);
    }
}
coder_15
  • 171
  • 1
  • 7
  • thanks @venkat below is a link to the pseudo code i wrote that i think might me correct. http://codepad.org/61KQNYKy an this does not use the threaded binary tress. – Bazooka Jan 27 '12 at 07:33
  • 1
    Hello Parminder check this [link](http://stackoverflow.com/questions/2595002/can-i-do-inorder-traversal-of-a-binary-tree-without-recursion-and-stack) – coder_15 Jan 27 '12 at 11:06
  • Hi Venkat. This is essentially what i tried to implement but this of course is a much more elegantly written. Thanks for the link. – Bazooka Jan 27 '12 at 11:57