[英](Java) Alphabetical binary tree class doesn't work as expected
我需要為作業開發自己的二進制搜索樹類。 應該對包含兩個字符串,用戶名和密碼的數據類(用戶)進行排序,然后再通過username變量允許它針對文本文件搜索它。 我使用TreeSet成功測試了文件分配,發現如果使用整個搜索文件,搜索將返回約300個正匹配項。 根據先前的工作,我認為我的二叉樹類可以工作,但是不管它永遠不會通過其搜索功能找到任何匹配項,即使我知道這是不正確的。
一些未在此處顯示的代碼信息-
這是(新)的grow()方法;
public BinTree grow(BinTree root, BinTree newNode)
{
if (root == null)
return newNode;
if (newNode.getUserName().compareTo(root.getUserName()) > 0)
{
if (root.right == null)
root.right = newNode;
else
root.right.grow(root.right, newNode);
}
else
{
if (root.left == null)
root.left = newNode;
else
root.left.grow(root.left, newNode);
}
return root;
}
這是(新的)search()方法;
public boolean search(BinTree r, String s) //search for a username, case sensitive
{
if (r.getUserName().compareTo(s) == 0)
return true;
else if (r.left != null && r.getUserName().compareTo(s) < 0)
return r.left.search(r.left, s);
else if (r.right != null)
return r.right.search(r.right, s);
return false;
}
就像我說的那樣,我之前做過一個簡單的二叉樹(只包含單個int值,不包含自定義數據類),並且嘗試了多種方式編寫這兩種方法,但是我覺得其中有一個關鍵的難題只是沒有得到。 在此先感謝您的幫助。
更新:感謝@Diasiare和@ c0der指出了一個明顯的問題,即我的代碼在返回和左/右方面存在錯別字。 我接受了這一點,並更改了上面的代碼以反映出來。 運行后,該程序似乎仍然無法運行。 然后,我編寫了這個show()方法來打印存儲在樹的用戶中的所有用戶名。
public void show(BinTree r)
{
if (r != null)
{
show(r.left);
System.out.println(r.getUserName());
show(r.right);
}
} // show
當我在更新和編譯所有其他內容后調用它時,實際上確實表明該列表已按字母順序填充了用戶名。 這是輸出的一小段(有很多行)
ted@wisler.com
teddy@borglum.com
teddy@winkey.com
-->teodoro@harkin.com<--
teodoro@stranford.com
teodoro@talaska.com
teodoro@willette.com
tera@norise.com
tera@strevels.com
terence@lathrum.com
terence@morocco.com
terence@neidig.com
terence@rabago.com
teresa@chauvin.com
teresa@dockus.com
我用箭頭挑出的那個是我在搜索.txt文件中手動搜索並找到的一個。 總而言之,利用這一新信息,我確定A)grow()正常工作,因為它以字母順序填充文件中的樹,而B)search()應該至少返回一個匹配項。 因此,我正在假設問題仍然出在search()上。
UPDATE2:這是我正在為感興趣的人調用search()的上下文。
try
{
BufferedReader fromPasswords = new BufferedReader(new FileReader("passwordInput.txt"));
while ((line = fromPasswords.readLine()) != null)
{
System.out.println(line);
if(b.search(b, line))
{
matches++;
}
}
fromPasswords.close();
}
catch (Exception e)
{
System.out.println("Error while searching tree: " + e);
System.exit(0);
}
您的主要問題是搜索功能不會返回遞歸調用的結果,它應讀取如下內容:
public boolean search(BinTree r, String s) //search for a username, case sensitive
{
if (r.getUserName().compareTo(s) == 0)
return true;
else if (r.left != null && r.getUserName().compareTo(s) < 0)
return r.left.search(r.left, s);
else if (r.right != null)
return r.right.search(r.right, s);
return false;
}
我還要指出:
if (root == null)
root = newNode;
沒什么意義。 如果root為null
,則最終將newNode
作為其自身的子級插入。 另外,您的方法無法通信該樹為空。 我建議您拋出NullPointerException
,或使該方法返回新樹。 如果將來希望它成為平衡樹,這將有所幫助,在這種情況下,根節點可能會更改。 在這種情況下,解決方案如下所示:
public BinTree grow(BinTree root, BinTree newNode)
{
if (root == null)
return newNode;
if (newNode.getUserName().compareTo(root.getUserName()) > 0)
{
if (root.right == null)
root.right = newNode;
else
root.right.grow(root.right, newNode);
}
else
{
if (root.left == null)
root.left = newNode;
else
root.left.grow(root.left, newNode);
}
return root;
}
另外,作為c0der注釋行root.left.grow(root.right, newNode);
應該可能是root.left.grow(root.left, newNode);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.