Changeset 6461
- Timestamp:
- 02/10/08 02:47:49 (5 years ago)
- Location:
- lang/c/rbtree/branches/bst_inherit_impl
- Files:
-
- 5 modified
-
ChangeLog (modified) (1 diff)
-
bstree.c (modified) (1 diff)
-
bstree.h (modified) (1 diff)
-
bstree_test/bstree_spec.rb (modified) (1 diff)
-
bstree_test/bstree_test_module.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/rbtree/branches/bst_inherit_impl/ChangeLog
r6431 r6461 1 2008-02-10 Kenta Murata <muraken@gmail.com> 2 3 * bstree.c: Left and right rotation are implemented. 4 5 * bstree_test/bstree_test_module.c, bstree_test/bstree_spec.rb: 6 Tests for left and right rotation are implemented. 7 1 8 2008-02-09 Kenta Murata <mrkn@gmail.com> 2 9 -
lang/c/rbtree/branches/bst_inherit_impl/bstree.c
r6431 r6461 75 75 76 76 struct bstree_node* 77 bstree_node_rotate_left(struct bstree_node* node) 78 { 79 if (0 == node) return 0; 80 81 node->right->parent = node->parent; 82 node->parent = node->right; 83 node->right = node->parent->left; 84 node->parent->left = node; 85 86 return node->parent; 87 } 88 89 struct bstree_node* 90 bstree_node_rotate_right(struct bstree_node* node) 91 { 92 if (0 == node) return 0; 93 94 node->left->parent = node->parent; 95 node->parent = node->left; 96 node->left = node->parent->right; 97 node->parent->right = node; 98 99 return node->parent; 100 } 101 102 struct bstree_node* 77 103 bstree_node_lookup_lower_bound(struct bstree_node* root, 78 104 bstree_compare_f compare, -
lang/c/rbtree/branches/bst_inherit_impl/bstree.h
r6431 r6461 129 129 #define bstree_node_is_maximum(node) (0==bstree_node_next(node)) 130 130 131 struct bstree_node* bstree_node_rotate_left(struct bstree_node* node); 132 struct bstree_node* bstree_node_rotate_right(struct bstree_node* node); 133 131 134 /* returns the maximum node which has a value less than or equal to 132 135 * the given value. */ -
lang/c/rbtree/branches/bst_inherit_impl/bstree_test/bstree_spec.rb
r6431 r6461 167 167 end 168 168 169 describe BSTreeNode, "in terms of rotation" do 170 before do 171 @a = BSTreeNode.new 'A' 172 @b = BSTreeNode.new 'B' 173 @c = BSTreeNode.new 'C' 174 @a.children = @b, @c 175 @d = BSTreeNode.new 'D' 176 @e = BSTreeNode.new 'E' 177 @b.children = @d, @e 178 @f = BSTreeNode.new 'F' 179 @g = BSTreeNode.new 'G' 180 @c.children = @f, @g 181 end 182 183 it "check precondition" do 184 @a.parent.should be_nil 185 @a.left.should == @b 186 @a.right.should == @c 187 @b.parent.should == @a 188 @b.left.should == @d 189 @b.right.should == @e 190 @d.parent.should == @b 191 @d.left.should be_nil 192 @d.right.should be_nil 193 @e.parent.should == @b 194 @e.left.should be_nil 195 @e.right.should be_nil 196 @c.parent.should == @a 197 @c.left.should == @f 198 @c.right.should == @g 199 @f.parent.should == @c 200 @f.left.should be_nil 201 @f.right.should be_nil 202 @g.parent.should == @c 203 @g.left.should be_nil 204 @g.right.should be_nil 205 end 206 207 # left rotation at the node `A' 208 # 209 # A C 210 # ,^. ,^. 211 # B C => A G 212 # ,^. ,^. 213 # F G B F 214 it "@a.rotate_left should returns @c" + 215 ", @c.parent is nil, @c.left is @a" + 216 ", @a.parent is @c, and @a.right is @f" do 217 @a.rotate_left.should == @c 218 @c.parent.should be_nil 219 @c.left.should == @a 220 @c.right.should == @g 221 @a.parent.should == @c 222 @a.left.should == @b 223 @a.right.should == @f 224 end 225 226 # right rotation at the node `A' 227 # 228 # A B 229 # ,^. ,^. 230 # B C => D A 231 # ,^. ,^. 232 # D E E C 233 it "@a.rotate_right should returns @b" + 234 ", @b.parent is nil, @b.right is @a" + 235 ", @a.parent is @b, and @a.left is @e" do 236 @a.rotate_right.should == @b 237 @b.parent.should be_nil 238 @b.left.should == @d 239 @b.right.should == @a 240 @a.parent.should == @b 241 @a.left.should == @e 242 @a.right.should == @c 243 end 244 end -
lang/c/rbtree/branches/bst_inherit_impl/bstree_test/bstree_test_module.c
r6431 r6461 54 54 55 55 return data->vself; 56 } 57 58 static VALUE 59 rb_bstree_node_initialize(int argc, VALUE* argv, VALUE vself) 60 { 61 struct bstree_node* self; 62 struct rb_bstree_node_data* data; 63 VALUE value; 64 65 Data_Get_Struct(vself, struct bstree_node, self); 66 data = (struct rb_bstree_node_data*)BSTREE_NODE_DATA(self); 67 68 rb_scan_args(argc, argv, "01", &value); 69 data->value = value; 70 71 return vself; 56 72 } 57 73 … … 295 311 } 296 312 313 static VALUE 314 rb_bstree_node_rotate_left(VALUE vself) 315 { 316 struct bstree_node* self; 317 struct bstree_node* res; 318 struct rb_bstree_node_data* res_data; 319 320 Data_Get_Struct(vself, struct bstree_node, self); 321 res = bstree_node_rotate_left(self); 322 if (0 != res) { 323 res_data = (struct rb_bstree_node_data*)BSTREE_NODE_DATA(res); 324 return res_data->vself; 325 } 326 327 return Qnil; 328 } 329 330 static VALUE 331 rb_bstree_node_rotate_right(VALUE vself) 332 { 333 struct bstree_node* self; 334 struct bstree_node* res; 335 struct rb_bstree_node_data* res_data; 336 337 Data_Get_Struct(vself, struct bstree_node, self); 338 res = bstree_node_rotate_right(self); 339 if (0 != res) { 340 res_data = (struct rb_bstree_node_data*)BSTREE_NODE_DATA(res); 341 return res_data->vself; 342 } 343 344 return Qnil; 345 } 346 297 347 void 298 348 Init_bstree_test_module(void) … … 302 352 cBSTreeNode = rb_define_class("BSTreeNode", rb_cData); 303 353 rb_define_alloc_func(cBSTreeNode, rb_bstree_node_s_allocate); 354 rb_define_method(cBSTreeNode, "initialize", rb_bstree_node_initialize, -1); 304 355 rb_define_method(cBSTreeNode, "inspect", rb_bstree_node_inspect, 0); 305 356 rb_define_method(cBSTreeNode, "left", rb_bstree_node_left, 0); … … 320 371 rb_define_method(cBSTreeNode, "lookup_upper_bound", 321 372 rb_bstree_node_lookup_upper_bound, 1); 322 } 373 374 rb_define_method(cBSTreeNode, "rotate_left", 375 rb_bstree_node_rotate_left, 0); 376 rb_define_method(cBSTreeNode, "rotate_right", 377 rb_bstree_node_rotate_right, 0); 378 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)