collection_utils 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -0
  3. data/README.md +3 -3
  4. data/documentation/CollectionUtils/Heap.html +1276 -0
  5. data/documentation/CollectionUtils/HeapUtils/MaxHeap.html +490 -0
  6. data/documentation/CollectionUtils/HeapUtils/MinHeap.html +490 -0
  7. data/documentation/CollectionUtils/HeapUtils.html +117 -0
  8. data/documentation/CollectionUtils/Queue.html +749 -0
  9. data/documentation/CollectionUtils/Stack.html +674 -0
  10. data/documentation/CollectionUtils/TreeUtils/BST.html +515 -0
  11. data/documentation/CollectionUtils/TreeUtils.html +117 -0
  12. data/documentation/CollectionUtils.html +134 -0
  13. data/documentation/_index.html +211 -0
  14. data/documentation/class_list.html +51 -0
  15. data/documentation/css/common.css +1 -0
  16. data/documentation/css/full_list.css +58 -0
  17. data/documentation/css/style.css +492 -0
  18. data/documentation/file.README.html +124 -0
  19. data/documentation/file_list.html +56 -0
  20. data/documentation/frames.html +17 -0
  21. data/documentation/index.html +124 -0
  22. data/documentation/js/app.js +248 -0
  23. data/documentation/js/full_list.js +216 -0
  24. data/documentation/js/jquery.js +4 -0
  25. data/documentation/method_list.html +355 -0
  26. data/documentation/top-level-namespace.html +110 -0
  27. data/lib/collection_utils/heap.rb +251 -0
  28. data/lib/collection_utils/heap_utils/max_heap.rb +12 -12
  29. data/lib/collection_utils/heap_utils/min_heap.rb +13 -11
  30. data/lib/collection_utils/queue.rb +21 -0
  31. data/lib/collection_utils/stack.rb +25 -2
  32. data/lib/collection_utils/tree_utils/bst.rb +69 -0
  33. data/lib/collection_utils/tree_utils.rb +5 -0
  34. data/lib/collection_utils/version.rb +1 -1
  35. metadata +28 -3
  36. data/lib/collection_utils/heap_utils/heap.rb +0 -97
@@ -15,28 +15,49 @@ module CollectionUtils
15
15
 
16
16
  #Public Methods
17
17
 
18
+ # Enqueue will add an element in queue.
19
+ # The added element can be seen through rear function call
20
+ #
21
+ # @param element to be added in queue
22
+ # @example Add element in queue
23
+ # queue = CollectionUtils::Queue.new()
24
+ # queue.enqueue(1)
25
+ # queue.enqueue([1,3,4,])
18
26
  def enqueue(element)
19
27
  @queue << element
20
28
  end
21
29
 
30
+ # Dequeue will remove element from queue and return the value
31
+ #
32
+ # @return element that has been removed
33
+ # @example delete element from queue
34
+ # queue = CollectionUtils::Queue.new()
35
+ # queue.enqueue(1)
36
+ # queue.enqueue([1,3,4,])
37
+ # element = queue.dequeue #element = 1
22
38
  def dequeue
23
39
  element = @queue.first
24
40
  @queue = @queue.slice(1..-1)
25
41
  return element
26
42
  end
27
43
 
44
+
45
+ # @return element that has been recently added
28
46
  def front
29
47
  return @queue.first
30
48
  end
31
49
 
50
+ # @return element that will be dequeued next
32
51
  def rear
33
52
  return @queue.last
34
53
  end
35
54
 
55
+ # @return [Boolean] which tells queue's emptiness
36
56
  def is_empty?
37
57
  return @queue.size == 0
38
58
  end
39
59
 
60
+ # @return [Integer] size of queue
40
61
  def size
41
62
  return @queue.size
42
63
  end
@@ -12,23 +12,46 @@ module CollectionUtils
12
12
  end
13
13
  end
14
14
 
15
- #Public methods
16
- def pop()
15
+ # Pop will remove the top element from the stack and return the
16
+ # removed element.
17
+ # @return Top element of the stack
18
+ #
19
+ # @example Create a stack [1,2,3,4,5] and remove 5 using pop
20
+ # stack = CollectionUtils::Stack.new([1,2,3,4,5])
21
+ # top_element = stack.pop()
22
+ def pop
17
23
  return @stack.pop
18
24
  end
19
25
 
26
+
27
+ # Add the element to the stack using push
28
+ #
29
+ # @param element that needs to added to stack. This element will the top element
30
+ # @example Create a stack [1,2,3,4,5] and add 6 to it.
31
+ # stack = CollectionUtils::Stack.new([1,2,3,4,5])
32
+ # stack.push(6)
33
+ # #stack will be [1,2,3,4,5,6]
20
34
  def push(element)
21
35
  @stack << element
22
36
  end
23
37
 
38
+ # @return [Boolean] stack's emptiness
24
39
  def is_empty?
25
40
  return @stack.size == 0
26
41
  end
27
42
 
43
+ # View the top element of the stack without removing it
44
+ #
45
+ # @return Top element of the stack
46
+ #
47
+ # @example Create a stack [1,2,3,4,5] and get 5 using peek
48
+ # stack = CollectionUtils::Stack.new([1,2,3,4,5])
49
+ # top_element = stack.peek # top_element = 5
28
50
  def peek
29
51
  return @stack.last
30
52
  end
31
53
 
54
+ # @return [Integer] size of stack
32
55
  def size
33
56
  return @stack.size
34
57
  end
@@ -0,0 +1,69 @@
1
+ require_relative '../heap'
2
+ module CollectionUtils
3
+ module TreeUtils
4
+ class BST < CollectionUtils::Tree
5
+ private
6
+ attr_accessor :bst, :height
7
+
8
+ public
9
+ def initialize(arr = [])
10
+ @heap = []
11
+ @height = 0
12
+ arr.each do |element|
13
+ insert(element, root)
14
+ end
15
+ end
16
+
17
+ def insert(element, node)
18
+ if !is_empty? && node == root
19
+ @height = 1
20
+ end
21
+ if is_empty?
22
+ @height = 1
23
+ @heap << {element: element, index: 0}
24
+ return
25
+ end
26
+ @height += 1
27
+ if element > node[:element]
28
+ right = right(node[:index])
29
+ if right.nil?
30
+ parent = node[:index]
31
+ right_in = right_index(parent)
32
+ value = {element: element, index: right_in}
33
+ assign_right(parent, value)
34
+ return
35
+ else
36
+ insert(element, right)
37
+ return
38
+ end
39
+ else
40
+ left = left(node[:index])
41
+ if left.nil?
42
+ parent = node[:index]
43
+ left_in = left_index(parent)
44
+ value = {element: element, index: left_in}
45
+ assign_left(parent, value)
46
+ return
47
+ else
48
+ insert(element, left)
49
+ return
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ def get_min
56
+ return root[:element]
57
+ end
58
+
59
+ def is_empty?
60
+ return @heap.empty?
61
+ end
62
+
63
+ def get_height
64
+ @height
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ Dir[File.dirname(__FILE__) + '/tree_utils/*.rb'].each {|file| require file }
2
+ module CollectionUtils
3
+ module TreeUtils
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module CollectionUtils
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collection_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - deeshugupta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-18 00:00:00.000000000 Z
11
+ date: 2017-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,13 +71,38 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - collection_utils.gemspec
74
+ - documentation/CollectionUtils.html
75
+ - documentation/CollectionUtils/Heap.html
76
+ - documentation/CollectionUtils/HeapUtils.html
77
+ - documentation/CollectionUtils/HeapUtils/MaxHeap.html
78
+ - documentation/CollectionUtils/HeapUtils/MinHeap.html
79
+ - documentation/CollectionUtils/Queue.html
80
+ - documentation/CollectionUtils/Stack.html
81
+ - documentation/CollectionUtils/TreeUtils.html
82
+ - documentation/CollectionUtils/TreeUtils/BST.html
83
+ - documentation/_index.html
84
+ - documentation/class_list.html
85
+ - documentation/css/common.css
86
+ - documentation/css/full_list.css
87
+ - documentation/css/style.css
88
+ - documentation/file.README.html
89
+ - documentation/file_list.html
90
+ - documentation/frames.html
91
+ - documentation/index.html
92
+ - documentation/js/app.js
93
+ - documentation/js/full_list.js
94
+ - documentation/js/jquery.js
95
+ - documentation/method_list.html
96
+ - documentation/top-level-namespace.html
74
97
  - lib/collection_utils.rb
98
+ - lib/collection_utils/heap.rb
75
99
  - lib/collection_utils/heap_utils.rb
76
- - lib/collection_utils/heap_utils/heap.rb
77
100
  - lib/collection_utils/heap_utils/max_heap.rb
78
101
  - lib/collection_utils/heap_utils/min_heap.rb
79
102
  - lib/collection_utils/queue.rb
80
103
  - lib/collection_utils/stack.rb
104
+ - lib/collection_utils/tree_utils.rb
105
+ - lib/collection_utils/tree_utils/bst.rb
81
106
  - lib/collection_utils/version.rb
82
107
  homepage: https://github.com/deeshugupta/collection_utils
83
108
  licenses:
@@ -1,97 +0,0 @@
1
- module CollectionUtils
2
- module HeapUtils
3
- class Heap
4
- private
5
- attr_accessor :heap
6
-
7
- public
8
- #Constructors
9
- def initialize(array = [])
10
- @heap = array
11
- end
12
-
13
- #Public methods
14
- def root
15
- return @heap.first
16
- end
17
-
18
- def push(element)
19
- @heap << element
20
- end
21
-
22
- def pop
23
- element = @heap.pop
24
- return element
25
- end
26
-
27
- def left_child(parent = 0)
28
- left = (2*parent + 1)
29
- return nil, nil if @heap[left].nil?
30
- return @heap[left], left
31
- end
32
-
33
- def right_child(parent = 0)
34
- right = (2*parent + 2)
35
- return nil, nil if @heap[right].nil?
36
- return @heap[right], right
37
- end
38
-
39
- def assign_left(parent =0, node)
40
- left = (2*parent + 1)
41
- @heap[left] = node
42
- end
43
-
44
- def assign_right(parent =0, node)
45
- right = (2*parent + 2)
46
- @heap[right] = node
47
- end
48
-
49
- def parent(child = 0)
50
- par = child/2
51
- return @heap[par], par
52
- end
53
-
54
- def size
55
- return @heap.size
56
- end
57
-
58
- def is_empty?
59
- return size == 0
60
- end
61
-
62
- def bfs
63
- queue = CollectionUtils::Queue.new
64
- queue.enqueue({element: @heap.first, index: 0})
65
- while true do
66
- node = queue.dequeue
67
- break if node.nil?
68
- left = left_child(node[:index])
69
- right = right_child(node[:index])
70
- yield(node[:element]) if block_given?
71
- queue.enqueue({element: left.first, index: left.last}) if !left.first.nil?
72
- queue.enqueue({element: right.first, index: right.last}) if !right.first.nil?
73
- break if left.first.nil? && right.first
74
- end
75
-
76
- end
77
-
78
- def dfs
79
- stack = CollectionUtils::Stack.new
80
- stack.push({element: @heap.first, index: 0})
81
- while true do
82
- node = stack.pop
83
- break if node.nil?
84
- left = left_child(node[:index])
85
- right = right_child(node[:index])
86
- yield(node[:element]) if block_given?
87
- stack.push({element: left.first, index: left.last}) if !left.first.nil?
88
- stack.push({element: right.first, index: right.last}) if !right.first.nil?
89
- break if left.first.nil? && right.first
90
- end
91
-
92
- end
93
-
94
- end
95
- Tree = Heap
96
- end
97
- end