出力はすべてかなり異なるため、修正しようとすると問題が発生します。
ここに1つがあります:(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)
もう1つ、これは機能します:
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;
}