简体   繁体   中英

Im having A problem in Keeping track of the overAllRoot, my code is at the end! im facing an infinite loop

Write a method combineWith that could be added to the IntTree class. The method accepts another binary tree of integers as a parameter and combines the two trees into a new third tree which is returned. The new tree's structure should be a union of the structures of the two original trees. It should have a node in any location where there was a node in either of the original trees (or both). The nodes of the new tree should store an integer indicating which of the original trees had a node at that position (1 if just the first tree had the node, 2 if just the second tree had the node, 3 if both trees had the node). For example, suppose IntTree variables t1 and t2 have been initialized and store the following trees:

t3

               +---+
               | 3 |
           ___ +---+ ___
         /               \
       +---+               +---+
       | 3 |               | 3 |
       +---+               +---+
      /     \             /     \

 +---+     +---+     +---+     +---+
 | 3 |     | 1 |     | 2 |     | 3 |  
+---+      +---+     +---+     +---+
     /               \
 +---+               +---+
 | 1 |               | 2 |
 +---+               +---+

You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class nor create any data structures such as arrays, lists, etc. Your method should not change the structure or contents of either of the two trees being compared.

public IntTree combineWith(IntTree t2)

{
    IntTree t3 = new IntTree();

    while(this.overallRoot != null && t2.overallRoot!= null)
    {
         t3.overallRoot = new IntTreeNode(3);

        //  for the 3 combination
        if(this.overallRoot.left != null && t2.overallRoot.left != null)
        {
            t3.overallRoot.left = new IntTreeNode(3) ;
        }

         if(this.overallRoot.right != null && t2.overallRoot.right != null)
        {
            t3.overallRoot.right = new IntTreeNode(3) ;
        }

        // for the 1 combination
         if(this.overallRoot.left != null && t2.overallRoot.left == null)
        {
            t3.overallRoot.left = new IntTreeNode(1) ;
        }

         if(this.overallRoot.right != null && t2.overallRoot.right == null)
        {
            t3.overallRoot.right = new IntTreeNode(1) ;
        }


        // for the 2 combination
        if(this.overallRoot.left == null && t2.overallRoot.left != null)
        {
            t3.overallRoot.left = new IntTreeNode(2) ;
        }

         if(this.overallRoot.right == null && t2.overallRoot.right != null)
        {
            t3.overallRoot.right = new IntTreeNode(2) ;
        }




    }
    return t3;
}

So the crux of your problem is that you're looping on conditions that expect to somehow change, but never change:

this.overallRoot != null && t2.overallRoot!= null

If this is true the first iteration through the loop, it is true for every iteration of the loop (hence your infinite loop)!

Since this is homework I won't give a direct answer, but you should consider recursing over both trees, where you compare each subtree. Your function as written should adequately support the base case, your challenge now will be calling the recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM