我一直试图找出为什么当我从测试人员类中调用它时,找不到我的countingLeaves方法。
我的编译器给了我错误TreeTester.java:25:错误:找不到符号 countLeaves(); ^ 符号:方法countLeaves() location:类TreeTester
public class BinarySearchTree
{
private Node root;
public BinarySearchTree()
{
root = null;
}
public void add(Comparable obj)
{
Node newNode = new Node();
newNode.data = obj;
newNode.left = null;
newNode.right = null;
if (root == null) { root = newNode; }
else { root.addNode(newNode); }
}
public boolean find(Comparable obj)
{
Node current = root;
while (current != null)
{
int d = current.data.compareTo(obj);
if (d == 0) { return true; }
else if (d > 0) { current = current.left; }
else { current = current.right; }
}
return false;
}
public void remove(Comparable obj)
{
Node toBeRemoved = root;
Node parent = null;
boolean found = false;
while (!found && toBeRemoved != null)
{
int d = toBeRemoved.data.compareTo(obj);
if (d == 0) { found = true; }
else
{
parent = toBeRemoved;
if (d > 0) { toBeRemoved = toBeRemoved.left; }
else { toBeRemoved = toBeRemoved.right; }
}
}
if (!found) { return; }
if (toBeRemoved.left == null || toBeRemoved.right == null)
{
Node newChild;
if (toBeRemoved.left == null)
{
newChild = toBeRemoved.right;
}
else
{
newChild = toBeRemoved.left;
}
if (parent == null) // Found in root
{
root = newChild;
}
else if (parent.left == toBeRemoved)
{
parent.left = newChild;
}
else
{
parent.right = newChild;
}
return;
}
Node smallestParent = toBeRemoved;
Node smallest = toBeRemoved.right;
while (smallest.left != null)
{
smallestParent = smallest;
smallest = smallest.left;
}
toBeRemoved.data = smallest.data;
if (smallestParent == toBeRemoved)
{
smallestParent.right = smallest.right;
}
else
{
smallestParent.left = smallest.right;
}
}
public void print()
{
print(root);
System.out.println();
}
private static void print(Node parent)
{
if (parent == null) { return; }
print(parent.left);
System.out.print(parent.data + " ");
print(parent.right);
}
public int countLeaves(Node node)
{
if(node == null)
return 0;
else if(node.left == null && node.right == null)
{
return 1;
}
else
{
return countLeaves(node.left) + countLeaves(node.right);
}
}
class Node
{
public Comparable data;
public Node left;
public Node right;
public void addNode(Node newNode)
{
int comp = newNode.data.compareTo(data);
if (comp < 0)
{
if (left == null) { left = newNode; }
else { left.addNode(newNode); }
}
else if (comp > 0)
{
if (right == null) { right = newNode; }
else { right.addNode(newNode); }
}
}
}
}
使用的测试人员类
public class TreeTester
{
public static void main(String[] args)
{
BinarySearchTree t = new BinarySearchTree();
t.add("D");
t.add("B");
t.add("A");
t.add("C");
t.add("F");
t.add("E");
t.add("I");
t.add("G");
t.add("H");
t.add("J");
t.remove("A"); // Removing leaf
t.remove("B"); // Removing element with one child
t.remove("F"); // Removing element with two children
t.remove("D"); // Removing root
t.print();
System.out.println("Expected: C E G H I J");
countLeaves(t);
}
}
答案 0 :(得分:0)
countLeaves需要一个Node,而不是BinarySearchTree。
答案 1 :(得分:0)
countLeaves需要Node
,而不是BinarySearchTree
。
您可以在BinarySearchTree中添加一个方法:
Node getRoot(){return root;}
Amd使用countLeaves(t.getRoot())
。