1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with errors in my BinaryTree code

  1. Mar 14, 2012 #1
    Mod note: I added [ code ] and [ /code ] tags.
    1. The problem statement, all variables and given/known data
    I am having touble with errors in this code and honestly I have nothing on why it does not work so if anyone can help it will be much appreciated! The errors are in the BinaryTree class I will bold the mistakes in the program.

    2. Relevant equations



    3. The attempt at a solution
    first class Tree:
    Code (Text):

    public interface Tree<E extends Comparable<E>> {
       
        public boolean search(E e);
        public boolean insert(E e);
        public boolean delete(E e);
        public void inOrder();
        public void postorder();
        public void preorder();
        public int getSize();
        public boolean isEmpty();
        public java.util.Iterator iterator();
    }
     
    second class Abstracttree:
    Mod note: It looks like you pasted the Abstracttree class in twice.
    Code (Text):

    public abstract class Abstracttree<E extends Comparable<E>>implements Tree<E> {
       
        public void inorder() {
           
        }
        public void postorder() {
           
        }
        public void preorder() {
           
        }
        public boolean isEmpty() {
            return getSize() == 0;
        }
        public java.util.Iterator iterator() {
            return null;
        }
    }


    public abstract class Abstracttree<E extends Comparable<E>>implements Tree<E> {
       
        public void inorder() {
           
        }
        public void postorder() {
           
        }
        public void preorder() {
           
        }
        public boolean isEmpty() {
            return getSize() == 0;
        }
        public java.util.Iterator iterator() {
            return null;
        }
    }
     
    final class BinaryTree:
    Code (Text):

    public class BinaryTree<E extends Comparable<E>> extends Abstracttree<E> {
       
        protected TreeNode<E> root;
        protected int size = 0;
       
        public BinaryTree(){
       
    }
        public BinaryTree(E[] objects){
            for(int i = 0;i < objects.length; i++)
                insert(objects[i]);
        }
        public boolean search(E e){
            while ([B] current [/B]!= null){
                if(e.compareTo([B]current[/B].element)< 0){
                    [B]current[/B] = [B]current[/B].left;
                }
                else if (e.compareTo([B]current[/B].element) > 0){
                   [B] current [/B]= [B]current[/B].right;
                }
                else
                    return true;
            }
            return false;
        }
        public boolean insert(E e){
            if(root == null)
                root = createNewNode(e);
            else{
                TreeNode<E> parent = null;
                TreeNode<E> current = root;
                while(current != null)
                    if (e.compareTo(current.element) < 0){
                        parent = current;
                        current = current.left;
                    }
                    else if (e.compareTo(current.element) > 0){
                        parent = current;
                        current = current.right;
                    }
                else
                        return false;
               
                if(e.compareTo(parent.element)< 0)
                    parent.left = createNewNode(e);
                else
                    parent.right = createNewNode(e);
            }
            size++;
            return true;
        }
        protected TreeNode<E> createNewNode(E e){
            return new TreeNode<E>(e);
    }
        public void inorder(){
            inorder(root);
        }
        protected void inorder(TreeNode<E> root){
           [B] if(root = null) return;[/B]        inorder(root.left);
            System.out.print(root.element + " ");
            inorder(root.right);
        }
        public void postorder(){
            postorder(root);
        }
        protected void postorder(TreeNode<E> root){
           [B] if(root = null) return;[/B]  
         postorder(root.left);
            postorder(root.right);
            System.out.print(root.element + " ");
        }
        public void preorder() {
            preorder(root);
        }
        protected void preorder(TreeNode<E> root){
            if(root == null) return;
            System.out.print(root.element + " ");
            preorder(root.left);
            preorder(root.right);
        }
        public static class TreeNode<E extends Comparable<E>>{
            E element;
            TreeNode<E> left;
            TreeNode<E> right;
           
            public TreeNode(E e){
                element = e;
            }
        }
        public int getSize(){
            return size;
        }
        public TreeNode getRoot(){
            return root;
        }
        public java.util.ArrayList<TreeNode<E>> path(E e) {
            java.util.ArrayList<TreeNode<E>> list = new java.util.ArrayList<TreeNode<E>>();
            TreeNode<E> current = root;
           
            while(current != null){
                list.add(current);
                if(e.compareTo(current.element) < 0){
                    current = current.left;
                }
                else if (e.compareTo(current.element) > 0){
                    current = current.right;
                }
                else break;
            }
            return list;
        }
        public boolean delete(E e){
            TreeNode<E> parent = null;
            TreeNode<E> current = root;
            while(current != null){
                if(e.compareTo(current.element) < 0){
                    parent = current;
                    current = current.left;
                }
                else if (e.compareTo(current.element) > 0){
                    parent = current;
                    current = current.right;
                }
                else
                    break;
            }
            if(current == null)
                return false;
            if(current.left == null){
                if(parent == null){
                    root = current.right;
                }
                else{
                    if(e.compareTo(parent.element) < 0)
                        parent.left = current.right;
                    else
                        parent.right = current.right;
                }
            }
            else{
                TreeNode<E> parentOfRightMost = current;
                TreeNode<E> rightMost = current.left;
               
                while(rightMost.right != null){
                    parentOfRightMost = rightMost;
                    rightMost  = rightMost.right;
                }
                current.element = rightMost.element;
               
                if(parentOfRightMost.right == rightMost)
                    parentOfRightMost.right = rightMost.left;
                else
                    parentOfRightMost.left = rightMost.left;
            }
            size--;
            return true;
        }
        public java.util.Iterator iterator(){
            return inorderIterator();
        }
        public java.util.Iterator inorderIterator(){
            return new InorderIterator();
        }
        class InorderIterator implements java.util.Iterator {
            private java.util.ArrayList<E> list = new java.util.ArrayList<E>();
            private int current = 0;
           
            public InorderIterator(){
           [B] inorder();[/B]
       }
            public void inorder(TreeNode<E> root){
                if(root == null) return;
                inorder(root.left);
                list.add(root.element);
                inorder(root.right);
            }
            public boolean hasNext(){
                if(current < list.size())
                    return true;
               
                return false;
            }
            public Object next(){
                return list.get(current++);
            }
            public void remove(){
                delete(list.get(current));
                list.clear();
               [B] inorder();[/B]      
      }
        }
        public void clear(){
            root = null;
            size = 0;
                   
        }
    }
     
     
    Last edited by a moderator: Mar 14, 2012
  2. jcsd
  3. Mar 14, 2012 #2
    sorry if it is hard to read I can resubmit if needed but please someone help me on my last three post I have followed all the requirements and not one person answered me
     
  4. Mar 14, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    use [ code ] and [ /code ] (without the spaces) to make your code more readable. If you get back to this question before 24 hours, you should be able to update your original post.

    Have you tried using a source level debugger to step through your code to see where the error is?
     
  5. Mar 14, 2012 #4
    lol I have never used such a thing how do I do this
     
  6. Mar 14, 2012 #5
    I am disappointed in myself for not catching these dumb errors thanks for the debugger idea.
     
  7. Mar 15, 2012 #6

    Mark44

    Staff: Mentor

    IIRC, the time interval is much shorter now, say 3 hours.
     
  8. Mar 15, 2012 #7
    MARK44 I already figured it out but thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with errors in my BinaryTree code
Loading...