I have a txtfile with a list of unsorted numbers. I took the list of numbers and made an array. So now i m trying to make a tree with those numbers and sort them out based off of what on the left and right. Right now its printing, but not sorted. I m pretty sure the tree isn t being made right but I m not sure how to fix it. I also want to keep track of any duplicates. In other words I don t want to print out any duplicates but instead just keep track of how many there are. Any help or advice is appreciated. Thanks in advance. -- I pass the array of numbers in method: dealArray(), which then converts it to a int. From there those # s are passed in findDuplicate() which I m not sure I should be doing or not.
BigTree类:
public class bigTree {
int data; int frequency;
bigTree Left, Right;
public bigTree makeTree(int x) {
bigTree p;
p = new bigTree();
p.data = x;
p.Left = null;
p.Right = null;
return p;
}
public void setLeft(bigTree t, int x) {
if (t.Left != null) {
System.out.println("Error");
}
else {
bigTree q;
q = t.Left;
q = makeTree(x);
}
}
public void setRight(bigTree t, int x) {
if (t.Right != null) {
System.out.println("Error");
} else {
bigTree q;
q = t.Right;
q = makeTree(x);
}
}
public void findDuplicate(int number) {
bigTree tree, p, q;
frequency = 0;
tree = makeTree(number);
//while (//there are still # s in the list) { //1st while()
p = tree;
q = tree;
while (number != p.data && q != null) { //2nd while()
p = q;
if (number < p.data ) {
q = q.Left;
} else {
q = q.Right;
}
} //end of 2nd while()
if (number == p.data) {
Sort(p);
//System.out.println( p.data );
frequency++;
}
else {
if (number < p.data) {
setLeft(p,number);
}
else {
setRight(p, number);
}
}
//} // End of 1st while()
}
public void Sort(bigTree t) {
if (t.Left != null) {
Sort(t.Left);
}
System.out.println(t.data);
if (t.Right != null) {
Sort(t.Right);
}
//Possible print
}
public void dealArray(String[] x) {
int convert;
for (int i = 0; i < x.length; i++){
convert = Integer.parseInt(x[i]);
findDuplicate(convert);
// System.out.println(x[i]);
}
}