我想实现BinarySearchTree,目前我已经走到了这一步:
我的问题是我应该如何完成第二个文件。
这是第一个文件:
package u7a3;
/**
* generic binary search-tree with integer keys.
* Empty trees are encoded as null references.
*
* @param <T> The type of the things which are managed in the binary
* search tree
*/
public class BinarySearchTree<T> {
/**
* The key by which the thing is refered to. Must be unique.
*/
public int key;
/**
* The thing itself.
*/
public T thing;
/**
* The left sub-tree
*/
public BinarySearchTree<T> left;
/**
* The right sub-tree
*/
public BinarySearchTree<T> right;
/**
* Create a new binary search tree without children.
* @param key the key by which the thing is refered to
* @param thing the new thing
*/
public BinarySearchTree(int key, T thing)
{
this.key = key;
this.thing = thing;
this.left = null;
this.right = null;
}
/**
* Create a new binary search tree
* @param key the key by which the thing is refered to
* @param thing the thing which is managed by the new binary search tree
* @param left the left sub-tree of the new binary search tree
* @param right the right sub-tree of the new binary search tree
*/
public BinarySearchTree(int key, T thing, BinarySearchTree<T> left, BinarySearchTree<T> right)
{
this.key = key;
this.thing = thing;
this.left = left;
this.right = right;
}
}
第二个文件是我仍然需要分别完成编辑的地方:
package u7a3;
import java.util.ArrayList;
class BinarySearchTreeUtils<T> implements IBinarySearchTreeUtils<T> {
public int key;
public T thing;
public BinarySearchTree<T> left;
public BinarySearchTree<T> right;
public BinarySearchTreeUtils()
{
this.key = key;
this.thing = thing;
this.left = null;
this.right = null;
}
public int height(BinarySearchTree<T> tree) //Need help
{
//if (tree.left == null || tree.right == null) return 0;
/*else if (tree.left == null) return height(tree.right);
else if (tree.right == null) return height(tree.left);
else*/ return 0;
}
public boolean isLeaf(BinarySearchTree<T> tree)
{
return tree.left == null && tree.right == null;
}
public boolean hasOneChild(BinarySearchTree<T> tree) //Correct?
{
//return (isLeaf(tree.left) == true || isLeaf(tree.right) == true) && (tree.left == null || tree.right == null);
return isLeaf(tree.left) == true && tree.right == null || isLeaf(tree.right) == true && tree.left == null;
}
public ArrayList<T> preOrder(BinarySearchTree<T> tree) //Need help
{
return new ArrayList<T>();
}
public ArrayList<T> inOrder(BinarySearchTree<T> tree) //Need help
{
return new ArrayList<T>();
}
public ArrayList<T> postOrder(BinarySearchTree<T> tree) //Need help
{
return new ArrayList<T>();
}
public BinarySearchTree<T> insert(BinarySearchTree<T> tree, int key, T thing) //Need help
{
return new BinarySearchTree(0, this);
}
public T find(BinarySearchTree<T> tree, int key) //Need help
{
BinarySearchTree<T> treeleft = tree.left;
BinarySearchTree<T> treeright = tree.right;
if (isLeaf(tree) == true && tree.key != key) return null;
//else if((treeleft.key) >= key) return find(tree.left, key);
//else if((treeright.key) <= key) return find(tree.right, key);
//else if (treeleft.key == key) return treeleft.thing;
//else if (treeright.key == key) return treeright.thing;
//else*/ //return tree.thing;
else return tree.thing;
}
public BinarySearchTree<T> remove(BinarySearchTree<T> tree, int key) //Need help
{
return new BinarySearchTree(0, this);
}
}
这是第三个文件:
package u7a3;
import java.util.ArrayList;
/**
* Utility functions for binary search trees containing generic things
*
* @param <T>
* The type of the things which are managed in the binary search tree
*/
public interface IBinarySearchTreeUtils<T> {
/**
* Determines the height of a tree.
*
* The empty tree has height zero. The tree consisting only of the root has
* height one.
*
* @param tree
* the tree in question
* @return the height of the given tree
*/
public int height(BinarySearchTree<T> tree);
/**
* Checks if a tree is actually a leaf node.
*
* A tree is a leave node if and only if (iff) it has no children.
*
* @param tree
* the tree in question. May not be null.
* @return true iff the given tree is a leaf node.
*/
public boolean isLeaf(BinarySearchTree<T> tree);
/**
* Checks if a tree has only one child.
*
* @param tree
* the tree in question
* @return true iff the given tree has only one child.
*/
public boolean hasOneChild(BinarySearchTree<T> tree);
/**
* Returns the things of a tree in pre-order.
*
* @param tree
* the tree containing the things
* @return an ArrayList of the things of the given tree, sorted in pre-order.
*/
public ArrayList<T> preOrder(BinarySearchTree<T> tree);
/**
* Returns the things of a tree in in-order.
*
* @param tree
* the tree containing the things
* @return an ArrayList of the things of the given tree, sorted in in-order.
*/
public ArrayList<T> inOrder(BinarySearchTree<T> tree);
/**
* Returns the things of a tree in post-order.
*
* @param tree
* the tree containing the things
* @return an ArrayList of the things of the given tree, sorted in post-order.
*/
public ArrayList<T> postOrder(BinarySearchTree<T> tree);
/**
* Inserts an thing into a tree
*
* If an thing with the given key already exists in the tree it is
* overwritten.
*
* @param tree
* the tree into which the thing is inserted. After the call this
* tree should not be used any more. Use the returned tree
* instead.
* @param key
* the key of the thing
* @param thing
* the thing which is inserted into the tree
* @return a new tree with the given thing inserted.
*/
public BinarySearchTree<T> insert(BinarySearchTree<T> tree, int key, T thing);
/**
* Finds an thing in a tree.
*
* @param tree
* the tree in which the thing is searched for.
* @param key
* the key of the thing which is searched
* @return the thing if it exists in the tree, and null otherwise.
*/
public T find(BinarySearchTree<T> tree, int key);
/**
* Removes a thing from a tree.
*
* If the thing is in a leaf node this node is simply removed
* If the thing is in node with one child this node is replaced by its child.
* Otherwise the removed node is replaced by the smallest node of its right subtree.
*
* @param tree
* the tree from which the thing is removed. After this call this
* tree should not be used any more. Use the returned tree
* instead.
* @param key
* the key of the thing
* @return either a new tree with the given thing removed or the simply the
* given tree if the thing could not be found.
*/
public BinarySearchTree<T> remove(BinarySearchTree<T> tree, int key);
}
第四个文件是主类,但对我的问题并不重要:
/**
* Main class of the Java program.
*
*/
public class Main {
public static void main(String[] args) {
System.out.println("-- Binary Search --");
/* you can make function calls from here*/
}
}
这是第五个也是最后一个应该没问题的文件:
package u7a3;
public class UtilsFactory {
public static IBinarySearchTreeUtils<String> create()
{
// TODO
return new BinarySearchTreeUtils();
}
}
答案 0 :(得分:0)
方法hasOneChild应该是这样的:
summarise