まず、これは宿題なので、そこに出してください。
特定の方法で二分探索木を実装することになっています。
void 挿入 (文字列)、ブール型の削除 (文字列)、およびブール型の検索 (文字列)。
insert メソッドと find メソッドのプログラミングとテストは成功しましたが、remove に問題があります。
私のプログラムで何が起こっているかというと、削除は実際にはツリーから何も削除していないということです。これは、現在のノードのローカル作成のみを参照しているためだと思いますが、間違っている可能性があります。テストする必要があるさまざまなケースのロジックを実装できると思います (2 つの子ケースを持つノードを削除するのに助けが必要かもしれませんが、概念的に理解していると思います)適切にツリー
current = null; // case
これが私がこれまでに得たものです:
public boolean remove(String title)
{
return remove(root, title);
}
private boolean remove(BSTNode current, String title)
{
if (current == null)
{
return false;
}
if (current.data == title)
{
if (current.left_child !=null && current.right_child != null)
{
return true; // returning true since I haven't finished writing this case
}
else if (current.left_child == null && current.right_child == null)
{
current = null; // this should remove the node from tree but it doesn't
return true;
}
else if (current.left_child != null && current.right_child == null)
{
current = current.left_child; // don't think this is right
return true;
}
else if (current.right_child != null && current.left_child == null)
{
current = current.right_child; // not sure about this
return true;
}
}
root = current;
if (title.compareToIgnoreCase(current.data) == -1)
{
return remove(current.left_child, title);
}
else
{
return remove(current.right_child, title);
}
}
どんな知識でも大歓迎です。