the implemention of redblack tree

Posted on 2017-06-12 23:34  Wujunde  阅读(142)  评论(0编辑  收藏  举报
  1 public class redbalcktree {
  2     
  3      private  class Node{
  4            private int val;
  5            private int key;
  6            boolean color;    //black true
  7            private Node left,right,p;
  8            private int N;   //the number of the all nodes of its child nodes and itself
  9            //private int num;//the number
 10            public Node(int val,int key){
 11                this.val = val; this.key = key;
 12            }
 13        
 14  }
 15      
 16      Node root;
 17     
 18     private void leftrotation(Node x){
 19         Node y = x.right;
 20         x.right = y.left;
 21         if(y.left != null){
 22             y.left.p = x;
 23         }
 24         y.p = x.p;
 25         if(x.p == null){
 26             root = y;
 27         }
 28         else if(x == x.p.left){
 29             x.p.left = y;
 30         }
 31         else{x.p.right = y;}
 32         y.left = x;
 33         x.p = y;
 34     }
 35     
 36     private void rightrotation(Node y){
 37         Node x = y.left;
 38         y.left = x.right;
 39         if(x.right != null){
 40             x.right.p = y;
 41         }
 42         x.p = y.p;
 43         if(y.p == null){
 44             root = x;
 45         }
 46         else if(y == y.p.left){
 47             y.p.left = x;
 48         }
 49         else{y.p.right = x;}
 50         x.right = y;
 51         y.p = x;
 52     }
 53     
 54     public void insert(int val,int key){
 55         Node y = null;
 56         Node x = root;
 57         Node z = new Node(val,key);
 58         if(x == null){root = z;}
 59         while(x != null){
 60             y = x;
 61             if(z.key < x.key){
 62                 x = x.left;
 63             }
 64             else{x = x.right;}
 65         }
 66         z.p = y;
 67        if(z.key < y.key){
 68            y.left = z;
 69        }
 70        else{y.right = z;}
 71        z.left = null;
 72        z.right = null;
 73        z.color = false;
 74        inseretfix(z);
 75         
 76     }
 77     
 78     private void inseretfix(Node z){
 79         Node y;
 80         while (z.p.color == false){
 81             if(z.p == z.p.p.left){
 82                 y = z.p.p.right;
 83                 if(y.color == false){   //case 1,both z.p and y(uncle) are red
 84                     z.p.color = true;
 85                     y.color = true;
 86                     z.p.p.color = false;
 87                     z= z.p.p;
 88                     continue;
 89                 }
 90                 else if(z == z.p.right){  //case 2, z.p is red,uncle is black,z is right,transfer to case 3
 91                     z = z.p;
 92                     leftrotation(z);
 93                 }
 94                 z.p.color = true;        //case 3, z.p is red,uncle is black,z is left;
 95                 z.p.p.color = false;
 96                 rightrotation(z.p.p);
 97             }
 98             else{   //set y(uncle) and change rotation
 99                 y = z.p.p.left;
100                 if(y.color == false){   //case 1,both z.p and y(uncle) are red
101                     z.p.color = true;
102                     y.color = true;
103                     z.p.p.color = false;
104                     z= z.p.p;
105                     continue;
106                 }
107                 else if(z == z.p.right){  //case 2, z.p is red,uncle is black,z is right,transfer to case 3
108                     z = z.p;
109                     rightrotation(z);
110                 }
111                 z.p.color = true;        //case 3, z.p is red,uncle is black,z is left;
112                 z.p.p.color = false;
113                 leftrotation(z.p.p);
114             }
115         }
116         root.color = true;
117     }
118     
119     private int get(Node x,int key){
120         if(x == null){
121             throw new NullPointerException("have't this key");
122         }
123         
124         if(x.key>key){
125             return get(x.left,key);
126         }
127         else if(x.key < key){
128             return get(x.right,key);
129         }
130         else{
131             return x.val;
132         }
133     }
134     
135     private void transplant(Node u,Node v){  //substitute u as v
136         if(u.p == null){
137             root = v;
138         }
139         else if(u == u.p.left){
140             u.p.left = v;
141         }
142         else {
143             u.p.right = v;
144         }
145         v.p = u.p;
146     }
147     
148      private Node deletemin(Node x){
149          if(x.left == null){return x.right;}
150          x.left = deletemin(x.left);
151          return x;
152          }
153     
154     public void delete(int key){
155         int val = get(root,key);
156         Node z = new Node(val,key);
157         Node x;
158         Node y = z;
159         boolean original = y.color;
160         if(z.left == null){
161             x = z.right;
162             transplant(z,z.right);
163         }
164         else if(z.right == null){
165             x = z.left;
166             transplant(z,z.left);
167         }
168         else{
169             y = deletemin(z.right);   // next node after z
170             original = y.color;
171             x = y.right;
172             if(y.p == z){
173                 x.p = y;
174             }
175             else{
176                 transplant(y,y.right); // put y.right on the original y's position
177                 y.right = z.right;
178                 y.right.p = y;
179             }
180             transplant(z,y);
181             y.left = z.left;
182             y.left.p = y;
183             y.color = z.color;
184         }
185         if(original == true){
186             deletefix(x);
187         }
188     }
189     
190     
191     private void deletefix(Node x) {
192        Node other;
193         while ((x.color = true) && (x != root)) {
194             if (x.p.left == x) {
195                 other = x.p.right;
196                 if (other.color == false) {
197                     // Case 1: x's brother is red 
198                     other.color = true;
199                     x.p.color = false;
200                     leftrotation(x.p);
201                     other = x.p.right;
202                 }
203 
204                 if ((other.left.color == true) &&
205                     (other.right.color == true) ){
206                     // Case 2: x's black is black and w 's both child are black 
207                     other.color = false;
208                     x = x.p;
209                     continue;
210                 } else if (other.right.color == true) {
211                         // Case 3: transfer to case 4  
212                        other.left.color = true;
213                        other.color = false;
214                         rightrotation(other);
215                         other = x.p.right;
216                     }
217                     // Case 4:  x's black is black,w's right child is red ,left anyway
218                  other.color = x.p.color;
219                    x.p.color = true;
220                     other.right.color = true;
221                     leftrotation(x.p);
222                    x = root;
223                     break;
224                 }
225              else {
226 
227                  other = x.p.left;
228                     if (other.color == false) {
229                         // Case 1: x's brother is red 
230                         other.color = true;
231                         x.p.color = false;
232                         rightrotation(x.p);
233                         other = x.p.right;
234                     }
235 
236                     if ((other.left.color == true) &&
237                         (other.right.color == true) ){
238                         // Case 2: x's black is black and w 's both child are black 
239                         other.color = false;
240                         x = x.p;
241                         continue;
242                     } else if (other.right.color == true) {
243                             // Case 3: transfer to case 4  
244                            other.left.color = true;
245                            other.color = false;
246                             leftrotation(other);
247                             other = x.p.right;
248                         }
249                         // Case 4:  x's black is black,w's right child is red ,left anyway
250                      other.color = x.p.color;
251                        x.p.color = true;
252                         other.right.color = true;
253                         rightrotation(x.p);
254                        x = root;
255                         break;
256                 }
257 
258             }
259         }
260 
261         
262     
263     
264 }