avl_tree 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,17 +1,12 @@
1
- avl_tree - Naive implementation of AVL tree for Ruby
1
+ avl_tree - AVL tree and Red-black tree in Ruby
2
2
  Copyright (C) 2012 Hiroshi Nakamura <nahi@ruby-lang.org>
3
3
 
4
4
 
5
- == TODO
6
-
7
- It's still under development.
8
-
9
-
10
5
  == Author
11
6
 
12
7
  Name:: Hiroshi Nakamura
13
8
  E-mail:: nahi@ruby-lang.org
14
- Project web site:: http://github.com/nahi/radix_tree
9
+ Project web site:: http://github.com/nahi/avl_tree
15
10
 
16
11
 
17
12
  == License
@@ -355,17 +355,17 @@ class AVLTree
355
355
  end
356
356
 
357
357
  def []=(key, value)
358
- @root = @root.insert(key.to_s, value)
358
+ @root = @root.insert(key, value)
359
359
  end
360
360
  alias insert []=
361
361
 
362
362
  def key?(key)
363
- @root.retrieve(key.to_s) != Node::UNDEFINED
363
+ @root.retrieve(key) != Node::UNDEFINED
364
364
  end
365
365
  alias has_key? key?
366
366
 
367
367
  def [](key)
368
- value = @root.retrieve(key.to_s)
368
+ value = @root.retrieve(key)
369
369
  if value == Node::UNDEFINED
370
370
  default_value
371
371
  else
@@ -374,7 +374,7 @@ class AVLTree
374
374
  end
375
375
 
376
376
  def delete(key)
377
- deleted, @root = @root.delete(key.to_s)
377
+ deleted, @root = @root.delete(key)
378
378
  deleted.value
379
379
  end
380
380
 
@@ -307,12 +307,15 @@ class RedBlackTree
307
307
  private
308
308
 
309
309
  # trying to rebalance when the left sub-tree is 1 level higher than the right
310
+ # precondition: self is black and @left is red
310
311
  def rebalance_for_left_insert
311
312
  ret = self
312
313
  if @right.red?
314
+ # pull-up red nodes and let the parent rebalance (see precondition)
313
315
  @color = :RED
314
316
  @left.color = @right.color = :BLACK
315
317
  else
318
+ # move 1 black from the left to the right by single/double rotation
316
319
  if @left.right.red?
317
320
  @left = @left.rotate_left
318
321
  end
@@ -322,6 +325,7 @@ class RedBlackTree
322
325
  end
323
326
 
324
327
  # trying to rebalance when the right sub-tree is 1 level higher than the left
328
+ # See rebalance_for_left_insert.
325
329
  def rebalance_for_right_insert
326
330
  ret = self
327
331
  if @left.red?
@@ -492,19 +496,19 @@ class RedBlackTree
492
496
  end
493
497
 
494
498
  def []=(key, value)
495
- @root = @root.insert(key.to_s, value)
499
+ @root = @root.insert(key, value)
496
500
  @root.set_root
497
501
  @root.check_height if $DEBUG
498
502
  end
499
503
  alias insert []=
500
504
 
501
505
  def key?(key)
502
- @root.retrieve(key.to_s) != Node::UNDEFINED
506
+ @root.retrieve(key) != Node::UNDEFINED
503
507
  end
504
508
  alias has_key? key?
505
509
 
506
510
  def [](key)
507
- value = @root.retrieve(key.to_s)
511
+ value = @root.retrieve(key)
508
512
  if value == Node::UNDEFINED
509
513
  default_value
510
514
  else
@@ -513,7 +517,7 @@ class RedBlackTree
513
517
  end
514
518
 
515
519
  def delete(key)
516
- deleted, @root, rebalance = @root.delete(key.to_s)
520
+ deleted, @root, rebalance = @root.delete(key)
517
521
  unless empty?
518
522
  @root.set_root
519
523
  @root.check_height if $DEBUG
@@ -372,9 +372,9 @@ class TestAVLTree < Test::Unit::TestCase
372
372
 
373
373
  def test_to_s
374
374
  h = AVLTree.new
375
- h[:abc] = 1
376
- assert_equal 1, h["abc"]
377
- assert_equal 1, h[:abc]
375
+ h[5] = 1
376
+ assert_equal 1, h[5]
377
+ assert_nil h["5"]
378
378
  end
379
379
 
380
380
  def test_key?
@@ -424,6 +424,14 @@ class TestAVLTree < Test::Unit::TestCase
424
424
  assert h.to_hash.empty?
425
425
  end
426
426
 
427
+ def test_non_string_keys
428
+ h = AVLTree.new
429
+ h[1.3] = 'a'
430
+ h[4.3] = 'b'
431
+
432
+ assert_equal [1.3, 'a' ], h.first
433
+ end
434
+
427
435
  if RUBY_VERSION >= '1.9.0'
428
436
  # In contrast to RadixTree, AVLTree just uses String#<=> as-is
429
437
  def test_encoding
@@ -522,9 +522,9 @@ class TestRedBlackTree < Test::Unit::TestCase
522
522
 
523
523
  def test_to_s
524
524
  h = RedBlackTree.new
525
- h[:abc] = 1
526
- assert_equal 1, h["abc"]
527
- assert_equal 1, h[:abc]
525
+ h[5] = 1
526
+ assert_equal 1, h[5]
527
+ assert_nil h["5"]
528
528
  end
529
529
 
530
530
  def test_key?
@@ -574,6 +574,14 @@ class TestRedBlackTree < Test::Unit::TestCase
574
574
  assert h.to_hash.empty?
575
575
  end
576
576
 
577
+ def test_non_string_keys
578
+ h = RedBlackTree.new
579
+ h[1.3] = 'a'
580
+ h[4.3] = 'b'
581
+
582
+ assert_equal [1.3, 'a' ], h.first
583
+ end
584
+
577
585
  if RUBY_VERSION >= '1.9.0'
578
586
  # In contrast to RadixTree, RedBlackTree just uses String#<=> as-is
579
587
  def test_encoding
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avl_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-05 00:00:00.000000000 Z
12
+ date: 2012-03-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: nahi@ruby-lang.org