Java 在二叉树中计算叶子
我一直在试图弄清楚,当我从tester类调用countingLeaves方法时,为什么找不到它 我的编译器给了我一个错误TreeTester.java:25:error:找不到符号 数叶(); ^ 符号:方法countLeaves() 地点:TreeTester级Java 在二叉树中计算叶子,java,Java,我一直在试图弄清楚,当我从tester类调用countingLeaves方法时,为什么找不到它 我的编译器给了我一个错误TreeTester.java:25:error:找不到符号 数叶(); ^ 符号:方法countLeaves() 地点:TreeTester级 public class BinarySearchTree { private Node root; public BinarySearchTree() { root = null;
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); }
}
}
}
}
countLeaves需要一个节点,而不是BinarySearchTree。countLeaves需要一个
节点,而不是BinarySearchTree
您可以在BinarySearchTree中添加如下方法:
Node getRoot(){return root;}
Amd使用countLeaves(t.getRoot())
编译器正在您的TreeTester
类中查找方法countLeaves()
。。。但是它是在您的二进制搜索树类中定义的。因此,您需要调用t.countLeaves()
并将一个节点作为参数传递。也许可以改为在Er,t.countLeaves上问这个问题。我使用了t.countLeaves(getRoot());它仍然在我的treetester类中搜索。我相信我没有理解这个概念。@sippycupt.countLeaves(t.getRoot())
我得到了3的结果,所以我相信你已经解决了我的难题谢谢@不客气。你能给我一个最好的答案吗?
Node getRoot(){return root;}