Results 1 to 27 of 27

Thread: Binary Tree (Solved)

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Smile Binary Tree (Solved)

    Hey... I am trying to create a Binary Tree Structure But I cannot use pointers... Any Ideas there? (Phinds already gave me an idea of this and I have already created a Binary Tree, so don't mind this)

    I would really appreciate any help. I am doing it from the scratch so please be patient with me...

    (As a matter of fact the Binary Tree is complete, I am just working on restructuring the Tree. I am writing a code to avoid the worst case with this... Can anyone tell me how to calculate the right and left height of a node using its level and the right or left height of the whole tree? With this Information I can be able to solve the whole problem)

    Come on... Don't tell me you are not interested. I know every programmer would like to have a Binary Tree, they are very useful!

    The code below has not yet the Balancing Rotations nor the DeleteNode. But since nobody has replied, I guess you don't want it. If you feel like it, please reply. (This was solved and the code is added later in this thread)

    Thanks
    Last edited by Tec-Nico; Nov 3rd, 2002 at 09:40 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    check this out
    Attached Files Attached Files

  3. #3

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking Thanks!

    I am going to test it now.. Thanks a lot!
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  4. #4

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Arrow Thank you!

    Thank you phinds!

    I just checked it and it is as I was programming it. But I have a doubt...

    How would you perform an inorder, post-order and pre-order?

    How did you solve the problem of pointers? I see you made an array of BTrees but I still don't get how it works. Could you explain it to me in a simple way?

    Tell me, what if I wanted to resize it?

    I have something like this... How can I adapt it?


    VB Code:
    1. 'Forget it.. I already solved this.
    2.  
    3. 'Useful Note: Use IsObject and IsEmpty to know if an Object is Null.
    4. 'Don't use IsNothing since it does not exist, though VB swears it
    5. 'does.

    What am I doing wrong?
    Last edited by Tec-Nico; Sep 16th, 2002 at 08:55 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  5. #5

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Red face Duh...

    Alright... After having slept I found the solution to my own question.

    Tell me... If Left or Right is 0 it means it is a leaf, right?

    Taking that in consideration I created the following procedures:


    VB Code:
    1. 'Don't Mind this, if it is -1 then it means it is a leaf.
    2. If (you want to see code) Then Go Below

    But still I am trying to make the BTree to resize and not to stay in only 15 elements...

    I am working on it... Could you please provide me with more information regarding to your code?

    Thanks in advance!
    Last edited by Tec-Nico; Sep 16th, 2002 at 08:53 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  6. #6

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Unhappy Still Working...

    Why can't I resize the array?

    VB Code:
    1. 'Don't Mind this.. I already solved it.
    2.  
    3. 'Don't try to Redim an Array part of a Data Structure if you ignore
    4. 'How it works!
    5.  
    6. 'There are 10 kinds of people on Earth. The ones who know binary...
    7. 'And the ones who don't.

    By the way, is UnWind a procedure to Balance the BTree?
    Last edited by Tec-Nico; Sep 16th, 2002 at 08:50 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  7. #7
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    you have to use redim with preserve to change the size of the array

    unwind just sucks all the elements out of the tree, but does so in order.

    this is the classic binary tree algorithm. I found it in the original C book by K&R 25 years ago.

  8. #8

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Question Yes, But...

    I know... If you see I tried Redim Preserve.

    But it only works for the first 3 elements. This means it will do fine until Index = 3 then it messes up since it says the Array is Temporaly Locked. Why is this happening?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  9. #9
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    sorry, I was in a hurry and didn't look at your code to see that you had used redim

    I've never tried anything like what you're doing, so have no idea why it doesn't work. When I redim, I redim an array, not an array that is part of a structure.

  10. #10
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Try this module dudes, its pretty usefull i guess

    Attached Files Attached Files

  11. #11
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    comment on the .BAS module posted in the previous entry of this thread:

    he's got a lot of good stuff in there, but you have to take what he says with a heavy grain of salt, since some of it is patently untrue, even according to his own comments.

    example: he states (correctly) that a quick sort isn't quick at all on pre-sorted lists and can in fact be twice as slow as even a bubble sort under those conditions. Then later, he states that a quick sort is 50 times faster than a bubble sort on a pre-sorted list (which is definitely not even close to being correct).

  12. #12

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking Thank you guys!

    I have not tried the module yet... As a matter of fact I just downloaded it.

    Anyway, thanks to Phinds I was able to develop my own BTree class I will show here. I have been working now in the Balancing Code... Do you have any algorithm that might do the trick?

    I don't want you to try to code it but just the algorithm would be nice. I haven't found it in my Data Structure Notebook. I wonder why I did not take notes then... I have some excercises but no algorithm.... Hmmmm...

    Oh well, here is the BTree Type so you can check it out. Thanks to all!


    VB Code:
    1. 'Nobody replied to this... So I guess you don't care for the code.
    2. 'That is why I took it off. If you want it, please feel free to ask.
    3.  
    4. If (You Want The Code) Then Ask

    I add here the module. Guys, check the code posted above works since I had to translate the name of some functions... That would be the main problem since some functions were named in Spanish for when my professor would check the code.

    In addition, if you don't feel comfortable with Spanish Names in the functions of this module, please feel free to change them. (The module is attached to the Next Reply)
    Last edited by Tec-Nico; Sep 16th, 2002 at 09:03 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  13. #13

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Arrow The Module...

    Here is the module. If you are going to use it, please don't forget to acknow my work.

    However, you could wait more until I post the final version with the DeleteNode procedure as well as Balance. Or you could also try to help out with the algorithm... It is up to you.
    Attached Files Attached Files
    Last edited by Tec-Nico; Sep 15th, 2002 at 01:09 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  14. #14

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Unhappy Isn't this Interesting?

    I noticed nobody took their time to check it... Is it that nobody likes BTrees?

    Or maybe my code must suck for you... Oh well...
    I'll keep working then.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  15. #15

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Lightbulb So far...

    So far I have been able to code the DeleteNode procedure and I am done programming the Rotations.

    RR, LL, RL and LR are already done. I also implemented some code to locate the parts which are not balanced... But Do you know how can I get the right and left height of a node with its level and the tree's right or left height?

    Any ideas?

    Oh come on... Don't tell me nobody here knows about BTrees.

    Thanks in advance, Me
    Last edited by Tec-Nico; Sep 18th, 2002 at 01:45 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  16. #16

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    :S Still Nothing?

    Why don't I get replies?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I just think a lot of people either don't know what a BTree is or really don't think it sounds like something they need, don't take it personal or anything.
    Last edited by Edneeis; Sep 18th, 2002 at 04:31 PM.

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I agree with Edneeis. Most of my work in VB is just way too simple. I've never come across a situation where a binary tree would be useful. In C++, on the other hand...

    Always innovate, ya' just never know when something will be just what is needed.

  19. #19

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Arrow If you wanted to see it...

    Here is the Binary Tree, if you want to check it and comment it I would feel happy to hear of you.

    Thanks for your help...
    Attached Files Attached Files
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  20. #20
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    What do you use this program for? Just curious.
    Please rate my post.

  21. #21

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    It is a program that shows you the Binary Tree formed by giving an array of integer numbers. The tree will be balanced of course, you can see the properties of each node by clicking on it, you can delete nodes (giving the number of the node, not the integer you gave to form the tree) And you could also perform some searching. (The code is there but I did not add a search trigger)

    This is mostly to help those people who are asked to develop a Binary Tree as they learn how to program... What do you think?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  22. #22
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you really didn't have to dig up so many old posts and post in it saying that you have a program that worked
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  23. #23

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Talking Do to others what you would like them to do to you...

    I am just doing what I would like someone to do to me. If they found a solution to a problem I had and they knew how hard it was they would find me to give me the answer of it.

    I just wanted to help... Sorry if it appeared I wanted to show off or something. But I still remember when that professor asked me for this program (Semesters ago) And how I would liked someone to help me with it.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  24. #24
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    I still don't understand what the hell's the purpose of the program. Sorry.
    Please rate my post.

  25. #25

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Er...

    1.- Create a node structure
    2.- Create a Binary tree structure
    3.- Be able to create a tree from an array
    4.- Be able to graph this tree
    5.- Be able to erase nodes from the tree
    6.- Be able to search for data in the tree
    7.- Be able to balance the tree
    8.- Be able to provide an inorder, preorder and postorder code that will let you call another procedure in them

    I guess that sumarizes it...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  26. #26
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    Tec-Nico's last answer looks too much at the details and not enough at the big picture. The point of a binary tree is that there are certain types of data manipulation algorithms that simple cannot be done in any way that is even close to the effeciency of a binary tree.

    The classic example is the need to enumerate all the identical elements in a data collection and to present an ordered list of all the elements with the number of times each occurs. There are plenty of practical applications for this, and it is just one example of a situation where there are LOTS of ways to do the job but none of them will approach a binary tree in effeciency.

    Because of this, and the fact that they are an elegant use of doubly-linked lists, binary trees are a staple of computer science classes.

  27. #27

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Amen, Phinds. I thought he asked what the program could achieve and not what the Binary Trees were for...

    I have to thank you, Phinds, you were helped me a lot so I could achieve this.

    I hope you like the program I developed with your advice.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

Posting Permissions

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



Click Here to Expand Forum to Full Width