Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Binary tree

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary tree

    Hello,

    I am writing a program that maintains basic information about people (each person have id num, first name, last name).
    I keep all the data using a binary tree, which DATA is PERSON type that I created. the data is sorted by ID number of the person.

    I want to develop a search in the binary tree by the name of the person (the tree sorted by ID, not by the name...)
    What is the most efficient way to do it?

    Thnks,


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Binary tree

    What do you mean by "most efficient?" If you had a flat data structure, you knew what you meant by efficient, AND roughly how large the number of people it contained, you should be able to read up on the efficiency of the most popular sorting algorithms to find which you'd like to use. Since you're using a sorted binary tree, the data structure may already be optimized for a relatively simple (and efficient) search to find the desired record.

    I suggest you read up on traversing a binary search tree (BST) to better understand the benefits of the data structure when looking for the data it contains.

    You might also considering reordering the data by the item being searched, but that may only be practical for relatively small data sets.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    The structure should support small and very large number of records.
    The Tree already supports the search (quick and eddicient) records by key field, the ID Number of structure person).

    What I'm asking is what is customary to do in cases where the tree has more data (in addition to key field - the id also has first and last name) and I want to do a quick search (and efficient) using this fields too?
    How to perform this search?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    It sounds like you want to have multiple keys to the records held in the collection/tree so the records can be accessed in different ways: by ID or by NAME or by ????
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    yes, the "DATA" of the tree is PERSON type (that i create) that have the following information about each person: ID, fname, lname, address.
    the tree sorted by the ID
    but also i want to search by fname for example

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    Can you create a separate index to the tree for each key you want to search on?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    how to do it?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    Are you asking how to build a tree with its contents sorted on another key?
    You have the logic to build a tree sorted on ID, use that logic to build a tree sorted on NAME.

    In other words build parallel trees with each tree having its own key.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    my PERSON type have ID, fname, lname, address
    and I built one tree that have that type.
    you mean I should build 4 trees (1- ID, 2 - fname, 3 - lname, 4 - address) instead?
    how will i do the link between the 4 trees?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    how will i do the link between the 4 trees?
    The trees are in parallel. Any changes made to one tree needs to be made to all of them to keep them in sync.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    but if i have 3 persons for example:
    id = 1, addr = z st
    id = 2, addr = h st
    id = 3, addr = a st

    the id's tree will be like: (if i sort it by the id...)
    2
    1 3

    the addr's tree wil be like: (if i sort it by the addr...)
    h
    a z

    do you have some examples for source codes like this?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    I don't have any sample code.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    ok, but do you have an idea how can i link between the trees?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary tree

    There is no link between the trees. Each one is separate from the other.
    It is possible to add the same object to many different collections/trees. There shouldn't be anything in the object referring to the collection it is in.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary tree

    Thank you!!!

Similar Threads

  1. (Binary Tree) Family tree - help with my addChild method
    By Pip_Squeak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2014, 07:52 AM
  2. Binary Tree ( Family Tree)
    By tommyacton in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 23rd, 2013, 10:40 PM
  3. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  4. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM