Results 1 to 6 of 6
  1. #1
    quest is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Defining methods in TreeComparable Interface

    I have the TreeComparable Interface and BinaryTree class, but I am having difficulty with understanding how to define the operate method within my class. At the end of the insertBSTDuplicate method, the operate is method is called, but I cannot figure out from the operation how operate needs to be defined. I have tried different things and none of them seem to work.

    Any help is appreciated.

    Java Code:
    // Class implementing Interface methods
    public void operate(Object o) {
    
    }
    
    // TreeComparable.java
    public interface TreeComparable {
        public int compareTo(Object o);
        public void operate(Object o);
        public void visit();
    }
    
    // BinaryTree
    	public void insertBST(Object o) {
            ObjectTreeNodeInterface p, q;
                    
            ObjectTreeNodeInterface r = new ObjectTreeNode(o);
            if (root == null)
                root = r;
            else {
                p = root;
                q = root;
                while (q != null) {
                    p = q;
                    if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0 )
                        q = p.getLeft();
                    else
                        q = p.getRight();
                }
                if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)
                    setLeftChild(p, r);
                else
                    setRightChild(p, r);
            }
        }
     
    	public void insertBSTDuplicate(Object o) {
            ObjectTreeNodeInterface p, q;
                    
            ObjectTreeNodeInterface r = new ObjectTreeNode(o);
            if (root == null)
                root = r;
            else {
                p = root;
                q = root;
                while (q != null && ((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) != 0) {
                    p = q;
                    if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)
                        q = p.getLeft();
                    else
                        q = p.getRight();
                }
                if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)
                    setLeftChild(p, r);
                else if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) > 0)
                    setRightChild(p, r);
                else ((TreeComparable)(p.getInfo())).operate((TreeComparable)(r.getInfo()));
            }
        }
    Last edited by JosAH; 11-28-2013 at 11:04 AM. Reason: added [code] ... [/code] tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Defining methods in TreeComparable Interface

    Where did the interface come from? Is it documented?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Defining methods in TreeComparable Interface

    I don't think comparing two binary search trees is that easy, i.e. two infix iterations over two trees might have the same result while the two trees can be entirely different. If you have to take that in account, there's no PTO (Partial Total Order).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Defining methods in TreeComparable Interface

    Assume two trees are guaranteed to be red-black and any given traversal of one tree produces the same results of another. Can one declare the trees to have the same parent-child relationships? Can this red-black structure be enforced (perhaps via some abstract class or interface requirement)? I admit I haven't given this any thought. Just thought I ask the questions in case someone else has.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Defining methods in TreeComparable Interface

    Quote Originally Posted by jim829 View Post
    Assume two trees are guaranteed to be red-black and any given traversal of one tree produces the same results of another. Can one declare the trees to have the same parent-child relationships? Can this red-black structure be enforced (perhaps via some abstract class or interface requirement)? I admit I haven't given this any thought. Just thought I ask the questions in case someone else has.
    I gave it some thoughts ;-) If the infix traversals compare unequal, you know the order of the two trees L and R; otherwise, structurally compare L against R; the restriction of red-black trees is unneeded; if the roots of the trees compare unequal, you're done; otherwise compare both left subtrees etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    quest is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Defining methods in TreeComparable Interface

    From what I understand when the left and right child of the parent tree node are equal to each other, the operate method is called. But as for the operate method, I am unsure how that is defined when implemented.

Similar Threads

  1. Using methods from AppletContext interface..
    By hsl in forum New To Java
    Replies: 4
    Last Post: 05-12-2012, 11:40 PM
  2. Interface methods
    By DeltaWarrior in forum New To Java
    Replies: 3
    Last Post: 12-26-2011, 01:48 PM
  3. Methods that are NOT defined in interface
    By johto760 in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 07:31 PM
  4. [SOLVED] defining methods
    By goliwala in forum New To Java
    Replies: 10
    Last Post: 10-24-2008, 04:00 PM
  5. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 08:57 PM

Posting Permissions

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