Skip to content

Commit

Permalink
feat: depth first search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisonCrece committed Aug 19, 2023
1 parent 9d21665 commit 88d52a9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 36 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/Algorithms in Ruby.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 51 additions & 36 deletions algorithms/recursion/binary_tree_tools.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class BinaryTreeTools
# frozen_string literal: true

class BinaryTreeTools # rubocop:disable Style/Documentation
def self.depth_first_search(node:, value:)
return false unless node
return true if node.value == value

return depth_first_search(node: node.right, value: value) if node.value < value
return depth_first_search(node: node.left, value: value) if node.value >= value
end

def self.breath_first_search(head:, needle:)
bfs(head: head, needle: needle)
end
Expand All @@ -15,53 +25,58 @@ def self.post_order_search(head:)
post_order_walk(current_node: head, path: [])
end

def self.compare(a:, b:)
return true if a == nil && b == nil
return false if a == nil || b == nil
return false if a.value != b.value
def self.compare(first:, second:)
return true if first.nil? && second.nil?
return false if first.nil? || second.nil?
return false if first.value != second.value

return compare(a: a.left, b: b.left) && compare(a: a.right, b: b.right)
compare(first: first.left, second: second.left) && compare(first: first.right, second: second.right)
end

private_class_method def self.pre_order_walk(current_node:, path:)
return path unless current_node
path << current_node.value
pre_order_walk(current_node: current_node.left, path: path)
pre_order_walk(current_node: current_node.right, path: path)
path
end
return path unless current_node

path << current_node.value
pre_order_walk(current_node: current_node.left, path: path)
pre_order_walk(current_node: current_node.right, path: path)
path
end

private_class_method def self.in_order_walk(current_node:, path:)
return path unless current_node
in_order_walk(current_node: current_node.left, path: path)
path << current_node.value
in_order_walk(current_node: current_node.right, path: path)
path
end
return path unless current_node

in_order_walk(current_node: current_node.left, path: path)
path << current_node.value
in_order_walk(current_node: current_node.right, path: path)
path
end

private_class_method def self.post_order_walk(current_node:, path:)
return path unless current_node
post_order_walk(current_node: current_node.left, path: path)
post_order_walk(current_node: current_node.right, path: path)
path << current_node.value
path
end
return path unless current_node

post_order_walk(current_node: current_node.left, path: path)
post_order_walk(current_node: current_node.right, path: path)
path << current_node.value
path
end

private_class_method def self.bfs(head:, needle:)
return false unless head
queue = [head]
until queue.empty?
current = queue.shift
return true if current.value == needle
queue << current.left if current.left
queue << current.right if current.right
end
# if we don't find the value
false
end
return false unless head

queue = [head]
until queue.empty?
current = queue.shift
return true if current.value == needle

queue << current.left if current.left
queue << current.right if current.right
end
# if we don't find the value
false
end
end

class Node
class Node # rubocop:disable Style/Documentation
attr_accessor :value, :left, :right

def initialize(value:, left: nil, right: nil)
Expand Down
2 changes: 2 additions & 0 deletions rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Documentation:
Enabled: false
19 changes: 19 additions & 0 deletions spec/recursion/node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'rspec'

RSpec.describe 'Node' do
before do
# Do nothing
end

after do
# Do nothing
end

context 'when condition' do
it 'succeeds' do
pending 'Not implemented'
end
end
end

0 comments on commit 88d52a9

Please sign in to comment.