简体   繁体   English

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

[英]Adding an element in an array based Binary Search Tree

So I've stumped on this current problem I'm working on.所以我很难解决我正在解决的这个当前问题。 Basically, I need to add an element to my array based binary search tree.基本上,我需要向基于数组的二叉搜索树添加一个元素。 According to my text it is similar to the compareTo method.根据我的文字,它类似于 compareTo 方法。 I'm not even sure what direction to head in. I'm a complete noob when it comes to OOP so any help would be appreciated.我什至不确定要往哪个方向发展。当涉及到 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);
    }
}

The add method is indeed similar to your contains method. add方法确实类似于您的contains方法。 In a typical binary tree represented with structs/objects you would access the right and left subtrees using pointers (as in your example temp.left and temp.right).在用结构/对象表示的典型二叉树中,您将使用指针访问左右子树(如您的示例 temp.left 和 temp.right)。 But, since you have a tree in an array you need to access that array by index, so the question is : How to access the index that corresponds to the left/right subtrees?但是,由于数组中有树,因此需要按索引访问该数组,因此问题是:如何访问对应于左/右子树的索引?

For that, you can use the following expression left = parent * 2 and right = parent * 2 + 1 .为此,您可以使用以下表达式left = parent * 2right = parent * 2 + 1 I will provide you with one example of the add method that would add elements to a tree represented as an array of integers, where -1 represents no values or null in java.我将为您提供一个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
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM