2013-10-13 13 views
0

輸出結果都非常不同,因此在嘗試修復它時會造成一些麻煩。我的私人二進制樹int列表給我錯誤的輸出,我該如何解決它?

這裏是一個:(0從填充停止列表)

run: 
Enter an integer to be inserted or 0 to quit: 
1 
2 
3 
0 
"printing P:" 
1 
2 
3 
"now copying:" 
1 
"done printing Q" 
BUILD SUCCESSFUL (total time: 7 seconds) 

這裏是另一個:

run: 
Enter an integer to be inserted or 0 to quit: 
1 
2 
3 
-4 
-5 
-6 
0 
printing P 
-6 
-5 
-4 
1 
2 
3 
now copying 
-6 
-5 
-4 
1 
done printing Q 
BUILD SUCCESSFUL (total time: 13 seconds) 

多個,其中工程:

run: 
Enter an integer to be inserted or 0 to quit: 
6 
5 
4 
0 
printing P 
4 
5 
6 
now copying 
4 
5 
6 
done printing Q 
BUILD SUCCESSFUL (total time: 11 seconds) 

所以只有它複製輸入的最小數字,除非有負數,在這種情況下,它複製這些數字,但只複製最低的正數進入。負數工作正常,只是按升序輸入的正數不會複製。

這裏是類:

public class Intcoll6 
    { 
    private int howmany; 
private btNode c; 

public Intcoll6() 
    { 
    howmany = 0; 
    c = null; 
    } 
//we have to use this private class 
private class btNode 
    { 
    int info; 
    btNode left; 
    btNode right; 

    public btNode() 
     { 
     info = 0; 
     left = null; 
     right = null; 
     } 
    } 
public boolean belongs(int i) 
    { 
    boolean result = false; 
    btNode p = c; 
    while ((p != null) && (p.info != i)) 
     { 
     if (i < p.info) 
      { 
      p = p.left; 
      } 
     if (p.info > i) 
      { 
      p = p.right; 
      } 
     if (p != null) 
      { 
      result = true; 
      } 
     } 
    return result; 
    } 
//I believe the problem is with this method 
public void insert(int i) 
    { 
    btNode p = c; 
    btNode pred = null; 
    while ((p != null) && (p.info != i)) 
     { 
     pred = p; 
     if (i < p.info) 
      { 
      p = p.left; 
      } else if (i > p.info) 
      { 
      p = p.right; 
      } 
     } 
    if (p == null) 
     { 
     howmany = howmany + 1; 
     p = new btNode(); 
     if (pred != null) 
      { 
      if (i < pred.info) 
       { 
       pred.left = p; 
       } 
      if (i > pred.info) 
       { 
       pred.right = p; 
       } 
      p.info = i; 
      } 
     if (pred == null) 
      { 
      c = p; 
      p.info = i; 
      } 

     } 
    } 

public void omit(int i) 
    { 
    btNode p = c; 
    btNode pred = null; 
    while ((p != null) && (p.info != i)) 
     { 
     pred = p; 
     if (i < p.info) 
      { 
      p = p.left; 
      } 
     if (i > p.info) 
      { 
      p = p.right; 
      } 
     } 
    if (p != null) 
     { 
     howmany--; 
     if (pred != null) 
      { 
      if ((p.left == null) && (p.right == null)) 
       { 
       if (p.info > pred.info) 
        { 
        pred.right = null; 
        } else 
        { 
        pred.left = null; 
        } 
       } else if ((p.left != null) && (p.right == null)) 
       { 
       if (p.info > pred.info) 
        { 
        pred.right = p.left; 
        } else 
        { 
        pred.left = p.left; 
        } 
       } else if ((p.right != null) && (p.left == null)) 
       { 
       if (p.info < pred.info) 
        { 
        pred.left = p.right; 
        } else 
        { 
        pred.right = p.right; 
        } 
       } else if ((p.left != null) && (p.right != null)) 
       { 
       if (p.info > pred.info) 
        { 
        btNode q = p; 
        btNode q1 = pred; 
        while (q != null) 
         { 
         q1 = q; 
         q = q.left; 
         } 
        p.info = q.info; 
        q1.left = null; 

        } 
       if (p.info < pred.info) 
        { 
        btNode q = p; 
        btNode q1 = p; 
        while (q != null) 
         { 
         q1 = q; 
         q = q.right; 
         } 
        p.info = q.info; 
        q1.right = null; 
        } 
       } 
      } 
     if (pred == null) 
      { 
      if ((p.left == null) && (p.right == null)) 
       { 
       c = null; 
       } else if ((p.left != null) && (p.right == null)) 
       { 
       c = p.left; 
       } else if ((p.left == null) && (p.right != null)) 
       { 
       c = p.right; 
       } else if ((p.right != null) && (p.left != null)) 
       { 
       btNode q = p; 
       btNode q1 = pred; 
       while (q != null) 
        { 
        q1 = q; 
        q = q.right; 
        } 
       p.info = q.info; 
       q1.right = null; 

       } 
      } 
     } 
    } 
private btNode copyTree(btNode tree) 
{ 
    btNode result = null; 
    if(tree != null) 
    { 
     btNode L; 
     btNode R; 
     L = copyTree(tree.left); 
     R = copyTree(tree.right); 
     result = new btNode(); 
     result.info = tree.info; 
     result.left = L; 
     tree.right = R; 
    } 
    return result; 
} 
public void copy (Intcoll6 obj) 
{ 
    if(this != obj) 
    { 
    howmany = obj.howmany; 
    c = copyTree(obj.c); 
    } 
} 
private static void printtree(btNode t) 
    { 
    if (t != null) 
     { 
     printtree(t.left); 
     System.out.println(t.info); 
     printtree(t.right); 
     } 
    } 
public void print() 
    { 
    printtree(c); 
    } 
public boolean equals(Intcoll6 obj) 
    { 
    boolean result = (howmany == obj.howmany); 
    btNode p = c; 
    btNode q = obj.c; 
    while ((p != null) && (q != null)) 
     { 
     btNode pred = p; 
     if ((p.info == q.info)) 
      { 
      if (p.info >= pred.info) 
       { 
       p = p.right; 
       q = q.right; 
       } 
      } 
     if ((p == null) && (q == null)) 
      { 
      result = true; 
      } 
     } 
    return result; 
    } 
    } 

司機:

import intcoll6.Intcoll6; 
import java.util.*; 
import static intcoll6.Intcoll6Client.SENTINEL; 
public class Driver6 
    { 
int value; 
public static final int SENTINEL = 0; 
public static void main(String[] args) 
    { 
    System.out.println("Enter an integer to be inserted or 0 to quit:"); 
    int value; 
    Scanner keyboard = new Scanner(System.in); 
    value = keyboard.nextInt(); 
    Intcoll6 P = new Intcoll6(); 
    while (value != SENTINEL) 
     { 
     P.insert(value); 
     value = keyboard.nextInt(); 
     } 
    System.out.println("printing P"); 
    P.print(); 
    System.out.println("now copying"); 
    Intcoll6 Q = new Intcoll6(); 
    Q.copy(P); 
    Q.print(); 
    System.out.println("done printing Q"); 
    } 
    } 

這裏是正確的copyTree方法:

private btNode copyTree(btNode tree) 
     { 
    btNode test = null; 
    if (tree != null) 
     { 
     test = new btNode(); 
     test.info = tree.info; 
     test.left = copyTree (tree.left); 
     test.right = copyTree(tree.right); 
     } 
    return test; 
    } 
+0

您是否嘗試過使用調試器或某些日誌語句來查看發生了什麼?你自己解決這個問題會更有教育意義。在編程工作中,你不能總是要求你的同事爲你找到你的錯誤。 –

+0

我有麻煩,因爲我試圖調試器,也許是因爲它是一個專用分類,它並沒有給我在樹中每個節點的值,就在最近的INT,所以我實在看不出有什麼繼續。 我知道我不能要求我的同事在工作中,我只是卡住了(一直試圖天)。如果有人甚至可以告訴我問題在哪裏,但不一定如何解決,這也會有所幫助。 – HelloKomputer

+0

那麼,這是學習調試的好時機。也是學習如何撰寫評論的好時機。 –

回答

0

你從來沒有在您的副本聲明中正確設置正確的樹。
話說tree.right = R是指派權樹的副本被髮送給該方法的參數。
你應該複製樹局部變量結果。你已經退出此方法後

一切樹權缺失,所以它永遠不會打印休息,因爲它只知道樹的左側。

private btNode copyTree(btNode tree) 
{ 
    btNode result = null; 
    if(tree != null) 
    { 
     btNode L; 
     btNode R; 
     L = copyTree(tree.left); 
     R = copyTree(tree.right); 
     result = new btNode(); 
     result.info = tree.info; 
     result.left = L; 
     //wrong//--> tree.right = R; 
     //Should be// -- > result.right = R; 
    } 
    return result; 
} 
+0

它只複製最低的數字,因爲在樹中,較低的數字在左側,而您只複製樹的左側。如果你說4 5 6而不是6 5 4,你很可能只會得到4 – dannyRods

相關問題