Tech Off Thread

17 posts

AVL tree vs red-black tree

Back to Forum: Tech Off
  • Mr Crash

    What is the difference between AVL tree and red-black tree ?

    What is each of them used for ?

    What about performance, etc..

    ( Not much information out there (on the internet) so i hope some experts could explain Smiley )

  • MasterPi

    I'm sensing a CS hw question.

     

    Not much information on the internet? The wikipedia articles for AVL Tree and Red-black tree didn't help you?

  • Mr Crash

    MasterPie said:

    I'm sensing a CS hw question.

     

    Not much information on the internet? The wikipedia articles for AVL Tree and Red-black tree didn't help you?

    "CS hw question"
    a what ?

     

    >Not much information on the internet? The wikipedia articles for AVL Tree and Red-black tree didn't help you?

     

    No it didn't, it didn't describe it in a way that i could understand (i don't have a computer science degree), and those too articles didn't describe a comparison of the two trees either.

     

    So now that we have gotten that out of the way, can anybody actually answer the questions, please ?

  • ManipUni

    Your question makes no sense. If you plan on using them then you'd already have a basic idea what they'd be used for. Reads like a homework assignment to me. Maybe you should go study them so you can answer these questions yourself?

  • Mr Crash

    ManipUni said:

    Your question makes no sense. If you plan on using them then you'd already have a basic idea what they'd be used for. Reads like a homework assignment to me. Maybe you should go study them so you can answer these questions yourself?

    ah so that's what MasterPie meant.

     

    Well i can assure you that it is not a homework assignment.

    Yes i have a somewhat limited basic idea of the trees but i want to hear what the experts think.

     

    No offense but why all this interrogation, all the time, it's just time wasting, i'm not joining the cia or anything like that i just want to hear what the experts think, is that so wrong ?

     

    I thought c9 was a friendly tech community and instead i get interrogated like a student (criminal ?)

     

    Is it wrong to ask questions here too ? Am i doing something wrong ? I'm getting sick of getting interrogated all the time.

     

    If you don't want to help me verify my knowledge about these trees then go away please you're not helping.

  • Charles

    Mr Crash said:
    ManipUni said:
    *snip*

    ah so that's what MasterPie meant.

     

    Well i can assure you that it is not a homework assignment.

    Yes i have a somewhat limited basic idea of the trees but i want to hear what the experts think.

     

    No offense but why all this interrogation, all the time, it's just time wasting, i'm not joining the cia or anything like that i just want to hear what the experts think, is that so wrong ?

     

    I thought c9 was a friendly tech community and instead i get interrogated like a student (criminal ?)

     

    Is it wrong to ask questions here too ? Am i doing something wrong ? I'm getting sick of getting interrogated all the time.

     

    If you don't want to help me verify my knowledge about these trees then go away please you're not helping.

    You are free to ask questions here. Some Niners can be harsh sometimes, but it's really just tough love. Smiley

     

    Be nice, guys. That said, when you ask a question in a public forum you need to be prepared to deal with some not-so-expected responses, some of which may come across as harsh. Don't have a thin skin. Keep on asking questions. Chances are that neither of the responders have a solid understanding of the data structures in question either Smiley. The links referenced by MasterPie aren't too complex in nature. You need to first understand what a binary search tree is - do you? If not, spend some quality time learning about this data structure and then move on to the more complex forms of it.

     

    C

  • Dexter

    Mr Crash said:
    ManipUni said:
    *snip*

    ah so that's what MasterPie meant.

     

    Well i can assure you that it is not a homework assignment.

    Yes i have a somewhat limited basic idea of the trees but i want to hear what the experts think.

     

    No offense but why all this interrogation, all the time, it's just time wasting, i'm not joining the cia or anything like that i just want to hear what the experts think, is that so wrong ?

     

    I thought c9 was a friendly tech community and instead i get interrogated like a student (criminal ?)

     

    Is it wrong to ask questions here too ? Am i doing something wrong ? I'm getting sick of getting interrogated all the time.

     

    If you don't want to help me verify my knowledge about these trees then go away please you're not helping.

    i want to hear what the experts think

     

    Well, an expert will likely talk in terms of complexity, tree depth and so on. Since you say you didn't understand the Wikipedia page why do you expect that you will understand what an expert has to say?

     

    All the information you want is there on the web but you have to read the right lines and skip the math. For example (from the above mentioned Wikipedia page on AVL trees):

     

    AVL trees are often compared with red-black trees because they support the same set of operations and because red-black trees also take O(log n) time for the basic operations. AVL trees perform better than red-black trees for lookup-intensive applications

     

    Both AVL trees and red-black trees are self-balancing binary search trees, so they are very similar mathematically. The operations to balance the trees are different, but both occur in constant time. The real difference between the two is the limiting height

     

    The AVL tree is more rigidly balanced than Red-Black trees, leading to slower insertion and removal but faster retrieval.

     

  • rhm

    I'm going to go out on a limb and say that RB-trees and AVL-trees are both largely redundant in contemporary software engineering.

     

    They reflected the way computers used to work: when it was just as fast to access any address in memory it made sense to store large amounts of ordered data in a low-arity binary tree. Now that's not the case and the less random the memory access patterns are, the faster you perform.

     

    On contemporary hardware, for a small number of items use an array. Inserts and deletes mean copying elements if you want to keep it sorted, but the way caches work, this is fast. If you have a large amount of data use a hash table if you don't need to maintain order. If you do need to maintain order, something like a b+tree would be a better choice (yes, even in RAM) than a binary tree.

     

    Most applications won't need to retain an ordered collection of elements large enough to make a tree useful though, which I assume is why there are no tree-based collections in the .NET framwork. Usually your application, just by virtue of its design domain, would split data in-memory up into multiple levels such that the number of elements in a single collection is low enough that the array-based collection performs well enough.

  • Mr Crash

    [Charles]

    Yes, i do have thick skin.

    I'm just completely sick and tired of it.

    I always google before asking in forums, etc. because it always goes like this:

    1. ask the question(s)

    2. lot's of interrigation question and answers (to make them stop, basically)

    3. the thread dies a slow death without the original questions being answered and lots of time wasted for nothing

     

    [Dexter]

    Yes, i did read that before creating a thread here.

    On some further contemplation, my questions was unclear.

     

    Not sure how to phrase the question, it's more the experience of using the trees i'm after,

    how they feel to work with in real-life.

    reading the math (if you understand it) is something but to actually use the trees is something else

    it's like the unwritten gotchas i'm asking about.

    ah, like an interviewer asking: "how has it been working with these tree algorithms?"


    Hope i cleared things up a bit.

  • Mr Crash

    rhm said:

    I'm going to go out on a limb and say that RB-trees and AVL-trees are both largely redundant in contemporary software engineering.

     

    They reflected the way computers used to work: when it was just as fast to access any address in memory it made sense to store large amounts of ordered data in a low-arity binary tree. Now that's not the case and the less random the memory access patterns are, the faster you perform.

     

    On contemporary hardware, for a small number of items use an array. Inserts and deletes mean copying elements if you want to keep it sorted, but the way caches work, this is fast. If you have a large amount of data use a hash table if you don't need to maintain order. If you do need to maintain order, something like a b+tree would be a better choice (yes, even in RAM) than a binary tree.

     

    Most applications won't need to retain an ordered collection of elements large enough to make a tree useful though, which I assume is why there are no tree-based collections in the .NET framwork. Usually your application, just by virtue of its design domain, would split data in-memory up into multiple levels such that the number of elements in a single collection is low enough that the array-based collection performs well enough.

    Based on what i've read and seen ,suggest/say we still need these kinds of algorithms for performance ( example, file systems )

     

    .NET (one of my favorite things to pick on) if you've chosen .net to begin with you're not concerned with performance (primarily) to begin with.

    That said, if there isn't any tree algorithms in .net then it would just be an oversight, i'm guessing, or perhaps feature prioritization.

     

  • Dexter

    Mr Crash said:
    rhm said:
    *snip*

    Based on what i've read and seen ,suggest/say we still need these kinds of algorithms for performance ( example, file systems )

     

    .NET (one of my favorite things to pick on) if you've chosen .net to begin with you're not concerned with performance (primarily) to begin with.

    That said, if there isn't any tree algorithms in .net then it would just be an oversight, i'm guessing, or perhaps feature prioritization.

     

    It's rare these days for someone to deal with these data structures directly. For example .NET's SortedDictionary and SortedSet use a red black tree under the covers. These 2 offer the performance characteristics of rb trees and an easy to use add/search/remove interface. There's little reason for rb tree (or any other kind of binary tree) to be exposed directly by .NET's  base class library.

     

    And yes, filesystems use trees for file lookup but they usually use B-trees, not RB/AVL trees. B-trees are better suited to the block based access that hard disks offer.

  • MasterPi

    Mr Crash said:
    ManipUni said:
    *snip*

    ah so that's what MasterPie meant.

     

    Well i can assure you that it is not a homework assignment.

    Yes i have a somewhat limited basic idea of the trees but i want to hear what the experts think.

     

    No offense but why all this interrogation, all the time, it's just time wasting, i'm not joining the cia or anything like that i just want to hear what the experts think, is that so wrong ?

     

    I thought c9 was a friendly tech community and instead i get interrogated like a student (criminal ?)

     

    Is it wrong to ask questions here too ? Am i doing something wrong ? I'm getting sick of getting interrogated all the time.

     

    If you don't want to help me verify my knowledge about these trees then go away please you're not helping.

    Forgive me for thinking you were asking a hw question. It's very typical for CS students to come onto a forum or QandA site to ask for help on some homework problem. Their posts generally are vague and of "hit and run...ask question, get quick answer, leave" form, prompting an answer that can at times be a 500 word essay. Sadly, questions on CS homework (I'm a CS student btw) are just as vague (as well as uninteresting) as those. Additionally, they're questions that are really only asked in a CS undergrad context.

     

    Thus, you have to understand why some of us might jump the gun when we see questions that follow the same pattern. While we would happily answer a CS hw question, I and many others would prefer that students find the answer on their own as hard work will only benefit them in the end.

     

    But you've clarified your question and added some context. My apologies for classifying you in the CS slackers group. Tongue Out

     

    As Dexter and others have said, people very rarely worry about each specific data structure in the context of a large application or system. If you worry too much about why basic arithmetic works the way it does, then you'll waste time that could be better spent solving the complex problem. Not to say that it's something you shouldn't care about at all, especially if you're in the process of designing a new tree based algorithm. But, if you're trying to solve a problem or create an app, you might be better off defaulting to your language's premade data structures as a lot of thought has already gone in to the construction of these implementations.

     

    Actually, I think that (correct me if I'm wrong guys) either Java's or .NET's list implementations will morph their data structure backings so as to work appropriately for your data. Though, this could just be my imagination. Wink

  • Dexter

    MasterPie said:
    Mr Crash said:
    *snip*

    Forgive me for thinking you were asking a hw question. It's very typical for CS students to come onto a forum or QandA site to ask for help on some homework problem. Their posts generally are vague and of "hit and run...ask question, get quick answer, leave" form, prompting an answer that can at times be a 500 word essay. Sadly, questions on CS homework (I'm a CS student btw) are just as vague (as well as uninteresting) as those. Additionally, they're questions that are really only asked in a CS undergrad context.

     

    Thus, you have to understand why some of us might jump the gun when we see questions that follow the same pattern. While we would happily answer a CS hw question, I and many others would prefer that students find the answer on their own as hard work will only benefit them in the end.

     

    But you've clarified your question and added some context. My apologies for classifying you in the CS slackers group. Tongue Out

     

    As Dexter and others have said, people very rarely worry about each specific data structure in the context of a large application or system. If you worry too much about why basic arithmetic works the way it does, then you'll waste time that could be better spent solving the complex problem. Not to say that it's something you shouldn't care about at all, especially if you're in the process of designing a new tree based algorithm. But, if you're trying to solve a problem or create an app, you might be better off defaulting to your language's premade data structures as a lot of thought has already gone in to the construction of these implementations.

     

    Actually, I think that (correct me if I'm wrong guys) either Java's or .NET's list implementations will morph their data structure backings so as to work appropriately for your data. Though, this could just be my imagination. Wink

    Java's or .NET's list implementations will morph their data structure backings so as to work appropriately for your data

     

    To the best of my knowledge only HybridDictionary fits this description. It uses a list when it contains only a few elements and a hashtable otherwise.

     

    Quick summary:

    - List<T>, Queue, Stack, ArrayList, SortedList - array

    - Dictionary<K, V>, HashSet<T>, Hashtable - hashtable

    - SortedDictionary<K, V>, SortedSet<T> - red-black tree

     

  • MasterPi

    Dexter said:
    MasterPie said:
    *snip*

     

    To the best of my knowledge only HybridDictionary fits this description. It uses a list when it contains only a few elements and a hashtable otherwise.

     

    Quick summary:

    - List<T>, Queue, Stack, ArrayList, SortedList - array

    - Dictionary<K, V>, HashSet<T>, Hashtable - hashtable

    - SortedDictionary<K, V>, SortedSet<T> - red-black tree

     

    Ah, that's probably what I was looking at.

  • Mr Crash

    MasterPie said:
    Dexter said:
    *snip*

    Ah, that's probably what I was looking at.

    Back to the original question:

    Yes it might be rare (these days) but it is still good to know, in case you need to ex. optimize for a specific usage, etc.

     

    So can anyone here put some experience on the table ?

    Smiley

  • Marhaw

    @Mr Crash:Todas estas respostas são evazivas porque vocês, personagens  do forum, não sabem porra nenhuma... Sr. Crash, a resposta para sua pergunta com relação ao seu trabalho de casa é a seguinte:

    Toda árvore Rubro Negra (red-black) é AVL, porém a recíproca não é verdadeira. Nem toda árvore AVL é Rubro Negra. As arvores Rubro negras são arvores tipicamente balanceadas, contudo as arvores AVL são arvores com uma balanceamento mais rígido, isto porque a sua estrutura requer a diferença de -1 nivel entre as sub-arvores esquerda e direita. Esta estrutura força alguns casos especiais onde não podem ser aplicados a arvore Rubro negras. Como por exemplo: no caso em que temos uma raiz com um único filho (esquerdo ou direito), sendo um dos lados com sub-arvore vazia. Neste caso ela é uma arvore AVL mas não é uma arvore rubro... a respostas para essas perguntas, sào triviais, basta analisar as caracteísticas dos dois tipos de árvores. IF YOU DONT' SPEAK PORTUGUESE, I'M SORRY, BUT NOT DESIST, USE O TRASLATE OF GOOGLE. Obrigado valeu.

  • PerfectPhase

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.