Help with errors in my BinaryTree code

  • Thread starter DODGEVIPER13
  • Start date
  • Tags
    Code Errors
In summary, The code is experiencing errors in the BinaryTree class and the poster is seeking help to fix them. They have tried using a debugger and have already found and fixed the errors. They also mention using [ code ] tags to make the code more readable.
  • #1
DODGEVIPER13
672
0
Mod note: I added [ code ] and [ /code ] tags.

Homework Statement


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.

Homework Equations


The Attempt at a Solution


first class Tree:
Code:
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:
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:
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:
Physics news on Phys.org
  • #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
 
  • #3
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?
 
  • #4
lol I have never used such a thing how do I do this
 
  • #5
I am disappointed in myself for not catching these dumb errors thanks for the debugger idea.
 
  • #6
rcgldr said:
use [ code ] and [ /code ] (without the spaces) to make your code more readable. If you get back to this question before 24 hours
IIRC, the time interval is much shorter now, say 3 hours.
rcgldr said:
, 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?
 
  • #7
MARK44 I already figured it out but thanks
 

FAQ: Help with errors in my BinaryTree code

Why am I getting a null pointer exception in my BinaryTree code?

A null pointer exception occurs when you try to access or manipulate an object that is null or does not exist. This could happen in your BinaryTree code if you are trying to access a node that does not have any children, or if you are trying to access a node that has not been initialized properly.

How can I fix errors with adding or removing nodes in my BinaryTree?

If you are encountering errors when adding or removing nodes in your BinaryTree, double check your code to ensure that you are properly updating the parent and child relationships. Additionally, make sure that you are handling cases where the node being added or removed is the root node.

Why is my BinaryTree not properly traversing in order?

If your BinaryTree is not traversing in the correct order, it could be due to errors in your traversal logic. Make sure that you are using the correct traversal method (pre-order, in-order, post-order) and that you are properly handling the left and right child nodes.

How do I handle duplicate values in my BinaryTree?

Handling duplicate values in a BinaryTree can be tricky. One approach is to store the duplicate values in a separate data structure, such as a list, and then add logic to handle those values when traversing or searching the BinaryTree. Another approach is to modify your BinaryTree to allow for duplicate values by creating a separate node for each instance of the value.

What resources can I use to learn more about BinaryTrees and troubleshoot errors?

There are many online resources available to learn more about BinaryTrees and troubleshoot errors. Some popular options include online tutorials, coding forums, and textbooks on data structures and algorithms. Additionally, you can reach out to more experienced programmers or seek help from a tutor or mentor to guide you through troubleshooting your BinaryTree code.

Similar threads

Replies
1
Views
9K
Replies
5
Views
2K
Replies
2
Views
3K
Replies
16
Views
4K
Replies
8
Views
3K
Replies
3
Views
3K
Replies
4
Views
3K
Back
Top