BinarySearchk 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/BinarySearchk.gemspec +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +5 -0
- data/lib/BinarySearchk/BinarySearchk.rb +81 -0
- data/lib/BinarySearchk/version.rb +3 -0
- data/lib/BinarySearchk.rb +2 -0
- data/spec/binarysearchk_spec.rb +41 -0
- data/spec/spec_helper.rb +97 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 858b852537424054e0f43ea1e38c882800914064
|
4
|
+
data.tar.gz: ec7199d8791374402d6ddb8061b0407173a3c3ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8997b23a4e67090afaa30ad789f175672cfd23768345453dcdb9876dbeab69f4232fd8e8d0279cc45319a397958ceedc1d37d6cecd744c16bca17e8ca888fbc
|
7
|
+
data.tar.gz: 888077c978156150984763aa65d15418da28fe09530cab295c002754385bf95841b746e85d6adfc04acf2c7e73db40d068501da51b193ab337840383039be94c
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'BinarySearchk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "BinarySearchk"
|
8
|
+
spec.version = BinarySearchk::VERSION
|
9
|
+
spec.authors = ["Kayode Adeniyi"]
|
10
|
+
spec.email = ["kayode.adeniyi@andela.co"]
|
11
|
+
spec.summary = %q{This gem builds a tree when an array is supplied and employs either DFS or BFS to search for any child}
|
12
|
+
spec.description = %q{This gem employs Breadth-First-Search or Depth-First-Search to search whether an element is in a tree. }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Kayode Adeniyi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# BinarySearchk
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'BinarySearchk'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install BinarySearchk
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/BinarySearchk/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
module BinarySearchk
|
2
|
+
class Node
|
3
|
+
attr_accessor :left, :right, :value
|
4
|
+
|
5
|
+
def initialize(v=nil)
|
6
|
+
@value = v
|
7
|
+
@left = nil
|
8
|
+
@right = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def insert(v)
|
12
|
+
case value <=> v
|
13
|
+
when 1 then insert_left(v)
|
14
|
+
when -1 then insert_right(v)
|
15
|
+
when 0 then false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def insert_left(v)
|
20
|
+
if left.nil?
|
21
|
+
self.left = Node.new(v)
|
22
|
+
else
|
23
|
+
left.insert(v)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def insert_right(v)
|
28
|
+
if right.nil?
|
29
|
+
self.right = Node.new(v)
|
30
|
+
else
|
31
|
+
right.insert(v)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.build_tree(array)
|
36
|
+
right = array.length-1
|
37
|
+
left=0
|
38
|
+
|
39
|
+
index_mid = (left + (right-left)) / 2
|
40
|
+
@node = Node.new(array[index_mid])
|
41
|
+
array.each {|v| @node.insert(v) }
|
42
|
+
|
43
|
+
@node
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.breadth_first_search(target_value, node)
|
47
|
+
queue = [node]
|
48
|
+
until queue.empty?
|
49
|
+
current_node = queue.shift
|
50
|
+
return "Found #{current_node.value}" if current_node.value == target_value
|
51
|
+
queue.push current_node.left unless current_node.left.nil?
|
52
|
+
queue.push current_node.right unless current_node.right.nil?
|
53
|
+
end
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.depth_first_search(target_value, node)
|
58
|
+
stack = [node]
|
59
|
+
until stack.empty?
|
60
|
+
current_node = stack.pop
|
61
|
+
return "Found #{current_node.value}" if current_node.value == target_value
|
62
|
+
stack.push current_node.right unless current_node.right.nil?
|
63
|
+
stack.push current_node.left unless current_node.left.nil?
|
64
|
+
end
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.dfs_rec(target_value, node)
|
69
|
+
if node.nil?
|
70
|
+
return nil
|
71
|
+
else
|
72
|
+
return "Found #{node.value}" if node.value == target_value
|
73
|
+
search_left_child = dfs_rec(target_value, node.left)
|
74
|
+
search_right_child = dfs_rec(target_value, node.right)
|
75
|
+
end
|
76
|
+
#return any of the two that is not nil
|
77
|
+
return (search_right_child or search_left_child)
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BinarySearchk do
|
4
|
+
it 'creates a node with value 10' do
|
5
|
+
tree = BinarySearchk::Node.new(10)
|
6
|
+
expect(tree.value).to eq(10)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'creates a tree with children' do
|
10
|
+
arr = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]
|
11
|
+
tree = BinarySearchk::Node.build_tree(arr)
|
12
|
+
expect(tree.value).to eq(4)
|
13
|
+
expect("#{tree.left.value if !tree.left.nil?}").to eq("1")
|
14
|
+
expect("#{tree.right.value unless tree.right.nil?}").to eq("7")
|
15
|
+
expect("#{tree.left.left.value unless tree.left.left.nil?}").to eq("")
|
16
|
+
expect("#{tree.left.right.value unless tree.left.right.nil?}").to eq("3")
|
17
|
+
expect("#{tree.right.left.value unless tree.right.left.nil?}").to eq("5")
|
18
|
+
expect("#{tree.right.right.value unless tree.right.right.nil?}").to eq("23")
|
19
|
+
expect("#{tree.right.right.left.value unless tree.right.right.left.nil?}").to eq("8")
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'Uses BFS to search for a number' do
|
23
|
+
arr = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]
|
24
|
+
tree = BinarySearchk::Node.build_tree(arr)
|
25
|
+
expect(BinarySearchk::Node.breadth_first_search(7, tree)).to eq("Found 7")
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'Uses DFS to search for a number' do
|
29
|
+
arr = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]
|
30
|
+
tree = BinarySearchk::Node.build_tree(arr)
|
31
|
+
expect(BinarySearchk::Node.depth_first_search(6345, tree)).to eq("Found 6345")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Uses recursive DFS to search for a number' do
|
35
|
+
arr = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]
|
36
|
+
tree = BinarySearchk::Node.build_tree(arr)
|
37
|
+
expect(BinarySearchk::Node.dfs_rec(9, tree)).to eq("Found 9")
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'BinarySearchk'
|
2
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
3
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
4
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
5
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
6
|
+
# files.
|
7
|
+
#
|
8
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
9
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
10
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
11
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
12
|
+
# a separate helper file that requires the additional dependencies and performs
|
13
|
+
# the additional setup, and require it from the spec files that actually need
|
14
|
+
# it.
|
15
|
+
#
|
16
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
17
|
+
# users commonly want.
|
18
|
+
#
|
19
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
=begin
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Allows RSpec to persist some state between runs in order to support
|
55
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
56
|
+
# you configure your source control system to ignore this file.
|
57
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
58
|
+
|
59
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
60
|
+
# recommended. For more details, see:
|
61
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
62
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
63
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
64
|
+
config.disable_monkey_patching!
|
65
|
+
|
66
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
67
|
+
# be too noisy due to issues in dependencies.
|
68
|
+
config.warnings = true
|
69
|
+
|
70
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
71
|
+
# file, and it's useful to allow more verbose output when running an
|
72
|
+
# individual spec file.
|
73
|
+
if config.files_to_run.one?
|
74
|
+
# Use the documentation formatter for detailed output,
|
75
|
+
# unless a formatter has already been configured
|
76
|
+
# (e.g. via a command-line flag).
|
77
|
+
config.default_formatter = 'doc'
|
78
|
+
end
|
79
|
+
|
80
|
+
# Print the 10 slowest examples and example groups at the
|
81
|
+
# end of the spec run, to help surface which specs are running
|
82
|
+
# particularly slow.
|
83
|
+
config.profile_examples = 10
|
84
|
+
|
85
|
+
# Run specs in random order to surface order dependencies. If you find an
|
86
|
+
# order dependency and want to debug it, you can fix the order by providing
|
87
|
+
# the seed, which is printed after each run.
|
88
|
+
# --seed 1234
|
89
|
+
config.order = :random
|
90
|
+
|
91
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
92
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
93
|
+
# test failures related to randomization by passing the same `--seed` value
|
94
|
+
# as the one that triggered the failure.
|
95
|
+
Kernel.srand config.seed
|
96
|
+
=end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: BinarySearchk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kayode Adeniyi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: 'This gem employs Breadth-First-Search or Depth-First-Search to search
|
56
|
+
whether an element is in a tree. '
|
57
|
+
email:
|
58
|
+
- kayode.adeniyi@andela.co
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- BinarySearchk.gemspec
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/BinarySearchk.rb
|
71
|
+
- lib/BinarySearchk/BinarySearchk.rb
|
72
|
+
- lib/BinarySearchk/version.rb
|
73
|
+
- spec/binarysearchk_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: This gem builds a tree when an array is supplied and employs either DFS or
|
99
|
+
BFS to search for any child
|
100
|
+
test_files:
|
101
|
+
- spec/binarysearchk_spec.rb
|
102
|
+
- spec/spec_helper.rb
|