1

所以我很难解决我正在解决的当前问题。基本上,我需要在基于数组的二叉搜索树中添加一个元素。根据我的文字,它类似于 compareTo 方法。我什至不确定该朝哪个方向前进。在 OOP 方面,我完全是个菜鸟,因此将不胜感激。

package lab9;

public class BinarySearchTreeArray<E> {

    Entry<E> [] tree;
    Entry<E> root;
    int size;

    public BinarySearchTreeArray()
    {
        tree = null;
        size = 0;
    }

    public int size()
    {
        return size;
    }

    public boolean contains(Object obj)
    {
        Entry<E> temp = root;
        int comp;

        if (obj == null)
            throw new NullPointerException();

        while (obj != null)
        {
            comp = ((Comparable)obj).compareTo (temp.element);
            if (comp == 0)
                return true;
            else if (comp < 0)
                temp = temp.left;
            else
                temp = temp.right;
        }//while
        return false;
    }//contains method

    /*
     * From the text:
     * The definition of the add (E element) method is only a little more
     * complicated than the definition of contains (Object obj).  Basically,
     * the add method starts at the root and branches down the tree 
     * searching for the element; if the search fails, the element is
     * inserted as a leaf.
     */

    public void add(E e)
    {
        Entry<E> node = new Entry<E>(e);

        if (tree[parent] == null)
        {
             tree[0] = node;
             size++;
        }
        else
        {
            tree[1] = node;
            size++;
        }
    }//add method

/****************************************************************/
    protected static class Entry<E>
    {
        private E element;
        private Entry<E> parent, left, right;

        public Entry(E e){this.element = element; left = right = null;}
        public Entry<E> getLeft(){return left;}
        public Entry<E> getRight(){return right;}
    }
/****************************************************************/

    public static void main(String[] args) {

        BinarySearchTreeArray<String> bsta1 = new BinarySearchTreeArray<String>();
        BinarySearchTreeArray<Integer> bsta2 = new BinarySearchTreeArray<Integer>();

        bsta1.add("dog");
        bsta1.add("tutle");
        bsta1.add("cat");
        bsta1.add("ferrit");
        bsta1.add("shark");
        bsta1.add("whale");
        bsta1.add("porpoise");

        bsta2.add(3);
        bsta2.add(18);
        bsta2.add(4);
        bsta2.add(99);
        bsta2.add(50);
        bsta2.add(23);
        bsta2.add(5);
        bsta2.add(101);
        bsta2.add(77);
        bsta2.add(87);
    }
}
4

1 回答 1

3

add方法确实与您的contains方法相似。在用结构/对象表示的典型二叉树中,您将使用指针访问右子树和左子树(如您的示例 temp.left 和 temp.right)。但是,由于您在数组中有一棵树,您需要通过索引访问该数组,所以问题是:如何访问对应于左/右子树的索引?

为此,您可以使用以下表达式left = parent * 2right = parent * 2 + 1。我将为您提供一个方法示例,该add方法将元素添加到表示为整数数组的树中,其中-1在 java 中表示无值或 null。

public void add(E e)
{
    Entry<E> node = new Entry<E>(e);
    index = 0;
    int comp;
    boolean not_add = true;
    while(not_add)
    {
      if (tree[index] == null) //if this node is empty
      {
          tree[index] = node;
          size++;
          not_add  = true;
      }
     
      comp = ((Comparable)e).compareTo (tree[index].element);
      
      if(comp == 0) not_add = true; // Same value
      else if (comp < 0) index = index * 2;  // should be insert on the left
      else index = index * 2 + 1; // should be insert on the right
     }
}
于 2012-10-27T16:16:05.953 回答