简体   繁体   中英

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

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. 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). 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 . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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