Complete code example of red black tree implemented by Java algorithm
Red black tree
definition
Red black tree (English: redcblack tree) is a self balancing binary search tree. It is a data structure used in computer science. Its typical purpose is to realize associative array.
Another definition of a red black tree is a binary lookup tree that contains red black links and meets the following conditions:
Red links are left links; No node is connected with two red links at the same time; The tree is perfectly black balanced, that is, the number of black links on the path of any empty link to the root node is the same.
The red black tree satisfying this definition corresponds to the corresponding 2-3 tree one by one.
rotate
Rotation is divided into left rotation and right rotation. Usually, the left rotation operation is used to rotate a red link tilted to the right into a left link. Comparing before and after the operation, we can see that the operation actually moves the larger node of the two nodes linked by the red line to the root node.
The left-hand operation is shown in the figure below:
The right-hand operation is shown in the figure below:
Namely:
Complexity
The average height of red black trees is about LGN.
The following figure shows the time complexity of red black tree in various cases. It can be seen that red black tree is an implementation of 2-3 search tree, which can ensure that it still has logarithmic time complexity in the worst case.
Java code
Output:
summary
The above is all about the complete code example of Java algorithm implementation of red black tree in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: the usage code example of list in Java collection, the service number payment code example of Java wechat payment, a quick understanding of the combination mode in Java design mode, etc. if you have any questions, you can leave a message at any time, and the Xiaobian will reply to you in time. Thank you for your support!