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,26 @@
1
+ module DS
2
+
3
+ #Container for linked lists elements.
4
+ class ListElement
5
+
6
+ attr_accessor :data, :next
7
+
8
+ def initialize(x=nil)
9
+ @data = x
10
+ @next= nil
11
+ end
12
+
13
+ #Adds new element to list.
14
+ def append(x)
15
+ elem = ListElement.new(x)
16
+ self.next = elem
17
+ end
18
+
19
+ #Checks if given element is last.
20
+ def tail?
21
+ @next.nil?
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,42 @@
1
+ module DS
2
+
3
+ #Ring - represent list which head is linked with tail.
4
+ class Ring < CyclicList
5
+
6
+ #Creates ring from array.
7
+ def self.from_array(arr)
8
+ list = Ring.new(arr.shift)
9
+ tail = list.head
10
+ arr.each { |e| tail = tail.append(e) }
11
+ tail.next = list.head
12
+ list
13
+ end
14
+
15
+ def length
16
+ cycle_size
17
+ end
18
+
19
+ #Removes ring elements by k until there is only one element in list.
20
+ def eliminate_by(k)
21
+ elem = self.head
22
+ prev = elem
23
+
24
+ k = k-1
25
+
26
+ while prev != elem.next
27
+
28
+ k.times do
29
+ prev = elem
30
+ elem = elem.next
31
+ end
32
+
33
+ prev.next = elem.next
34
+ elem = prev.next
35
+ end
36
+
37
+ return prev.data
38
+
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ module DS
2
+
3
+ #Implements two dimensional array.
4
+ class Array2D
5
+
6
+
7
+ #Creates new two dimensional array. Init is the default value of the array.
8
+ def initialize(size=1,init=0)
9
+ @init = init
10
+ @store = Array.new(size){Array.new(size){init}}
11
+ end
12
+
13
+
14
+ def [](x,y)
15
+ if @store[x].nil?
16
+ @store[x] = []
17
+ @init
18
+ elsif @store[x][y].nil?
19
+ @init
20
+ else
21
+ @store[x][y]
22
+ end
23
+ end
24
+
25
+ def []=(x,y,v)
26
+ @store[x] = [] if @store[x].nil?
27
+ @store[x][y] = v
28
+ end
29
+
30
+ def to_a
31
+ @store.flatten
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module DS
2
+
3
+ #Class provides access to any element in array. If index i
4
+ #is greter than array size then elements from size to i are
5
+ #filled with extender.
6
+ class ExpandableArray < Array
7
+ def initialize(size=0,extender=0)
8
+ @extender = extender
9
+ super(size,extender)
10
+ end
11
+
12
+ #Returns element at index. If index is greater than size of array then
13
+ #elements from size to index are filled with extender.
14
+ def [](x)
15
+ if x >=size
16
+ for i in size..x
17
+ self[i]=@extender
18
+ end
19
+ end
20
+ super(x)
21
+ end
22
+
23
+ #Sets the element at index. If index is greater than size of array then
24
+ #elements from size to index are filled with extender.
25
+ def []=(x,v)
26
+ max = size
27
+ super(x,v)
28
+ if size - max >1
29
+ (max..size-2).each do |i|
30
+ self[i] = @extender
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,30 @@
1
+ module DS
2
+ class TriMatrix
3
+
4
+ #Class implements Triangular Matrix (lower).
5
+
6
+ #Creates new triangular matrix.
7
+ def initialize(init=0)
8
+ @store =ExpandableArray.new(0,init)
9
+ end
10
+
11
+ #Returns element at index x,y (or y,x).
12
+ def [](x,y)
13
+ x,y = y,x if y > x
14
+ index = (x*x+x)/2 +y
15
+ @store[index]
16
+ end
17
+
18
+ #Sets the element at index x,y (or y,x).
19
+ def []=(x,y,v)
20
+ x,y = y,x if y>x
21
+ index = (x*x+x)/2 +y
22
+ @store[index] = v
23
+ end
24
+
25
+ def to_a
26
+ @store
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ module DS
2
+
3
+ #Class implements queue data structure.
4
+
5
+ class Queue
6
+
7
+ #Create new queue. First parameter determines how the queue will be represented internally.
8
+ def initialize(store = :array)
9
+ if store == :array
10
+ @store = []
11
+ else
12
+ @store = List.new
13
+ end
14
+ end
15
+
16
+ #Create new queue.Internaly uses list to store elements.
17
+ def Queue.create
18
+ new(:list)
19
+ end
20
+
21
+ #Adds element to queue and returns queue itself.
22
+ def enqueue(x)
23
+ @store << x
24
+ self
25
+ end
26
+
27
+ alias :push :enqueue
28
+
29
+ #Removes element from queue and returns it.
30
+ def dequeue
31
+ @store.shift
32
+ end
33
+
34
+ alias :shift :dequeue
35
+
36
+ #Returns element from forehead of queue.
37
+ def peek
38
+ @store.first
39
+ end
40
+
41
+ #Returns length of queue. If queue is empty returns 0.
42
+ def length
43
+ @store.length
44
+ end
45
+
46
+ #Checks if queue is empty.
47
+ def empty?
48
+ @store.empty?
49
+ end
50
+
51
+ end
52
+ end
53
+
@@ -0,0 +1,39 @@
1
+ module DS
2
+
3
+ #Class implements Stack data structure.
4
+ #Internaly uses array to store elements.
5
+
6
+ class Stack
7
+
8
+ def initialize
9
+ @store = []
10
+ end
11
+
12
+ #Returns the stack size.
13
+ def size
14
+ @store.size
15
+ end
16
+
17
+ # Removes element from stack and returns it.
18
+ def pop
19
+ @store.pop
20
+ end
21
+
22
+ # Adds element to the top of the stack.
23
+ def push(x)
24
+ @store.push x
25
+ end
26
+
27
+ # Returns element from the top of the stack.
28
+ def peek
29
+ @store.last
30
+ end
31
+
32
+ # If stack is empty returns true otherwise returns false.
33
+ def empty?
34
+ @store.empty?
35
+ end
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,71 @@
1
+ module DS
2
+ class BinaryHeap < CompleteBinaryTree
3
+ attr_accessor :data
4
+
5
+ #Create new Heap from args.
6
+ #Given block sets the heap relation. Default heap relation is Max Heap.
7
+ def initialize(*args, &block)
8
+ if block_given?
9
+ @relation = block
10
+ else
11
+ @relation = Proc.new{|parent,child| parent >= child}
12
+ end
13
+ @data = args.to_a
14
+ heapify!
15
+ end
16
+
17
+ def set_relation(&relation)
18
+ @relation = relation
19
+ end
20
+
21
+ def relation(parent,child)
22
+ @relation.call(@data[parent],@data[child])
23
+ end
24
+
25
+ #Arranges data in heap.
26
+ #O(n)
27
+ def heapify!
28
+ (@data.size/2).downto(0) do |i|
29
+ heapify(i)
30
+ end
31
+ end
32
+
33
+ #Inserts new value to heap maintaining the heap relation.
34
+ #Returns heap itself.
35
+ #O(log)
36
+ def insert(value)
37
+ @data.push value
38
+ child = @data.size-1
39
+ parent = parent_index(child)
40
+
41
+ while parent >= 0 and !relation(parent,child)
42
+ @data[child], @data[parent] = @data[parent], @data[child]
43
+ child = parent
44
+ parent = parent_index(child)
45
+ end
46
+ self
47
+ end
48
+
49
+ #Maintain heap condition for i node.
50
+ #O(log)
51
+ def heapify(i)
52
+ left = left_index(i)
53
+ left = nil if left >= @data.size
54
+
55
+ right = right_index(i)
56
+ right = nil if right >= @data.size
57
+
58
+ largest = [i,left,right].compact.sort{|x,y| relation(x,y)?-1:1}.first
59
+
60
+ if largest != i
61
+ @data[i], @data[largest] = @data[largest], @data[i]
62
+ heapify(largest)
63
+ end
64
+ end
65
+
66
+ def to_a
67
+ @data
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,32 @@
1
+ module DS
2
+ class BinarySearchTree < BinaryTree
3
+
4
+ def self.from_array(arr)
5
+ tree = BinarySearchTree.new(arr.shift)
6
+ arr.each do |e|
7
+ tree.insert e
8
+ end
9
+ tree
10
+ end
11
+
12
+ def insert x
13
+ if x > @data and right.nil?
14
+ self.right = BinarySearchTree.new(x)
15
+ elsif x <= @data and left.nil?
16
+ self.left = BinarySearchTree.new(x)
17
+ elsif x > @data
18
+ right.insert x
19
+ else
20
+ left.insert x
21
+ end
22
+ end
23
+
24
+
25
+ def BinarySearchTree.valid?(other)
26
+ walker = TreeWalker.new(other)
27
+ walker.traverse(:inorder)
28
+ walker.visited.sorted?
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,65 @@
1
+ module DS
2
+ class BinaryTree < Tree
3
+
4
+ def left
5
+ if @children.empty?
6
+ nil
7
+ else
8
+ @children[0]
9
+ end
10
+ end
11
+
12
+ def left=(value)
13
+ @children[0] = value
14
+ end
15
+
16
+ def right
17
+ if @children.empty?
18
+ nil
19
+ else
20
+ @children[1]
21
+ end
22
+ end
23
+
24
+ def right=(value)
25
+ @children[1] = value
26
+ end
27
+
28
+ def << (value)
29
+ subtree = BinaryTree.new(value)
30
+ @children << subtree
31
+ return subtree
32
+ end
33
+
34
+ #Inserts new element in BSF order
35
+ def insert(x)
36
+ q = Queue.new
37
+ if @data == nil
38
+ @data = x
39
+ elsif self.left == nil
40
+ self.left = BinaryTree.new(x)
41
+ elsif self.right == nil
42
+ self.right = BinaryTree.new(x)
43
+ else
44
+ q.push self.left
45
+ q.push self.right
46
+ loop do
47
+ node = q.shift
48
+ if node.left == nil
49
+ node.insert(x)
50
+ break
51
+ else
52
+ q.push node.left
53
+ end
54
+ if node.right == nil
55
+ node.insert(x)
56
+ break
57
+ else
58
+ q.push node.right
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
@@ -0,0 +1,52 @@
1
+ module DS
2
+ class CompleteBinaryTree
3
+
4
+ def initialize(*args)
5
+ if args.empty?
6
+ @data = []
7
+ else
8
+ @data = args.to_a
9
+ end
10
+ end
11
+
12
+ def << (value)
13
+ @data.push value
14
+ end
15
+
16
+ def root
17
+ @data.first
18
+ end
19
+
20
+ def children(value)
21
+ i = @data.index(value)
22
+ return [@data[2*i+1], @data[2*i+2]]
23
+ end
24
+
25
+ def right value
26
+ children(value)[1]
27
+ end
28
+
29
+ def left value
30
+ children(value).first
31
+ end
32
+
33
+ def left_index(i)
34
+ 2*i+1
35
+ end
36
+
37
+ def right_index(i)
38
+ 2*i+2
39
+ end
40
+
41
+ def parent_index(i)
42
+ (i+1)/2 - 1
43
+ end
44
+
45
+ def parent value
46
+ i = @data.index(value)
47
+ @data[parent_index(i)]
48
+ end
49
+
50
+ end
51
+ end
52
+