Java 在基于数组的二叉搜索树中添加元素

Java 在基于数组的二叉搜索树中添加元素,java,arrays,binary-tree,binary-search-tree,Java,Arrays,Binary Tree,Binary Search Tree,所以我一直在努力解决这个问题。基本上,我需要向基于数组的二进制搜索树中添加一个元素。根据我的文本,它类似于compareTo方法。我甚至不知道该往哪个方向走。说到OOP,我是一个彻头彻尾的傻瓜,所以任何帮助都将不胜感激 package lab9; public class BinarySearchTreeArray<E> { Entry<E> [] tree; Entry<E> root; int size; public

所以我一直在努力解决这个问题。基本上,我需要向基于数组的二进制搜索树中添加一个元素。根据我的文本,它类似于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);
    }
}
lab9包装;
公共类二进制搜索树array{
条目[]树;
入口根;
整数大小;
公共二进制搜索树array()
{
tree=null;
尺寸=0;
}
公共整数大小()
{
返回大小;
}
公共布尔包含(对象obj)
{
入口温度=根;
国际公司;
if(obj==null)
抛出新的NullPointerException();
while(obj!=null)
{
组件=((可比)对象)。比较到(温度元素);
如果(comp==0)
返回true;
否则如果(组件<0)
温度=左侧温度;
其他的
温度=右侧温度;
}//当
返回false;
}//包含方法
/*
*案文如下:
*add(E元素)方法的定义只是稍微多一点
*比contains(Object obj)的定义复杂。基本上,
*add方法从树的根和分支开始
*正在搜索元素;如果搜索失败,则元素将被删除
*作为叶子插入的。
*/
公共空间添加(E)
{
条目节点=新条目(e);
if(树[父]==null)
{
树[0]=节点;
大小++;
}
其他的
{
树[1]=节点;
大小++;
}
}//添加方法
/****************************************************************/
受保护的静态类条目
{
私人电子元件;
私有入口父级,左、右;
公共条目(E){this.element=element;left=right=null;}
公共条目getLeft(){return left;}
公共条目getRight(){return right;}
}
/****************************************************************/
公共静态void main(字符串[]args){
BinarySearchTrearray bsta1=新的BinarySearchTrearray();
BinarySearchTrearray bsta2=新的BinarySearchTrearray();
bsta1.添加(“狗”);
bsta1.添加(“tutle”);
bsta1.添加(“cat”);
bsta1.添加(“ferrit”);
bsta1.添加(“鲨鱼”);
bsta1.添加(“鲸鱼”);
bsta1.添加(“鼠海豚”);
bsta2.添加(3);
bsta2.添加(18);
bsta2.添加(4);
bsta2.添加(99);
bsta2.添加(50);
bsta2.添加(23);
bsta2.添加(5);
bsta2.添加(101);
bsta2.添加(77);
bsta2.添加(87);
}
}

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

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

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
     }
}
public void add(E)
{
条目节点=新条目(e);
指数=0;
国际公司;
布尔值not_add=true;
while(不添加)
{
if(tree[index]==null)//如果此节点为空
{
树[索引]=节点;
大小++;
not_add=true;
}
comp=((可比)e).compareTo(树[index].element);
如果(comp==0)不是_add=true;//相同的值
如果(comp<0)index=index*2;//应该插入左侧
else index=index*2+1;//应插入右侧
}
}