Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively generate an ASCII binary tree

Here is a picture of what my code needs to do.

Before Call:

            +----+
            | -9 |
            +----+
           /      \
          /        \
      +----+      +----+
      |  3 |      | 15 |
      +----+      +----+
     /           /      \
    /           /        \
+----+      +----+      +----+
|  0 |      | 12 |      | 24 |
+----+      +----+      +----+
           /      \
          /        \
      +----+      +----+
      |  6 |      | -3 |
      +----+      +----+

After Call:

            +----+
            | -9 |
            +----+
           /      \
          /        \
      +----+      +----+
      |  6 |      | 30 |
      +----+      +----+
     /           /      \
    /           /        \
+----+      +----+      +----+
|  0 |      | 24 |      | 48 |
+----+      +----+      +----+
           /      \
          /        \
      +----+      +----+
      | 12 |      | -3 |
      +----+      +----+

Basically this problem requires that I double all data values greater than 0 in a binary tree of integers. My code below does this for a few values but stops early. I am not sure how to fix this recursively. This is what my output looks like for the tree given above.

       overallRoot
         _[-9]_______________
        /                    \
    _[6]                 _____[30]
   /                    /         \
[0]                _[12]           [24]
                  /     \
               [6]       [-3]
public void doublePositives() {
    doublePositives(overallRoot);
}

private IntTreeNode doublePositives(IntTreeNode root) {
    if (root != null) {
        if (root.data > 0) {
            root.data = 2 * root.data;
        } else {
            root.left = doublePositives(root.left);
            root.right = doublePositives(root.right);
        }
    }
    return root;
}
like image 663
this1lefty Avatar asked Jun 10 '13 03:06

this1lefty


3 Answers

You still need to traverse the tree if you are doubling a node. Drop the else so that you always traverse. Also, I've removed the assignments because you are not changing the tree structure:

if(root != null) {
    if(root.data > 0) {
        root.data = 2 * root.data;
    }
    doublePositives(root.left);
    doublePositives(root.right);
}
like image 76
paddy Avatar answered Oct 18 '22 22:10

paddy


Looks like a logic issue - you will only double values of children of negative nodes:

if (root.data > 0) {
    root.data = 2 * root.data;
} else {
    root.left = doublePositives(root.left);
    root.right = doublePositives(root.right);
}

If the root value is positive - you never get to root.left & root.right. Something like this would be better:

if (root.data > 0) {
    root.data = 2 * root.data;
}
root.left = doublePositives(root.left);
root.right = doublePositives(root.right);
like image 41
mishik Avatar answered Oct 18 '22 22:10

mishik


Try this: You were making mistake in conditional statement. This should have been written like this. If root has data is positive - make it double, roger that! and out! In next step, proceed for left and right child nodes.

Additionally take a note of this, you don't need to return anything (other than void) in the recursive function doublePositives().

public void iWillDoit() {
    doublePositives(Root);
}

private void doublePositives(IntTreeNode root) {
    if (root != null) {
        if (root.data > 0) {
            root.data = 2 * root.data;
        }
        doublePositives(root.left);
        doublePositives(root.right);
    }
}
like image 3
kishoredbn Avatar answered Oct 18 '22 23:10

kishoredbn