Question: Ruby Recursive Depth Search with Rspec

Question

Ruby Recursive Depth Search with Rspec

Answers 1
Added at 2017-01-03 03:01
Tags
Question

I'm currently running into a problem understanding and implementing DFS with my current challenge. #find method is suppose to take a root and data (classified as a node) and return the title if there is a match. Here is what I currently have and the only help I could find is here: Ruby recursive DFS method.

class Node
  attr_accessor :title
  attr_accessor :rating
  attr_accessor :left
  attr_accessor :right

  def initialize(title, rating)
    @title = title
    @rating = rating
    @left = nil
    @right = nil
  end
end

class BinarySearchTree

  def initialize(root)
    @root = root
  end

  def insert(root, node)
    if @root.nil?
      @root = node
    else
      current = @root

      while(true) #while an of the below are true statements, keep performing while loop
        if node.rating >= current.rating
          if current.right == nil
            current.right = node
            break
          else
            current = current.right #moving down the right side until nil
          end
        else
          if current.left == nil
            current.left = node
            break
          else
          current = current.left  #moving down the left side until nil
          end
        end
      end
    end
  end

  # Recursive Depth First Search
  def find(root, data)
    #if data == nil
      #return nil
    #elsif root.title == data
      #return data
    #else
    #left = find(root.left, data) if root.left
    #right = find(root.right, data) if root.right
    #left or right
  end

Rspec I'm trying to pass

describe "#find(data)" do
    it "handles nil gracefully" do
      tree.insert(root, empire)
      tree.insert(root, mad_max_2)
      expect(tree.find(root, nil)).to eq nil
    end

    it "properly finds a left node" do
      tree.insert(root, pacific_rim)
      expect(tree.find(root, pacific_rim.title).title).to eq "Pacific Rim"
    end

    it "properly finds a left-left node" do
      tree.insert(root, braveheart)
      tree.insert(root, pacific_rim)
      expect(tree.find(root, pacific_rim.title).title).to eq "Pacific Rim"
    end

    it "properly finds a left-right node" do
      tree.insert(root, donnie)
      tree.insert(root, inception)
      expect(tree.find(root, inception.title).title).to eq "Inception"
    end

    it "properly finds a right node" do
      tree.insert(root, district)
      expect(tree.find(root, district.title).title).to eq "District 9"
    end

    it "properly finds a right-left node" do
      tree.insert(root, hope)
      tree.insert(root, martian)
      expect(tree.find(root, martian.title).title).to eq "The Martian"
    end

    it "properly finds a right-right node" do
      tree.insert(root, empire)
      tree.insert(root, mad_max_2)
      expect(tree.find(root, mad_max_2.title).title).to eq "Mad Max 2: The Road Warrior"
    end
  end

Common error I'm running into

BinarySearchTree#find(data) properly finds a left node
     Failure/Error: expect(tree.find(root, pacific_rim.title).title).to eq "Pacific Rim"

     NoMethodError:
       undefined method `title' for "Pacific Rim":String
     # ./binary_search_tree.rb:37:in `find'
     # ./binary_search_tree_spec.rb:66:in `block (3 levels) in <top (required)>'

I want data (node) returned, but unsure how to interpret the test and get the right output. Any help and/or advice is appreciated. Thanks.

Answers to

Ruby Recursive Depth Search with Rspec

nr: #1 dodano: 2017-01-03 03:01

Here's your #find method:

def find(root, data)
  if data == nil
    return nil
  elsif root.title == data
    return data
  else
    left = find(root.left, data) if root.left
    right = find(root.right, data) if root.right
    left or right
  end
end

You are returning data, which is going to be a string - or at least that's what is indicated when you are comparing root.title == data. You want to return the node itself.

Source Show
◀ Wstecz