Results 1 to 5 of 5
  1. #1
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default How to insert node to tree while comparing string instead of int

    I want to create a program that insert phone record to a binary search tree...

    The record accept name and phone number as input, so when i want to compare the data(key), which is the string type name, i have to compare it in alphabetical order.
    For example when i insert Kevin to the tree, Aaron will be the first node, and Brandon will be inserted again to the tree, the program will compare these 2 strings and insert to the left node if the alphabet is less than the first node. Does anyone know how to compare them? I know the equal method check whether string name is same and compareTo is to compare both string but i dont really understand how it works, can anyone explain the method with examples?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The API to the rescue: String.html#compareTo(java.lang.String)

    Much luck!
    Thanks for the reference. Ok now I understand the compareTo() method, but somehow i stil need some examples to go into deeper of my understanding, can you briefly provide an example with simple variable inside? just a brief example will do. Thanks again..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Javanoobs View Post
    Thanks for the reference. Ok now I understand the compareTo() method, but somehow i stil need some examples to go into deeper of my understanding, can you briefly provide an example with simple variable inside? just a brief example will do. Thanks again..
    Example of what specifically, and what in particular don't you understand? Perhaps if you try to create what you are trying to build we can go from there.

  5. #5
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    ok....so far im having problem of comparing the name in a phone book, I have to create a binary search tree which contain the name and phone number, so the name would be the data key...everytime I insert a name, a new node will be created and inserted into the tree, suppose the tree is not empy and contain a name Kevin, so i wanted to insert a name, i have to compare the name i have inserted with the name Kevin in alphabetical order, if the insert name is less than Kevin, then it will create a left subtree, else it wil create right subtree...so my problem is I not quite clear how to compare the strings, below is my code :

    Java Code:
     
    public void compareTo(String name)         // method to compare string
        {        
         this.charAt(p_name)- name.charAt(p_name)
          }   
     public void insert(String p_name, int p_phoneNum)
        {
         PhoneBook newPhoneBook = new PhoneBook();   // create a new node
         newPhoneBook.name = p_name;
         newPhoneBook.phoneNum = p_phoneNum;
         if(root == null)                           // if no node in root
         root = newPhoneBook;                      // assign root to new node
         else if (p_name != null)
         {
             PhoneBook current = root;  // start at first node
             PhoneBook parent;
             while(true)
             {
                 parent = current;
                 if(p_name.equalsIgnoreCase(current.name));    // if name inserted exist
                 {
                     System.out.print("Insert error: Record has already exist");
                 }
                 else
          {
                    root = p_name.compareTo(name);         // invoke compareTo method
                   }
                  if(current == null)
                 {
                     
                     parent.leftChild = newPhoneBook;
                     return;
                 }
                 else
                 {
                         current = current.rightChild;
                         if(current == null)
                         {
                             parent.rightChild = newPhoneBook;
                             return;
                         }
                     }
                 }
             } // end while
            }
        } // end insert()


    so this is how much i got for my insert part, the compareTo method Im not very sure about that, hopeful to get from help

Similar Threads

  1. Replies: 0
    Last Post: 04-17-2011, 04:57 PM
  2. How to sample a node from a binary tree efficiently?
    By malaguena in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2011, 05:57 PM
  3. How to capture a tree node edit ?
    By ni4ni in forum AWT / Swing
    Replies: 1
    Last Post: 03-11-2011, 05:21 AM
  4. Replies: 1
    Last Post: 08-24-2009, 11:41 AM
  5. Replies: 0
    Last Post: 02-18-2009, 05:20 AM

Posting Permissions

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