ds_algo 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3c1d14669980be5c2cd68fe505c7b3aa8f3e4b8e300bb3ec8d428adaa631529
4
- data.tar.gz: 805e016b98d8266edc71b45e4c0d143fbdda494d4df49595cd357d94be391968
3
+ metadata.gz: 6fc3ae32de8f716602291fada0719eea3e9e23b7b4aadca52492b6a8ce7d4a43
4
+ data.tar.gz: 519a96cd24fe9c8ca97898675b5d2692d7ec0eb574ce24f43da6f632173fbecd
5
5
  SHA512:
6
- metadata.gz: c4a9064e64336177c063051a46abf14a170f12a066d354ad20f03b9b4bcb2b237439a5261390bd47c683f37a06b098d69b28017f19004dfa26cbf0d0bb58e6da
7
- data.tar.gz: 4b77c762d047d4505bfa6f8c0bfd79518d1dfded2a74484df8daaaef781ec0b275a980dd76cc16333a62f2afdfa6e3d4f6d97ac0d79fd097ae8adc106ea09ba4
6
+ metadata.gz: 15bb60332a8a4de4ce20ebc5a0aca6bafa881ea12cb65ac9e3dca20de4e87fbae927ca67293a2640099a5b3152e7dc48df8823dd16e92847c411f3c2f19ebb97
7
+ data.tar.gz: 15350ac6d7971f5775326ea41db49c57496ff86f732fc6c6d323574bdc98a1cbb8a7bb152c3fcd2a7108fd87c83d0b77c2c36f3994a84f32937b5afcc5c9dd4a
@@ -2,7 +2,9 @@
2
2
 
3
3
  # lib/data_struct/linked_list.rb
4
4
 
5
+ # This module consists of all nodes and implementation of linked list
5
6
  module LinkedList
7
+ # This node for Singly Linked list implementation.
6
8
  class SinglyNode
7
9
  attr_accessor :data, :next_val
8
10
  def initialize(data, next_val = nil)
@@ -10,7 +12,7 @@ module LinkedList
10
12
  @next_val = next_val
11
13
  end
12
14
  end
13
-
15
+ # This node for Doubly Linked list implementation.
14
16
  class DoublyNode
15
17
  attr_accessor :data, :next_val, :pre_val
16
18
  def initialize(data, pre_val = nil, next_val = nil)
@@ -19,7 +21,7 @@ module LinkedList
19
21
  @pre_val = pre_val
20
22
  end
21
23
  end
22
-
24
+ # This is a singly linked list implementation.
23
25
  class Singly
24
26
  attr_reader :head, :length
25
27
  def initialize
@@ -118,7 +120,7 @@ module LinkedList
118
120
  @length -= 1
119
121
  end
120
122
  end
121
-
123
+ # This is a Doubly linked list implementation.
122
124
  class Doubly
123
125
  attr_reader :head, :length
124
126
  def initialize
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # This is a implementation of Queue using an array
3
4
  class Queue
4
5
  attr_reader :length
5
6
  def initialize
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # This is a implementation of stack using array
3
4
  class Stack
4
5
  attr_reader :length
5
6
  def initialize
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Tree moudle consists of different implementation of tree in different nodes
4
+ module Tree
5
+ # This node primarily for binary tree and binary search tree
6
+ class BinaryNode
7
+ attr_accessor :data, :left, :right
8
+ def initialize(data, left = nil, right = nil)
9
+ @data = data
10
+ @left = left
11
+ @right = right
12
+ end
13
+ end
14
+ # This node is general purpose multi child node
15
+ class NormalNode
16
+ attr_accessor :data, :left, :right
17
+ def initialize(data, left = [], right = [])
18
+ @data = data
19
+ @left = left
20
+ @right = right
21
+ end
22
+ end
23
+ # This is implementation Binary search tree using Tree::BinaryNode
24
+ class BST
25
+ attr_reader :length
26
+ def initialize
27
+ @root = nil
28
+ @length, @height = 0
29
+ @state_arr = []
30
+ end
31
+
32
+ def insert val
33
+ in_length
34
+ @root = recur_insert @root, val
35
+ end
36
+ def find val
37
+ recur_search @root, val
38
+ end
39
+ def to_a
40
+ @state_arr = []
41
+ recur_pre_order(@root)
42
+ @state_arr
43
+ end
44
+ def empty?
45
+ @length.zero?
46
+ end
47
+ def height?
48
+ recur_max_depth @root
49
+ end
50
+ private
51
+ def recur_insert node, val
52
+ return BinaryNode.new(val) if node.nil?
53
+ if val < node.data
54
+ node.left = recur_insert(node.left, val)
55
+ elsif val > node.data
56
+ node.right = recur_insert(node.right, val)
57
+ end
58
+ node
59
+ end
60
+
61
+ def recur_search node, val
62
+ if node.nil? || node.data == val
63
+ return node
64
+ end
65
+ if node.data < val
66
+ return recur_search(node.right, val)
67
+ end
68
+ recur_search(node.left, val)
69
+ end
70
+
71
+ def recur_pre_order node
72
+ return if node.nil?
73
+ @state_arr << node.data
74
+ recur_pre_order(node.left)
75
+ recur_pre_order(node.right)
76
+ end
77
+ def recur_max_depth node
78
+ return 0 if node.nil?
79
+ left_depth = recur_max_depth(node.left)
80
+ right_depth = recur_max_depth(node.right)
81
+
82
+ if left_depth > right_depth
83
+ return left_depth + 1
84
+ else
85
+ return right_depth + 1
86
+ end
87
+ end
88
+ def in_length
89
+ @length += 1
90
+ end
91
+
92
+ def de_length
93
+ @length -= 1
94
+ end
95
+ end
96
+ end
data/lib/ds_algo.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # This is class used to learn about algo and ds
3
4
  class DsAlgo
5
+ # #ds-eff? => nil
6
+ # used to know the efficiency of the data structure, its a table in a terminal
4
7
  def ds_eff?
5
8
  puts '+-----------------------------------------------------------------------------------+'
6
9
  puts '| Efficency table for DsAlgo |'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ds_algo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - imhtapm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-27 00:00:00.000000000 Z
11
+ date: 2019-07-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: collection of data structures and algorithms for learning and to build
14
14
  (under development)
@@ -20,11 +20,13 @@ files:
20
20
  - lib/data_struct/linked_list.rb
21
21
  - lib/data_struct/queue.rb
22
22
  - lib/data_struct/stack.rb
23
+ - lib/data_struct/tree.rb
23
24
  - lib/ds_algo.rb
24
25
  homepage: https://rubygems.org/gems/ds_algo
25
26
  licenses:
26
27
  - MIT
27
- metadata: {}
28
+ metadata:
29
+ source_code_uri: https://github.com/imhtapm/ds_algo
28
30
  post_install_message:
29
31
  rdoc_options: []
30
32
  require_paths: