Java - 二元家谱 - 找不到节点
Java - Binary Family Tree - Can't Find Node
我正在做一项作业,要求我输入和显示家谱,方法是首先将其转换为二叉树 - child 在左边,兄弟姐妹在右边。
我了解树、遍历树以及如何使用 pre-、in- 和 post-order 方法搜索某些节点。
我已经编写了插入新节点、查找节点和打印整棵树的代码,但是我的 findNode 方法无法正常工作。我需要它使用 pre-order 和 return 它正在寻找的节点来搜索树。目前,递归方法一直到左下角(最低child),最低child的右下角(最低兄弟),但它永远不会回到原始节点我打电话给 - 因此打破了递归。
这是我的 FamilyTree 和 Main 的代码 类:
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 class Main
{
public static void main (String[] args)
{
FamilyTree myTree = new FamilyTree();
myTree.insertNode("", "Grandma Marx");
myTree.insertNode("", "Great-Aunt Peggie");
myTree.insertNode("", "Great-Aunt Katherine");
myTree.insertNode("Grandma Marx", "Aunt Sarah");
myTree.insertNode("Grandma Marx", "Aunt Tory");
myTree.insertNode("Grandma Marx", "Uncle Frank");
myTree.insertNode("Grandma Marx", "Uncle Charles");
myTree.insertNode("Grandma Marx", "Mom");
myTree.insertNode("Aunt Sarah", "Morgan");
myTree.insertNode("Aunt Sarah", "Tommy");
myTree.insertNode("Aunt Sarah", "Austin");
myTree.insertNode("Aunt Sarah", "Luke");
myTree.insertNode("Aunt Tory", "Tim");
myTree.insertNode("Mom", "Barret");
myTree.insertNode("Mom", "Jeremy");
myTree.insertNode("Mom", "Elliot");
//myTree.printTree("Grandma Marx", myTree.findNode("Grandma Marx", myTree.root));
}
}
问题出在您过早 return
搜索:
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!
}
...
}
这导致函数在遍历完左子树后结束,即使结果是null
,即没有找到节点。
这样的事情怎么样:
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;
}
此外,在 Java 中,您永远不应使用 ==
来比较字符串。它不会正常工作。对于字符串,始终使用 String.equals(...)
。例如上面的代码,this SO question.
我正在做一项作业,要求我输入和显示家谱,方法是首先将其转换为二叉树 - child 在左边,兄弟姐妹在右边。 我了解树、遍历树以及如何使用 pre-、in- 和 post-order 方法搜索某些节点。
我已经编写了插入新节点、查找节点和打印整棵树的代码,但是我的 findNode 方法无法正常工作。我需要它使用 pre-order 和 return 它正在寻找的节点来搜索树。目前,递归方法一直到左下角(最低child),最低child的右下角(最低兄弟),但它永远不会回到原始节点我打电话给 - 因此打破了递归。
这是我的 FamilyTree 和 Main 的代码 类:
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 class Main
{
public static void main (String[] args)
{
FamilyTree myTree = new FamilyTree();
myTree.insertNode("", "Grandma Marx");
myTree.insertNode("", "Great-Aunt Peggie");
myTree.insertNode("", "Great-Aunt Katherine");
myTree.insertNode("Grandma Marx", "Aunt Sarah");
myTree.insertNode("Grandma Marx", "Aunt Tory");
myTree.insertNode("Grandma Marx", "Uncle Frank");
myTree.insertNode("Grandma Marx", "Uncle Charles");
myTree.insertNode("Grandma Marx", "Mom");
myTree.insertNode("Aunt Sarah", "Morgan");
myTree.insertNode("Aunt Sarah", "Tommy");
myTree.insertNode("Aunt Sarah", "Austin");
myTree.insertNode("Aunt Sarah", "Luke");
myTree.insertNode("Aunt Tory", "Tim");
myTree.insertNode("Mom", "Barret");
myTree.insertNode("Mom", "Jeremy");
myTree.insertNode("Mom", "Elliot");
//myTree.printTree("Grandma Marx", myTree.findNode("Grandma Marx", myTree.root));
}
}
问题出在您过早 return
搜索:
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!
}
...
}
这导致函数在遍历完左子树后结束,即使结果是null
,即没有找到节点。
这样的事情怎么样:
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;
}
此外,在 Java 中,您永远不应使用 ==
来比较字符串。它不会正常工作。对于字符串,始终使用 String.equals(...)
。例如上面的代码,this SO question.