How a tree uses the red ebony algorithm

I have read many articles about red ebony where O (log n) time is required for operations. I don’t really understand how it works and how the tree map actually uses the red ebony algorithm to balance the tree compared to binary tree search.

Link Link https://www.topcoder.com/community/data-science/data-science-tutorials/an-introduction-to-binary-search-and-red-black-trees/

Can someone explain how the algorithm works.

+6
source share
2 answers

Red-black tree - binary search tree. It's just a BST flavor that has fancy versions of insert and delete operations that reorganize the tree as it starts, so the tree never becomes "long and stiff." The longer and stricter the tree gets, the more it behaves like a linked list. On average, operations with a linked list require half the list to be touched (or the entire list in the worst case), so the execution time changes linearly (O (n) in the number of elements n). When the tree is "thick" or almost balanced, then the operations are much cheaper (O (log n)) each. Red-black algorithms ensure that the tree remains dense.

To make it concrete, here are two trees that store keys from A to G. The left ones are long and hard. Pay attention to what the list looks like. In the worst case, 4 key comparisons are required to search for an item. The tree on the right is thick. He needs only 3. Here the difference is small. For a tree of 1023 elements for a string tree, 512 comparisons are required, but only a thick one - 10. This is the power of log n.

  B _D_ / \ / \ ADBF / \ / \ / \ CFACEG / \ EG 

A red-black tree is not guaranteed to be absolutely thick (in the correct terminology it is "perfectly balanced"), but the rules of red-black tree guarantee that it is sufficiently thick mathematically strict, so that the operating time varies depending on the log n, and not linearly in n.

The genius of red-black rules is that they are "local." During insertion or deletion that violate the rules, you can restore them by adjusting only a constant number of nodes for each node affected by the operation. Therefore, they are cheap and quite simple to implement.

However, when the red-black rules are true for the whole tree, it can be shown with the help of clever mathematical proof that it is thick enough, as described above.

How about a map of trees? A map is a function with a domain called a set of keys K and a range called a set of values ​​V. To implement a map of trees, each node stores a pair of key values <k,v> , where k \in K and v \in V The figures above (and most presentations) show only the keys (letters AG). On the map, insertion, search, and deletion work on these pairs in a rather obvious way. For example, a search searches for a key using the usual BST algorithm. When a key is found, the value is also found because it is in the same pair. It is back. In java, a couple is called Map.Entry . You can check this in the Java source code .

I will not go into details about how red-black rules work, because I could not improve the Wikipedia page . I assume and hope that you are missing the "big picture", so now the discussion will make sense. The good news is that almost all languages ​​provide a thoroughly tested and performance-optimized tree implementation, so knowledge of the secret details of the rotation is not required. Of course, if you are interested and just want to know, have it! Congratulations!

For what it's worth, Top Coder explains the algorithms not always clearer. Hunt around for others until you need a “click”. Dear CS textbooks are respected for one reason: their explanations are excellent. Corman and Rivest is a recognized favorite.

+11
source

First of all, you should clarify your question. Indicate that you know what you are not doing and what you have tried.

Coming to the question, TreeMap and Red-black trees are completely different concepts. The conceptual understanding of one or the other is independent of the other, and I suggest you not mix them. I will not give you an exact answer; rather, I will give a brief overview of the concepts and the sequence in which you should study them (IMO).

Map Data Structures

  • Map
  • Map types - HashMap, SortedMap, TreeMap, etc.

Tree data structures

  • Wood
  • Binary tree
  • Binary Search Tree (BST)
  • Balanced BST
  • Self-balancing BST - AVL tree, Red-Black tree, etc.

I assume that you know the concept of Maps and binary search trees. If not, a simple search will lead you to many good resources.

Now, to the actual answer.

First of all, you should know what SortedMap and Self-balancing BST are.

What is a SorteMap?

From Java docs ,

A card that further provides complete order on its keys. The card is ordered according to the natural order of its keys or the comparator typically provided during map sorting.

A SortedMap is used if you want the base key, pairs of values ​​sorted (by key). Thus, it would be easier to get minimum, maximum, or perform range-based operations.

What is a self-balancing binary search tree?

From wikipedia ,

A self-balancing (or height-balanced) binary search tree is any tree with a node-based binary search that automatically keeps its height (maximum number of levels below the root) small compared to arbitrary insertions and deletions of elements.

Again, red-black wood in one implementation of Self-balancing BST. There are others like ALV tree etc. The worst case for any operation on a regular BST is O(n) . The main advantage of using Balanced BST over regular / unbalanced BST is that the worst case of all operations comes down to O(logn) with little overhead associated with node permutation during insert / delete. Check it out .

So what is TreeMap?

A TreeMap is an implementation of SortedMap .

How are TreeMap and Red-black tree related?

No, it is not. Theoretically, you can use any of the binary search trees to implement TreeMap. To get good results, we use the Self-balancing Binary Search Trees, which have less time complexity for insert, delete and extract operations. In the case of Java, a red-black tree is used. I do not know the exact reason why red-black wood is used, but I believe that there is one.

+2
source

All Articles