Mercurial > hg > Members > shoshi > AADS
annotate src/tree/AVLTreeMap.java @ 1:15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 09 Aug 2011 11:13:36 +0900 |
parents | a9cb12a7f995 |
children | 36f2373f867b |
rev | line source |
---|---|
0 | 1 package tree; |
2 | |
3 public class AVLTreeMap<K extends Comparable<K>,V> implements Map<K,V> | |
4 { | |
5 private Node<K,V> root; | |
6 private final Node<K,V> NULL_NODE = new Node<K,V>(null,null,-1,-1,null,null); | |
7 | |
8 public static void main(String args[]) | |
9 { | |
10 AVLTreeMap<Integer,String> map = new AVLTreeMap<Integer,String>(); | |
11 | |
1
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
12 map.put(10,"10"); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
13 map.put(20,"20"); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
14 map.put(30,"20"); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
15 map.put(5,"5"); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
16 map.put(7,"7"); |
0 | 17 } |
18 | |
19 public AVLTreeMap() | |
20 { | |
21 root = null; | |
22 } | |
23 | |
24 @Override | |
25 public void put(K key,V value) | |
26 { | |
27 if(root == null){ | |
28 root = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE); | |
29 return; | |
30 } | |
31 | |
32 root = _put(root,key,value); | |
33 } | |
34 | |
35 public Node<K,V> _put(Node<K,V> cur,K key,V value) | |
36 { | |
37 int result = cur.key.compareTo(key); | |
38 | |
39 if(result < 0){ | |
40 //right | |
41 if(cur.right == NULL_NODE){ | |
42 cur.right = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE); | |
43 cur.rdepth = 1; | |
44 return cur; | |
45 } | |
46 | |
47 cur.right = _put(cur.right,key,value); | |
1
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
48 cur.rdepth = Math.max(cur.right.ldepth,cur.right.rdepth) + 1; |
0 | 49 int diff = cur.rdepth - cur.ldepth; |
50 | |
51 if(diff > 1){ | |
52 //一重回転か二重回転か判断する。 | |
53 //以下の式は一重回転の場合は rottype が正になるはず。負の場合は二重回転、0の場合はあり得ない。 | |
54 int rottype = result * cur.right.key.compareTo(key); | |
55 if(rottype > 0){ | |
56 return singleRotate(cur,OP_RIGHT); | |
57 }else{ | |
58 return doubleRotate(cur,OP_RIGHTLEFT); | |
59 } | |
60 } | |
61 }else if(result > 0){ | |
62 //left | |
63 if(cur.left == NULL_NODE){ | |
64 cur.left = new Node<K,V>(key,value,0,0,NULL_NODE,NULL_NODE); | |
65 cur.ldepth = 1; | |
66 return cur; | |
67 } | |
68 | |
69 cur.left = _put(cur.left,key,value); | |
70 cur.ldepth = Math.max(cur.left.ldepth,cur.left.rdepth) + 1; | |
71 int diff = cur.ldepth - cur.rdepth; | |
72 | |
73 if(diff > 1){ | |
74 //一重回転か二重回転か判断する。 | |
75 //以下の式は一重回転の場合は rottype が正になるはず。負の場合は二重回転、0の場合はあり得ない。 | |
76 int rottype = result * cur.left.key.compareTo(key); | |
77 if(rottype > 0){ | |
78 return singleRotate(cur,OP_LEFT); | |
79 }else{ | |
80 return doubleRotate(cur,OP_LEFTRIGHT); | |
81 } | |
82 } | |
83 }else{ | |
84 //find | |
85 cur.value = value; | |
86 } | |
87 | |
88 return cur; | |
89 } | |
90 | |
1
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
91 public V remove(K key) |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
92 { |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
93 RemoveResult<K,V> r = findRemoveTarget(root,key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
94 root = r.cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
95 return r.target.value; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
96 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
97 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
98 private RemoveResult<K,V> findRemoveTarget(Node<K,V> cur,K key) |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
99 { |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
100 int result = cur.key.compareTo(key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
101 if(result < 0){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
102 //right |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
103 if(cur == NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
104 return new RemoveResult<K,V>(cur,NULL_NODE); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
105 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
106 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
107 RemoveResult<K,V> r = findRemoveTarget(cur.right,key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
108 cur.right = r.cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
109 cur.rdepth = Math.max(cur.right.rdepth,cur.right.ldepth) + 1; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
110 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
111 int diff = cur.rdepth - cur.ldepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
112 if(diff > 1){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
113 int rottype = result * cur.right.key.compareTo(key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
114 if(rottype > 0){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
115 r.cur = singleRotate(cur,OP_RIGHT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
116 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
117 }else{ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
118 r.cur = doubleRotate(cur,OP_RIGHTLEFT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
119 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
120 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
121 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
122 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
123 r.cur = cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
124 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
125 }else if(result > 0 ){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
126 //left |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
127 if(cur == NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
128 return new RemoveResult<K,V>(cur,NULL_NODE); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
129 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
130 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
131 RemoveResult<K,V> r = findRemoveTarget(cur.left,key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
132 cur.left = r.cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
133 cur.ldepth = Math.max(cur.left.rdepth,cur.left.ldepth) + 1; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
134 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
135 int diff = cur.rdepth - cur.ldepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
136 if(diff > 1){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
137 int rottype = result * cur.left.key.compareTo(key); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
138 if(rottype > 0){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
139 r.cur = singleRotate(cur,OP_LEFT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
140 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
141 }else{ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
142 r.cur = doubleRotate(cur,OP_LEFTRIGHT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
143 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
144 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
145 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
146 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
147 r.cur = cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
148 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
149 }else{ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
150 //find |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
151 if(cur.left == NULL_NODE && cur.right == NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
152 //delete |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
153 return new RemoveResult<K,V>(NULL_NODE,cur); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
154 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
155 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
156 if(cur.left != NULL_NODE && cur.right == NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
157 //replace left |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
158 return new RemoveResult<K,V>(cur.left,cur); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
159 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
160 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
161 if(cur.left == NULL_NODE && cur.right != NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
162 //replace right |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
163 return new RemoveResult<K,V>(cur.right,cur); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
164 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
165 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
166 //both nodes are not null |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
167 RemoveResult<K,V> r = findSubstituteNode(cur.left); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
168 cur.key = r.target.key; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
169 cur.value = r.target.value; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
170 cur.right = r.cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
171 cur.rdepth = Math.max(cur.ldepth,cur.rdepth) + 1; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
172 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
173 int diff = cur.rdepth - cur.ldepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
174 if(diff > 1){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
175 r.cur = doubleRotate(cur,OP_LEFTRIGHT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
176 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
177 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
178 return r; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
179 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
180 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
181 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
182 private RemoveResult<K,V> findSubstituteNode(Node<K,V> cur) |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
183 { |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
184 if(cur.right != NULL_NODE){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
185 RemoveResult<K,V> result = findSubstituteNode(cur.right); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
186 cur.right = result.cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
187 cur.rdepth = Math.max(cur.right.rdepth,cur.right.ldepth) + 1; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
188 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
189 int diff = cur.ldepth - cur.rdepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
190 if(diff > 1){ |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
191 result.cur = singleRotate(cur.left,OP_LEFT); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
192 return result; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
193 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
194 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
195 result.cur = cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
196 return result; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
197 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
198 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
199 RemoveResult<K,V> result = new RemoveResult<K,V>(cur.left,cur); |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
200 return result; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
201 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
202 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
203 private static class RemoveResult<K,V> |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
204 { |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
205 Node<K,V> cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
206 Node<K,V> target; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
207 |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
208 public RemoveResult(Node<K,V> cur,Node<K,V> target) |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
209 { |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
210 this.cur = cur; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
211 this.target = target; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
212 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
213 } |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
214 |
0 | 215 private static final int OP_LEFT = 1; |
216 private static final int OP_RIGHT = 2; | |
217 | |
218 public Node<K,V> singleRotate(Node<K,V> target,int type) | |
219 { | |
220 switch(type){ | |
221 case OP_LEFT: | |
222 Node<K,V> left = target.left; | |
223 target.left = left.right; | |
224 left.right = target; | |
225 | |
226 target.ldepth = Math.max(target.left.rdepth,target.left.ldepth) + 1; | |
227 left.rdepth = Math.max(left.right.rdepth,left.right.ldepth) + 1; | |
228 return left; | |
229 case OP_RIGHT: | |
230 Node<K,V> right = target.right; | |
231 target.right = right.left; | |
232 right.left = target; | |
233 | |
234 target.rdepth = Math.max(target.right.rdepth,target.right.ldepth) + 1; | |
235 right.ldepth = Math.max(right.left.ldepth,right.left.rdepth) + 1; | |
236 return right; | |
237 default: | |
238 throw new IllegalArgumentException("invalid rotation type"); | |
239 } | |
240 } | |
241 | |
242 private static final int OP_LEFTRIGHT = 1; | |
243 private static final int OP_RIGHTLEFT = 2; | |
244 | |
245 public Node<K,V> doubleRotate(Node<K,V> target,int type) | |
246 { | |
247 switch(type){ | |
248 case OP_LEFTRIGHT: | |
249 target.left = singleRotate(target.left,OP_RIGHT); | |
250 return singleRotate(target,OP_LEFT); | |
251 case OP_RIGHTLEFT: | |
252 target.right = singleRotate(target.right,OP_LEFT); | |
253 return singleRotate(target,OP_RIGHT); | |
254 default: | |
255 throw new IllegalArgumentException("invalid rotation type."); | |
256 } | |
257 } | |
258 | |
259 @Override | |
260 public V get(K key) | |
261 { | |
262 Node<K,V> cur = root; | |
263 | |
264 while(cur != NULL_NODE){ | |
265 int result = cur.key.compareTo(key); | |
266 if(result < 0){ | |
267 cur = cur.right; | |
268 }else if(result > 0){ | |
269 cur = cur.left; | |
270 }else{ | |
271 //find | |
272 break; | |
273 } | |
274 } | |
275 | |
276 return cur.value; | |
277 } | |
278 | |
279 private static class Node<K,V> | |
280 { | |
281 K key; | |
282 V value; | |
283 | |
1
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
284 int rdepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
285 int ldepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
286 |
0 | 287 Node<K,V> left; |
288 Node<K,V> right; | |
289 | |
290 public Node(K key,V value,int rdepth,int ldepth,Node<K,V> left,Node<K,V> right) | |
291 { | |
1
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
292 this.right = right; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
293 this.left = left; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
294 this.ldepth = ldepth; |
15920e9b562d
added MulticastQueue.java the BlockingMulticastQueue
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
295 this.rdepth = rdepth; |
0 | 296 this.key = key; |
297 this.value = value; | |
298 } | |
299 | |
300 @Override | |
301 public String toString() | |
302 { | |
303 return "[" + key + ":" + value + "]"; | |
304 } | |
305 } | |
306 } |