Thread: Red Black Tree Implementaion

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Red Black Tree Implementaion

    It seems like implementing an RB tree in C++ is a hell of a nightmare.
    I have tried to look for an implementation online but couldn't find a good and clean one.

    Is there anyone here who had this nightmare before and could share his code?

    Thanks.

    By the way, for those who wonder, it is a part of a work for university ( supposed to be a tiny part, but actually it's a serious burden V_V )
    gavra.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    instead of looking for an implementation, I'd recommend looking for an explanation of how it works, and then write the implementation yourself.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by gavra View Post
    It seems like implementing an RB tree in C++ is a hell of a nightmare. I have tried to look for an implementation online but couldn't find a good and clean one.
    Is this homework (you're supposed to figure out to implement it), or is this something that university needs you to code up, even it's from some other example implementation?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you just need to USE a red-black tree, not actually implement one yourself, the standard library has std::map and std::set, which are typically implemented as a balanced red-black tree (though other implementations would be valid; the standard doesn't specify how the implementation must work).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Quote Originally Posted by Elkvis View Post
    instead of looking for an implementation, I'd recommend looking for an explanation of how it works, and then write the implementation yourself.
    Yeah, it's not that it's hard or something, it's just a lot to write and many cases to handle which I find boring and frustrating...

    Quote Originally Posted by rcgldr View Post
    Is this homework (you're supposed to figure out to implement it), or is this something that university needs you to code up, even it's from some other example implementation?
    The homework is to develop an algorithm that solves a specific problem. In order to solve it efficiently (as they demanded) I need to use a red black tree, meaning I need to implement it T_T

    Quote Originally Posted by Cat View Post
    If you just need to USE a red-black tree, not actually implement one yourself, the standard library has std::map and std::set, which are typically implemented as a balanced red-black tree (though other implementations would be valid; the standard doesn't specify how the implementation must work).
    Wish I could do that, they want it to be 'clean' code, I can't use that stuff.
    gavra.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I find red-black trees harder to perform deletion with, and so-so for insertion.

    As it seems that your requirements are not as strict as "use a red-black tree", but rather you feel that this would best meet the performance requirements, I strongly suggest you post what those performance requirements are. There are plenty of other data structures that are as performant, or more so, in many of the areas that a red-black tree is. We may be able to suggest a data structure that is easier to implement, and just as effective.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Quote Originally Posted by iMalc View Post
    I find red-black trees harder to perform deletion with, and so-so for insertion.

    As it seems that your requirements are not as strict as "use a red-black tree", but rather you feel that this would best meet the performance requirements, I strongly suggest you post what those performance requirements are. There are plenty of other data structures that are as performant, or more so, in many of the areas that a red-black tree is. We may be able to suggest a data structure that is easier to implement, and just as effective.
    The question is on the chapter of rb trees XD unfortunately..
    However, I think I'll just start to implement it hoping it'll go out well

    Thanks
    gavra.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Hi, in this book (Introduction to Algorithms) you can find a complete chapter about RB trees (properties, rotations, insertion and deletion), It includes analysis and pseudocode.

    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help -- Red Black Tree
    By Fatima Rizwan in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2011, 01:38 AM
  2. Help with red black tree
    By franciscobom in forum C Programming
    Replies: 2
    Last Post: 04-26-2011, 01:28 AM
  3. Help with red black tree
    By jabajaba in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2009, 12:36 AM
  4. Red Black Tree
    By nomes in forum C Programming
    Replies: 2
    Last Post: 10-08-2003, 08:05 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM