Java-二进制家谱树-Can';找不到节点
我正在做一项作业,要求我输入并显示一棵族谱,首先将它转换为一棵二叉树——孩子在左边,兄弟姐妹在右边。 我了解树,遍历树,以及如何使用前、中、后顺序方法搜索某些节点 我已经编写了插入新节点、查找节点和打印整个树的代码,但是我的findNode方法无法正常工作。我需要它使用pre-order搜索树,并返回它要查找的节点。目前,递归方法一直到左下角(最低的子节点)和右下角(最低的同级节点),但它从不返回到我调用的原始节点,因此破坏了递归 以下是my FamilyTree和主要类的代码:Java-二进制家谱树-Can';找不到节点,java,tree,traversal,preorder,Java,Tree,Traversal,Preorder,我正在做一项作业,要求我输入并显示一棵族谱,首先将它转换为一棵二叉树——孩子在左边,兄弟姐妹在右边。 我了解树,遍历树,以及如何使用前、中、后顺序方法搜索某些节点 我已经编写了插入新节点、查找节点和打印整个树的代码,但是我的findNode方法无法正常工作。我需要它使用pre-order搜索树,并返回它要查找的节点。目前,递归方法一直到左下角(最低的子节点)和右下角(最低的同级节点),但它从不返回到我调用的原始节点,因此破坏了递归 以下是my FamilyTree和主要类的代码: public
public class FamilyTree
{
Node root;
// Initialize tree
public FamilyTree()
{
root = null;
}
// --------------------------------------------------------------
// This method inserts a new family member into the tree.
// It takes two parameters - the parent who the new node should
// be inserted under, and the name of the new member being added.
// --------------------------------------------------------------
public void insertNode(String par, String name)
{
Node parent, current;
Node newMember = new Node(name);
// If tree is empty, then the new member becomes the root
if(root == null)
root = newMember;
// If adding a sibling to the root, insert to root's right child
else if(par == "")
{
// Check if root's sibling is empty
if(root.rightChild == null)
root.rightChild = newMember;
// Traverse root's siblings until end, then insert at end
else
{
current = root;
while(current.rightChild != null)
current = current.rightChild;
current.rightChild = newMember;
}
}
else
{
// Find the parent where we will insert under
parent = findNode(par, root);
System.out.println("parent is = " + parent);
System.out.println("newMember is = " + newMember + "\n");
// If that parent doesn't exist, print error msg
if (parent == null)
System.out.println("Parent doesn't exist");
// If parent does exist, but has no left child,
// then the new member becomes left child
else if(parent.leftChild == null)
parent.leftChild = newMember;
// If parent already has a left child, then traverse
// to the end of it's left children and insert node
else
{
current = parent.leftChild;
while(current.rightChild != null)
current = current.rightChild;
current.rightChild = newMember;
}
}
}
// ------------------------------------------------------------
// This method recursively finds a node in the family tree,
// given the name of the node to look for, and the tree.
// It is run pre-order, and, if found, returns the node
// ------------------------------------------------------------
public Node findNode(String name, Node localTree)
{
Node current = localTree;
// Visit the node
if(current.name == name)
return current;
// Pre-order - go left
if(current.leftChild != null)
{
System.out.println("going left to " + current.leftChild);
return findNode(name, current.leftChild);
}
// Pre-order - go right
if(current.rightChild != null)
{
System.out.println("going right to " + current.rightChild);
return findNode(name, current.rightChild);
}
return null;
}
// ------------------------------------------------------------
// This method prints the family tree, given a parent name
// and a tree to print from. It will attempt to find the parent
// node with the given name, then print the entire tree
// (all children and grandchildren) from that point.
// ------------------------------------------------------------
public void printTree(String par, Node localTree)
{
Node parent, current;
// Find the parent to start printing from
parent = findNode(par, root);
System.out.println("parent= " + parent);
// If parent doesn't exist, print error msg
if (parent == null)
System.out.println(par + " doesn't exist.");
else
{
current = localTree;
System.out.println(current);
if(current.leftChild != null)
printTree(par, current.leftChild);
else if(current.rightChild != null)
printTree(par, current.rightChild);
}
}
public class Node
{
Node leftChild, rightChild;
String name;
public Node(String n)
{
leftChild = null;
rightChild = null;
name = n;
}
public String toString()
{
return name;
}
}
}
}问题在于您从搜索中过早返回的
:
public Node findNode(String name, Node localTree)
{
...
// Pre-order - go left
if(current.leftChild != null)
{
System.out.println("going left to " + current.leftChild);
return findNode(name, current.leftChild); // <===== HERE!
}
...
}
另外,在Java中,永远不要使用==
来比较字符串。它不会正常工作。对于字符串,始终使用String.equals(…)
。例如,请参见上面的代码,并且。您不应该current.name==name
,或者par==”
。对于String
s,始终使用equals()!哇!首先感谢您的快速回复!!!第二,这非常有效!我没有意识到return语句导致它崩溃。我认为递归总是“返回函数(param a,param b-1)”等等。
public Node findNode(String name, Node localTree)
{
...
// Pre-order - go left
if(current.leftChild != null)
{
System.out.println("going left to " + current.leftChild);
return findNode(name, current.leftChild); // <===== HERE!
}
...
}
public Node findNode(String name, Node localTree)
{
Node current = localTree;
// Visit the node
if(current.name.equals(name))
return current;
// Pre-order - go left
if(current.leftChild != null)
{
System.out.println("going left to " + current.leftChild);
Node nodeFound = findNode(name, current.leftChild);
if ( nodeFound != null ) {
// Only return from findNode if we have already found what we're looking for.
return nodeFound;
}
}
// Pre-order - go right
if(current.rightChild != null)
{
System.out.println("going right to " + current.rightChild);
return findNode(name, current.rightChild);
}
return null;
}