Results 1 to 2 of 2
  1. #1
    HelloKomputer is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Unhappy I've been trying for days to get my binary tree class to work.

    The debugger won't work. it just says # with random numbers next to it and doesn't tell me what ints are being passed. I don't know how to set up printlns for trees either.

    The outputs are all pretty different so that causes me some trouble trying to fix it.

    Here is one: (0 stops the list from populating)
    Java Code:
    run:
    Enter an integer to be inserted or 0 to quit:
    1
    2
    3
    0
    "printing P:"
    1
    2
    3
    "now copying:"
    1
    "done printing Q"
    BUILD SUCCESSFUL (total time: 7 seconds)
    
    Here is another:
    
    run:
    Enter an integer to be inserted or 0 to quit:
    1
    2
    3
    -4
    -5
    -6
    0
    printing P
    -6
    -5
    -4
    1
    2
    3
    now copying
    -6
    -5
    -4
    1
    done printing Q
    BUILD SUCCESSFUL (total time: 13 seconds)
    One more, which works:
    
    run:
    Enter an integer to be inserted or 0 to quit:
    6
    5
    4
    0
    printing P
    4
    5
    6
    now copying
    4
    5
    6
    done printing Q
    BUILD SUCCESSFUL (total time: 11 seconds)
    So it only copies the lowest number entered, unless there are negative numbers, in which case it copies those but only the lowest positive number entered. The negative numbers work fine, it's only the positive numbers entered in ascending order that don't copy.
    
    Here is the class:
    
    public class Intcoll6
      {
        private int howmany;
    private btNode c;
    
    public Intcoll6()
      {
        howmany = 0;
        c = null;
      }
    //we have to use this private class
    private class btNode
      {
        int info;
        btNode left;
        btNode right;
    
        public btNode()
          {
            info = 0;
            left = null;
            right = null;
          }
      }
    public boolean belongs(int i)
      {
        boolean result = false;
        btNode p = c;
        while ((p != null) && (p.info != i))
          {
            if (i < p.info)
              {
                p = p.left;
              }
            if (p.info > i)
              {
                p = p.right;
              }
            if (p != null)
              {
                result = true;
              }
          }
        return result;
      }
    //I believe the problem is with this method
    public void insert(int i)
      {
        btNode p = c;
        btNode pred = null;
        while ((p != null) && (p.info != i))
          {
            pred = p;
            if (i < p.info)
              {
                p = p.left;
              } else if (i > p.info)
              {
                p = p.right;
              }
          }
        if (p == null)
          {
            howmany = howmany + 1;
            p = new btNode();
            if (pred != null)
              {
                if (i < pred.info)
                  {
                    pred.left = p;
                  }
                if (i > pred.info)
                  {
                    pred.right = p;
                  }
                p.info = i;
              }
            if (pred == null)
              {
                c = p;
                p.info = i;
              }
    
          }
      }
    
    public void omit(int i)
      {
        btNode p = c;
        btNode pred = null;
        while ((p != null) && (p.info != i))
          {
            pred = p;
            if (i < p.info)
              {
                p = p.left;
              }
            if (i > p.info)
              {
                p = p.right;
              }
          }
        if (p != null)
          {
            howmany--;
            if (pred != null)
              {
                if ((p.left == null) && (p.right == null))
                  {
                    if (p.info > pred.info)
                      {
                        pred.right = null;
                      } else
                      {
                        pred.left = null;
                      }
                  } else if ((p.left != null) && (p.right == null))
                  {
                    if (p.info > pred.info)
                      {
                        pred.right = p.left;
                      } else
                      {
                        pred.left = p.left;
                      }
                  } else if ((p.right != null) && (p.left == null))
                  {
                    if (p.info < pred.info)
                      {
                        pred.left = p.right;
                      } else
                      {
                        pred.right = p.right;
                      }
                  } else if ((p.left != null) && (p.right != null))
                  {
                    if (p.info > pred.info)
                      {
                        btNode q = p;
                        btNode q1 = pred;
                        while (q != null)
                          {
                            q1 = q;
                            q = q.left;
                          }
                        p.info = q.info;
                        q1.left = null;
    
                      }
                    if (p.info < pred.info)
                      {
                        btNode q = p;
                        btNode q1 = p;
                        while (q != null)
                          {
                            q1 = q;
                            q = q.right;
                          }
                        p.info = q.info;
                        q1.right = null;
                      }
                  }
              }
            if (pred == null)
              {
                if ((p.left == null) && (p.right == null))
                  {
                    c = null;
                  } else if ((p.left != null) && (p.right == null))
                  {
                    c = p.left;
                  } else if ((p.left == null) && (p.right != null))
                  {
                    c = p.right;
                  } else if ((p.right != null) && (p.left != null))
                  {
                    btNode q = p;
                    btNode q1 = pred;
                    while (q != null)
                      {
                        q1 = q;
                        q = q.right;
                      }
                    p.info = q.info;
                    q1.right = null;
    
                  }
              }
          }
      }
    private btNode copyTree(btNode tree)
    {
        btNode result = null;
        if(tree != null)
        {
            btNode L;
            btNode R;
            L = copyTree(tree.left);
            R = copyTree(tree.right);
            result = new btNode();
            result.info = tree.info;
            result.left = L;
            tree.right = R;
        }
        return result;
    }  
    public void copy (Intcoll6 obj)
    {
        if(this != obj)
        {
        howmany = obj.howmany;
        c = copyTree(obj.c);
        }
    }
    private static void printtree(btNode t)
      {
        if (t != null)
          {
            printtree(t.left);
            System.out.println(t.info);
            printtree(t.right);
          }
      }
    public void print()
      {
        printtree(c);
      }
    public boolean equals(Intcoll6 obj)
      {
        boolean result = (howmany == obj.howmany);
        btNode p = c;
        btNode q = obj.c;
        while ((p != null) && (q != null))
          {
            btNode pred = p;
            if ((p.info == q.info))
              {
                if (p.info >= pred.info)
                  {
                    p = p.right;
                    q = q.right;
                  }
              }
            if ((p == null) && (q == null))
              {
                result = true;
              }
          }
        return result;
      }  
      }
    Driver:
    
    import intcoll6.Intcoll6;
    import java.util.*;
    import static intcoll6.Intcoll6Client.SENTINEL;
    public class Driver6
      {
    int value;
    public static final int SENTINEL = 0;
    public static void main(String[] args)
      {
        System.out.println("Enter an integer to be inserted or 0 to quit:");
        int value;
        Scanner keyboard = new Scanner(System.in);
        value = keyboard.nextInt();
        Intcoll6 P = new Intcoll6();
        while (value != SENTINEL)
          {
            P.insert(value);
            value = keyboard.nextInt();
          }
        System.out.println("printing P");
        P.print();
        System.out.println("now copying");
        Intcoll6 Q = new Intcoll6();
        Q.copy(P);
        Q.print();
        System.out.println("done printing Q");
      }
      }

  2. #2
    HelloKomputer is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:08 AM
  2. binary tree
    By Dedo in forum Advanced Java
    Replies: 15
    Last Post: 05-26-2011, 10:11 PM
  3. binary tree
    By ryamz in forum New To Java
    Replies: 2
    Last Post: 08-12-2010, 03:45 AM
  4. Replies: 0
    Last Post: 04-04-2010, 08:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •