Red-Black Tree Insert Java implementation

Source: Internet
Author: User

 Packagepractice; Public classTestmain { Public Static voidMain (string[] args) {int[] ao = {5, 1, 18, 3, 8, 20, 13, 16, 12}; Integer[] a=NewInteger[9];  for(inti = 0; i < a.length; i++) {A[i]=NewInteger (Ao[i]); } Redblackbst<Integer,String> tree=NewRedblackbst<integer,string>();  for(inti = 0; i < a.length; i++) {Tree.put (A[i], a[i].tostring ());    } tree.print (); }}/*
* Red and black trees
* Compared to 2-3 trees, red links represent the same location as the 3-node 4-node in the 2-3 tree (the red link is drawn flat)
*/
classRedblackbst <kextendsComparable<k>, v> { Private Static Final BooleanRED =true; Private Static Final BooleanBLACK =false; PrivateNode Root; Private classnode{K Key; V value; Node left, right; intN; Booleancolor; PublicNode (K key, V value,intNBooleancolor) { This. Key =key; This. Value =value; This. N =N; This. color =color; } } /** Insert Node*/ Public voidput (K key, V value) {root=put (root, key, value); } Privatenode put (node node, K key, V value) {if(node = =NULL) {return NewNode (Key, value, 1, RED);} /** The front part is consistent with the two-fork tree insertion, and the red-black tree is inserted and adjusted like a binary tree (rotation, color conversion)*/ if(Compare (key, Node.key) < 0) {Node.left =put (Node.left, key, value);} Else if(Compare (key, Node.key) > 0) {node.right =put (node.right, key, value);} Else{Node.value =value;} /*
* Here the rotation is to ensure that the "same position" (that is, a node has two red links) three nodes from small to large sorted and intermediate nodes attached to their parent node
* Color conversion is the "grow up" in the 2-3 tree
*/
if(!isred (Node.left) &&isred (node.right)) {node = rorateleft (node);}//L - if(Isred (Node.left) &&isred (node.left.left)) {node = rorateright (node);}//right-handed if(Isred (Node.left) &&isred (node.right)) {flipcolors (node);}//Color Conversionnode. N= Size (Node.left) + size (node.right) + 1; returnnode; } /** Node left parent node is dad red node is son (Dad is the diagram of the e son of the graph of s) * When the right child node is red and the left child node is black, this node is dad, the right child node for son*/ PrivateNode rorateleft (node Dad) {node son=Dad.right; Dad.right=Son.left; Son.left=dad; Son.color=Dad.color; Dad.color=RED; Son. N=Dad. N Dad. N= Size (Dad.left) + size (dad.right) +1; returnSon; } /** Node Right-hand parent node is dad red node is son * When left dial hand node is red and its left child node is red, this node is dad and left child node is son right-handed*/ PrivateNode rorateright (node Dad) {node son=Dad.left; Dad.left=Son.right; Son.right=dad; Son.color=Dad.color; Dad.color=RED; Son. N=Dad. N Dad. N= Size (Dad.left) + size (dad.right) +1; returnSon; } /** Color Conversion * If the left and right child nodes of this node are red, turn them into black and turn themselves red*/ Private voidflipcolors (node node) {Node.color=RED; Node.left.color=BLACK; Node.right.color=BLACK; } /** Whether it is red*/ Private Booleanisred (node node) {if(node = =NULL) {return false;} returnNode.color = =RED; } /** Size of the tree*/ Private intSize (node node) {if(node = =NULL) {return0;} Else{returnnode. N;} } /** Key1 < key2-1 * Key1 > Key2 1 * key1 = key2 0*/ Private intCompare (k Key1, K key2) {returnKey1.compareto (Key2); } Public voidprint () {print (root); } Private voidPrint (node node) {if(node = =NULL) { return; } print (Node.left); System.out.println ("Key =" +node.key+ "node. N = "+node. N); Print (node.right); }}

Insert Diagram (s,e,a,r,c,h,x,m,p corresponds to 5,1,18,3,8,20,13,16)

Left rotation

Rotate Right

Color conversion

Red-Black Tree Insert Java implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.