-2

如何查找节点中包含的内容?我的意思是如何进行比较?我要做什么不同的事情来完成这项工作?我知道,或者至少我认为我知道,我必须将搜索方法传递给字符串和节点。

if (value == root)
        return root;
4

2 回答 2

1

In your tag you say it is a binary tree (altough this does not mean it is a sorted tree).

What you can do in the case of a sorted tree:

public String searchTree (Node n, String searchVal)
{
   if (n.isEmpty())//no more children
   {
      return null;
   }
   else if (n.root().toString().equals(searchVal) //we found it
   {
      return n.root();
   }
   else if (searchVal < n.root().toString()) //search left child
   {
      return searchTree(n.leftChild(),searchVal);
   }
   else //search right child
   {
      return searchTree(n.rightChild(),searchVal);
   }
}

This is the basic code but needs to be refined for you Tree class In case the tree is not sorted, the last if-statements can be combined (first check left tree; if that is null return the search on the right tree, otherwise return the search on the left tree)

Kind regards,
Héctor van den Boorn

于 2013-03-31T01:42:00.257 回答
0

treeSet.contains(对象)

其中 treeSet 是 TreeSet 的实例,而 object 是正确覆盖 equals() 和 hashcode() 的实例。

于 2013-03-31T11:48:56.940 回答