Help with errors in my BinaryTree code

  • Thread starter Thread starter DODGEVIPER13
  • Start date Start date
  • Tags Tags
    Code Errors
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting errors in a BinaryTree implementation in Java. Participants are seeking assistance with specific issues in the code related to class structure, method functionality, and debugging techniques.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The original poster expresses confusion over errors in their BinaryTree class and requests help in identifying the issues.
  • Some participants suggest using a source-level debugger to step through the code to locate errors.
  • There are mentions of formatting issues in the code presentation, with suggestions to use code tags for better readability.
  • The original poster acknowledges their frustration and expresses a desire to improve their debugging skills.

Areas of Agreement / Disagreement

Participants generally agree on the usefulness of debugging tools and the importance of code formatting for clarity. However, there is no consensus on the specific errors in the code, as the original poster has not yet received detailed feedback on their implementation.

Contextual Notes

The discussion includes references to potential errors in the BinaryTree code, but specific details about these errors remain unresolved. The original poster has not provided a complete context for all issues encountered.

DODGEVIPER13
Messages
668
Reaction score
0
Mod note: I added [ code ] and [ /code ] tags.[/color]

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. [/color]
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
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
 
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?
 
lol I have never used such a thing how do I do this
 
I am disappointed in myself for not catching these dumb errors thanks for the debugger idea.
 
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?
 
MARK44 I already figured it out but thanks
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K