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

标签 java arrays binary-tree binary-search-tree

所以我遇到了我正在处理的当前问题。基本上,我需要向基于数组的二叉搜索树添加一个元素。根据我的文字,它类似于 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);
    }
}

最佳答案

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
     }
}

关于java - 在基于数组的二叉搜索树中添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096540/

相关文章:

Java IF 语句需要多个输入

java - 如何获取Hbase中给定行键一部分的所有行

java - 在VideoView中播放 'unsupported'编解码器

java - 如何通过另一个ArrayList中的字段从ArrayList中删除项目?

c - 了解 C 中数组元素中分配的内存地址(Windows 10 中的 gcc)

c++ - : expected constructor,析构函数错误,或者 '*' token之前的类型转换|

c# - 如何在 C# 中删除选定的数组值

c++ - 相同大小的小对象的非常快速的对象分配器

c - 如何从内存中正确分配结构

java - 从级别顺序输入创建二叉树