ds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/Rakefile +13 -0
  4. data/doc/Array.html +264 -0
  5. data/doc/DS.html +292 -0
  6. data/doc/DS/Array2D.html +345 -0
  7. data/doc/DS/BinaryHeap.html +493 -0
  8. data/doc/DS/BinarySearchTree.html +313 -0
  9. data/doc/DS/BinaryTree.html +433 -0
  10. data/doc/DS/CompleteBinaryTree.html +550 -0
  11. data/doc/DS/CyclicList.html +234 -0
  12. data/doc/DS/Digraph.html +299 -0
  13. data/doc/DS/Edge.html +283 -0
  14. data/doc/DS/ExpandableArray.html +316 -0
  15. data/doc/DS/Graph.html +739 -0
  16. data/doc/DS/GraphAsList.html +361 -0
  17. data/doc/DS/GraphAsMatrix.html +633 -0
  18. data/doc/DS/GraphAsTriMatrix.html +274 -0
  19. data/doc/DS/List.html +1263 -0
  20. data/doc/DS/ListElement.html +344 -0
  21. data/doc/DS/Queue.html +517 -0
  22. data/doc/DS/Ring.html +323 -0
  23. data/doc/DS/Stack.html +407 -0
  24. data/doc/DS/Tree.html +770 -0
  25. data/doc/DS/TreeWalker.html +561 -0
  26. data/doc/DS/TriMatrix.html +338 -0
  27. data/doc/created.rid +25 -0
  28. data/doc/ds/graphs/digraph_rb.html +52 -0
  29. data/doc/ds/graphs/edge_rb.html +52 -0
  30. data/doc/ds/graphs/graph_as_list_rb.html +52 -0
  31. data/doc/ds/graphs/graph_as_matrix_rb.html +52 -0
  32. data/doc/ds/graphs/graph_as_tri_matrix_rb.html +52 -0
  33. data/doc/ds/graphs/graph_rb.html +52 -0
  34. data/doc/ds/lists/cyclic_list_rb.html +52 -0
  35. data/doc/ds/lists/list_element_rb.html +52 -0
  36. data/doc/ds/lists/list_rb.html +52 -0
  37. data/doc/ds/lists/ring_rb.html +52 -0
  38. data/doc/ds/matrixes/array_2d_rb.html +52 -0
  39. data/doc/ds/matrixes/expandable_array_rb.html +52 -0
  40. data/doc/ds/matrixes/tri_matrix_rb.html +52 -0
  41. data/doc/ds/queues/queue_rb.html +52 -0
  42. data/doc/ds/stacks/stack_rb.html +52 -0
  43. data/doc/ds/trees/binary_heap_rb.html +52 -0
  44. data/doc/ds/trees/binary_search_tree_rb.html +52 -0
  45. data/doc/ds/trees/binary_tree_rb.html +52 -0
  46. data/doc/ds/trees/complete_binary_tree_rb.html +52 -0
  47. data/doc/ds/trees/tree_rb.html +52 -0
  48. data/doc/ds/trees/tree_walker_rb.html +52 -0
  49. data/doc/ds/version_rb.html +52 -0
  50. data/doc/ds_rb.html +98 -0
  51. data/doc/ext/ext_rb.html +52 -0
  52. data/doc/images/brick.png +0 -0
  53. data/doc/images/brick_link.png +0 -0
  54. data/doc/images/bug.png +0 -0
  55. data/doc/images/bullet_black.png +0 -0
  56. data/doc/images/bullet_toggle_minus.png +0 -0
  57. data/doc/images/bullet_toggle_plus.png +0 -0
  58. data/doc/images/date.png +0 -0
  59. data/doc/images/find.png +0 -0
  60. data/doc/images/loadingAnimation.gif +0 -0
  61. data/doc/images/macFFBgHack.png +0 -0
  62. data/doc/images/package.png +0 -0
  63. data/doc/images/page_green.png +0 -0
  64. data/doc/images/page_white_text.png +0 -0
  65. data/doc/images/page_white_width.png +0 -0
  66. data/doc/images/plugin.png +0 -0
  67. data/doc/images/ruby.png +0 -0
  68. data/doc/images/tag_green.png +0 -0
  69. data/doc/images/wrench.png +0 -0
  70. data/doc/images/wrench_orange.png +0 -0
  71. data/doc/images/zoom.png +0 -0
  72. data/doc/index.html +375 -0
  73. data/doc/js/darkfish.js +116 -0
  74. data/doc/js/jquery.js +32 -0
  75. data/doc/js/quicksearch.js +114 -0
  76. data/doc/js/thickbox-compressed.js +10 -0
  77. data/doc/rdoc.css +763 -0
  78. data/ds.gemspec +20 -0
  79. data/lib/ds.rb +38 -0
  80. data/lib/ds/graphs/digraph.rb +20 -0
  81. data/lib/ds/graphs/edge.rb +15 -0
  82. data/lib/ds/graphs/graph.rb +107 -0
  83. data/lib/ds/graphs/graph_as_list.rb +48 -0
  84. data/lib/ds/graphs/graph_as_matrix.rb +114 -0
  85. data/lib/ds/graphs/graph_as_tri_matrix.rb +25 -0
  86. data/lib/ds/lists/cyclic_list.rb +21 -0
  87. data/lib/ds/lists/list.rb +303 -0
  88. data/lib/ds/lists/list_element.rb +26 -0
  89. data/lib/ds/lists/ring.rb +42 -0
  90. data/lib/ds/matrixes/array_2d.rb +35 -0
  91. data/lib/ds/matrixes/expandable_array.rb +37 -0
  92. data/lib/ds/matrixes/tri_matrix.rb +30 -0
  93. data/lib/ds/queues/queue.rb +53 -0
  94. data/lib/ds/stacks/stack.rb +39 -0
  95. data/lib/ds/trees/binary_heap.rb +71 -0
  96. data/lib/ds/trees/binary_search_tree.rb +32 -0
  97. data/lib/ds/trees/binary_tree.rb +65 -0
  98. data/lib/ds/trees/complete_binary_tree.rb +52 -0
  99. data/lib/ds/trees/tree.rb +117 -0
  100. data/lib/ds/trees/tree_walker.rb +179 -0
  101. data/lib/ds/version.rb +3 -0
  102. data/lib/ext/ext.rb +15 -0
  103. data/test/help.rb +8 -0
  104. data/test/test_array2d.rb +51 -0
  105. data/test/test_binary_heap.rb +35 -0
  106. data/test/test_binary_search_tree.rb +32 -0
  107. data/test/test_binary_tree.rb +51 -0
  108. data/test/test_complete_binary_tree.rb +30 -0
  109. data/test/test_digraph.rb +134 -0
  110. data/test/test_expandable_array.rb +26 -0
  111. data/test/test_graph.rb +71 -0
  112. data/test/test_list.rb +138 -0
  113. data/test/test_list_element.rb +56 -0
  114. data/test/test_queue.rb +110 -0
  115. data/test/test_ring.rb +28 -0
  116. data/test/test_stack.rb +87 -0
  117. data/test/test_tree.rb +48 -0
  118. data/test/test_tree_walker.rb +69 -0
  119. data/test/test_tri_matrix.rb +22 -0
  120. metadata +184 -0
@@ -0,0 +1,51 @@
1
+ require 'help'
2
+
3
+ describe BinaryTree do
4
+
5
+ before do
6
+
7
+ @bin_tree = BinaryTree.new
8
+ [2,5,8,9,11,12,14].each{|x| @bin_tree.insert(x)}
9
+
10
+ @izo_tree = BinaryTree.new
11
+ [2,7,8,3,11,12,14].each{|x| @izo_tree.insert(x)}
12
+
13
+ @bin_walker = TreeWalker.new(@bin_tree)
14
+
15
+ end
16
+
17
+ it "#to_a should return tree converted to array." do
18
+ @bin_tree.to_a.must_equal [2,5,8,9,11,12,14]
19
+ end
20
+
21
+ it "#height should return tree height." do
22
+ @bin_tree.height.must_equal 3
23
+ end
24
+
25
+ it "#width should return tree width." do
26
+ @bin_tree.width.must_equal 4
27
+ end
28
+
29
+ it "#leaf_count should return number of tree leaves." do
30
+ @bin_tree.leaf_count.must_equal 4
31
+ end
32
+
33
+ it "#get_leaves should return list of tree leaves." do
34
+ @bin_tree.get_leaves.to_a.collect{ |e| e.data }.must_equal [9,11,12,14]
35
+ end
36
+
37
+ it "#lowest_height should return node which lies closest to the root." do
38
+ assert @bin_tree.lowest_height.must_equal 9
39
+ end
40
+
41
+ it "#mirror should mirror tree." do
42
+ @bin_tree.mirror!
43
+ @bin_tree.to_a.must_equal [2,8,5,14,12,11,9]
44
+ end
45
+
46
+ it "#isometric should check if tree is isometric to antother." do
47
+ assert @bin_tree.izometric?(@izo_tree)
48
+ end
49
+
50
+ end
51
+
@@ -0,0 +1,30 @@
1
+ require 'help'
2
+
3
+ describe CompleteBinaryTree do
4
+
5
+ before do
6
+ @tree = CompleteBinaryTree.new(1,2,3,4,5,6,7)
7
+ end
8
+
9
+ it "#root should return root of the tree." do
10
+ @tree.root.must_equal 1
11
+ end
12
+
13
+ it "#left should return left child" do
14
+ @tree.left(1).must_equal 2
15
+ @tree.left(2).must_equal 4
16
+ end
17
+
18
+ it "#right should return right child" do
19
+ @tree.right(1).must_equal 3
20
+ @tree.right(2).must_equal 5
21
+ end
22
+
23
+ it "#parent should return parent." do
24
+ @tree.parent(4).must_equal 2
25
+ @tree.parent(5).must_equal 2
26
+ @tree.parent(2).must_equal 1
27
+ end
28
+
29
+ end
30
+
@@ -0,0 +1,134 @@
1
+ require 'help'
2
+
3
+ describe Digraph do
4
+
5
+ describe "without weighted edges" do
6
+
7
+ before do
8
+
9
+ edges = []
10
+ edges << Edge.new('Lukas','Marc')
11
+ edges << Edge.new('Lukas','Tom')
12
+ edges << Edge.new('Marc','Jack')
13
+ edges << Edge.new('Jack','Marc')
14
+ edges << Edge.new('Tom','Marc')
15
+
16
+ @digraph = Digraph.create(edges)
17
+
18
+ end
19
+
20
+ it "#degree should return vertex degree." do
21
+ @digraph.degree("Marc").must_equal 4
22
+ @digraph.degree("Tom").must_equal 2
23
+ @digraph.degree("Lukas").must_equal 2
24
+ end
25
+
26
+ it "#in_degree should return number of incoming edges to the vertex." do
27
+ @digraph.in_degree("Marc").must_equal 3
28
+ @digraph.in_degree("Tom").must_equal 1
29
+ @digraph.in_degree("Lukas").must_equal 0
30
+ end
31
+
32
+ it "#out_degree should return number of outgoing edges from vertex." do
33
+ @digraph.out_degree("Marc").must_equal 1
34
+ @digraph.out_degree("Tom").must_equal 1
35
+ @digraph.out_degree("Lukas").must_equal 2
36
+ end
37
+
38
+ it "#get_edge should return graph edge." do
39
+ lukas_tom = @digraph.get_edge("Lukas","Tom")
40
+ lukas_tom.from.must_equal "Lukas"
41
+ lukas_tom.to.must_equal "Tom"
42
+ lukas_tom.weight.must_equal 1
43
+ lukas_tom.must_be_instance_of Edge
44
+ end
45
+
46
+ it "#edge? should check if two vertex are connected." do
47
+ refute @digraph.edge?("Marc","Tom")
48
+ refute @digraph.edge?("Jack","Tom")
49
+ refute @digraph.edge?("Tom","Lukas")
50
+
51
+ assert @digraph.edge?("Tom","Marc")
52
+ assert @digraph.edge?("Lukas","Marc")
53
+ end
54
+
55
+ it "#neighbors should return array containing all neighbors for given vertex." do
56
+ n = @digraph.neighbors('Tom')
57
+ n.must_be_instance_of Array
58
+ n.size.must_equal 1
59
+ assert n.include?("Marc")
60
+ refute n.include?("Lukas")
61
+ refute n.include?("Jack")
62
+ end
63
+
64
+ it "#add_edge should add new edge." do
65
+ @digraph.add_edges([Edge.new("Marc","Kate")])
66
+ @digraph.vertex_size.must_equal 6
67
+ assert @digraph.edge?("Marc","Kate")
68
+ refute @digraph.edge?("Kate","Marc")
69
+ refute @digraph.edge?("Tom","Kate")
70
+ end
71
+
72
+ it "#remove_edge should remove edge." do
73
+ @digraph.remove("Marc","Jack")
74
+ @digraph.vertex_size.must_equal 4
75
+ refute @digraph.edge?("Marc","Jack")
76
+ assert @digraph.edge?("Jack","Marc")
77
+ end
78
+
79
+ end
80
+
81
+ describe "with weighted edges" do
82
+
83
+ before do
84
+ edges = []
85
+
86
+ edges << Edge.new(:A,:C,5)
87
+ edges << Edge.new(:A,:D,3)
88
+ edges << Edge.new(:A,:G,14)
89
+ edges << Edge.new(:C,:E,3)
90
+ edges << Edge.new(:C,:F,2)
91
+ edges << Edge.new(:D,:C,11)
92
+ edges << Edge.new(:D,:E,7)
93
+ edges << Edge.new(:D,:G,6)
94
+ edges << Edge.new(:G,:E,7)
95
+ edges << Edge.new(:E,:B,5)
96
+ edges << Edge.new(:G,:B,6)
97
+ edges << Edge.new(:F,:B,7)
98
+
99
+ @wdigraph = Digraph.create(edges)
100
+ end
101
+
102
+ it "edge should be assigned a weight." do
103
+ d_c = @wdigraph.get_edge(:D,:C)
104
+ d_c.weight.must_equal 11
105
+ d_c.from.must_equal :D
106
+ d_c.to.must_equal :C
107
+ d_c.must_be_instance_of Edge
108
+ end
109
+
110
+ it "#degree should return vertex degree." do
111
+ @wdigraph.degree(:E).must_equal 4
112
+ @wdigraph.degree(:C).must_equal 4
113
+ @wdigraph.degree(:A).must_equal 3
114
+ end
115
+
116
+ it "#in_degree should return number of incoming edges to the vertex." do
117
+ @wdigraph.in_degree(:E).must_equal 3
118
+ @wdigraph.in_degree(:C).must_equal 2
119
+ @wdigraph.in_degree(:A).must_equal 0
120
+ end
121
+
122
+ it "#out_degree should return number of outgoing edges from vertex." do
123
+ @wdigraph.out_degree(:E).must_equal 1
124
+ @wdigraph.out_degree(:B).must_equal 0
125
+ @wdigraph.out_degree(:A).must_equal 3
126
+ end
127
+
128
+ it "#bfs should iterate over graph in bfs order." do
129
+ @wdigraph.bfs(:A).must_equal [:A, :C, :D, :G, :E, :F, :B]
130
+ end
131
+ end
132
+ end
133
+
134
+
@@ -0,0 +1,26 @@
1
+ require 'help'
2
+
3
+ describe ExpandableArray do
4
+
5
+ describe "Zero Array" do
6
+
7
+ before do
8
+ @zero_arr = ExpandableArray.new(0,0)
9
+ @zero_arr[0] = 1
10
+ end
11
+
12
+ it "access to element x should set all elements from array size to x to zero." do
13
+ @zero_arr.size.must_equal 1
14
+ @zero_arr[3].must_equal 0
15
+ @zero_arr.size.must_equal 4
16
+ @zero_arr.must_equal [1,0,0,0]
17
+ end
18
+
19
+ it "setting value on index x should set all elements from array size to x to zero." do
20
+ @zero_arr[5] = 2
21
+ @zero_arr.must_equal [1,0,0,0,0,2]
22
+ end
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,71 @@
1
+ require 'help'
2
+
3
+ describe Graph do
4
+
5
+ before do
6
+ edges = []
7
+ edges << Edge.new('Lukas','Marc')
8
+ edges << Edge.new('Lukas','Tom')
9
+ edges << Edge.new('Marc','Jack')
10
+ edges << Edge.new('Tom','Marc')
11
+ @graph = Graph.create(edges)
12
+ end
13
+
14
+ it "#vertex_size should return number of vertexes" do
15
+ @graph.vertex_size.must_equal 4
16
+ end
17
+
18
+ it "#degree should return degree for given vertex." do
19
+ @graph.degree("Marc").must_equal 3
20
+ @graph.degree("Jack").must_equal 1
21
+ @graph.degree("Lukas").must_equal 2
22
+ @graph.degree("Tom").must_equal 2
23
+ end
24
+
25
+ it "#edge? should check if given vertexes are connected." do
26
+ assert @graph.edge?("Marc","Tom")
27
+ assert @graph.edge?("Tom","Marc")
28
+ refute @graph.edge?("Tom","Jack")
29
+ refute @graph.edge?("Jack","Tom")
30
+ end
31
+
32
+ it "#add_edges should add new edges." do
33
+ @graph.add_edges([Edge.new("Marc","Kate")])
34
+ @graph.vertex_size.must_equal 5
35
+ assert @graph.edge?("Marc","Kate")
36
+ assert @graph.edge?("Kate","Marc")
37
+ refute @graph.edge?("Tom","Kate")
38
+ end
39
+
40
+ it "#remove_edge should remove edge." do
41
+ @graph.remove("Marc","Jack")
42
+ @graph.vertex_size.must_equal 3
43
+ refute @graph.edge?("Marc","Jack")
44
+ refute @graph.edge?("Jack","Marc")
45
+ end
46
+
47
+ it "#neighbors should return all neighbors for given vertex." do
48
+ n = @graph.neighbors('Tom')
49
+ n.must_be_instance_of Array
50
+ n.size.must_equal 2
51
+ assert n.include?("Marc")
52
+ assert n.include?("Lukas")
53
+ refute n.include?("Jack")
54
+ end
55
+
56
+ it "should iterate edges." do
57
+ r = []
58
+ @graph.each_edge{|e| r.push e}
59
+ r.size.must_equal 4
60
+ end
61
+
62
+ it "should iterate vertexes." do
63
+ r = []
64
+ @graph.each_vertex { |v| r.push v }
65
+ r.size.must_equal 4
66
+ r.sort.must_equal ['Jack','Lukas', 'Marc', 'Tom' ]
67
+ end
68
+
69
+ end
70
+
71
+
@@ -0,0 +1,138 @@
1
+ require 'help'
2
+
3
+ describe List do
4
+
5
+ before do
6
+ @list = List.from_array([1,2,3,4])
7
+ @list2 = List.from_array([4,5,7])
8
+ end
9
+
10
+ it "#from_array should transform array to list." do
11
+ @list.must_be_kind_of List
12
+ @list.length.must_equal 4
13
+ end
14
+
15
+ it "#length should return list size." do
16
+ @list.length.must_equal 4
17
+ @list2.length.must_equal 3
18
+ end
19
+
20
+
21
+ it "#append should add element to the end of the list." do
22
+ x = 5
23
+ @list.append(x)
24
+ @list.length.must_equal 5
25
+ @list.last.must_be_same_as x
26
+ end
27
+
28
+ it "#prepend should add element at the beginning of the list." do
29
+ x = 0
30
+ @list.prepend(x)
31
+ @list.length.must_equal 5
32
+ @list.first.must_be_same_as x
33
+ end
34
+
35
+ it "#remove should remove element from list." do
36
+ second = @list.head.next
37
+ @list.remove(second)
38
+ @list.to_a.must_equal [1,3,4]
39
+ end
40
+
41
+ it "#insert_before should insert new element before another." do
42
+ second = @list.head.next
43
+ @list.insert_before(9,second)
44
+ @list.to_a.must_equal [1,9,2,3,4]
45
+
46
+ @list.insert_before(8,@list.head)
47
+ @list.to_a.must_equal [8,1,9,2,3,4]
48
+
49
+ @list.insert_before(7,@list.tail)
50
+ @list.to_a.must_equal [8,1,9,2,3,7,4]
51
+
52
+ end
53
+
54
+ it "#insert_after should insert new element after another." do
55
+ second = @list.head.next
56
+ @list.insert_after(9,second)
57
+ @list.to_a.must_equal [1,2,9,3,4]
58
+ end
59
+
60
+
61
+ it "#head should point to first element of the list." do
62
+ @list.head.must_be_kind_of ListElement
63
+ @list2.head.must_be_kind_of ListElement
64
+ @list.head.data.must_equal 1
65
+ @list2.head.data.must_equal 4
66
+ end
67
+
68
+ it "#tail sould point to last element of the list" do
69
+ @list.tail.must_be_kind_of ListElement
70
+ @list2.tail.must_be_kind_of ListElement
71
+ @list.tail.next.must_be_nil
72
+ @list2.tail.next.must_be_nil
73
+
74
+ @list.tail.data.must_equal 4
75
+ @list2.tail.data.must_equal 7
76
+ end
77
+
78
+ it "#first should return value of the first list element." do
79
+ @list.first.must_equal 1
80
+ @list2.first.must_equal 4
81
+ end
82
+
83
+
84
+ it "#last should return value of the last list element." do
85
+ @list.last.must_equal 4
86
+ @list2.last.must_equal 7
87
+ end
88
+
89
+ it "#zip? should check if two list are joined." do
90
+ refute @list.zip?(@list2)
91
+ end
92
+
93
+ it "#reverse! should reverse list" do
94
+ @list.reverse!.to_a.must_equal [4,3,2,1]
95
+ @list.reverse!.to_a.must_equal [1,2,3,4]
96
+ @list.reverse!.length.must_equal 4
97
+ end
98
+
99
+ it "#merge should merge two list into one." do
100
+ @list2.merge(@list).to_a.must_equal [1,2,3,4,4,5,7]
101
+ end
102
+
103
+ it "#orderize should order elements by evaluating block." do
104
+ not_sorted_list = List.from_array([3,-1,0,-8,2,0,1])
105
+ not_sorted_list.orderize{|elem| elem <=> 0}.to_a.must_equal [-1,-8,0,0,3,2,1]
106
+ end
107
+
108
+ it "#remove! should remove all elements that occur on the other list." do
109
+ @list.remove!(@list2).to_a.must_equal [1,2,3]
110
+ end
111
+
112
+ it "#looped? should check if list has cycle." do
113
+ refute @list2.looped?
114
+ refute @list.looped?
115
+ end
116
+
117
+ it "should include Enumerable methods." do
118
+ @list.map{ |e| e }.must_equal [1,2,3,4]
119
+ @list.inject(0){ |mem, var| mem = mem + var }.must_equal 10
120
+ end
121
+
122
+
123
+ describe "Zipped list" do
124
+
125
+ before do
126
+ last = @list.tail
127
+ @zipped = List.from_array([1,2])
128
+ @zipped.tail = last
129
+ end
130
+
131
+ it "#zip? should return true" do
132
+ assert @list.zip?(@zipped)
133
+ end
134
+
135
+ end
136
+
137
+ end
138
+