当前位置: 首页 > 知识库问答 >
问题:

使用Rspec的Ruby递归深度搜索

郗浩
2023-03-14

我目前面临的挑战是理解和实施DFS。假设< code>#find方法接受< code>root和< code>data(归类为节点),如果匹配,则返回< code>title。这是我目前拥有的,也是我唯一能找到的帮助:Ruby递归DFS方法。

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

我正在努力通过

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

我遇到的常见错误

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)>'

我希望返回< code>data (node),但不确定如何解释测试并获得正确的输出。感谢任何帮助和/或建议。谢了。

共有1个答案

莘睿
2023-03-14

下面是您的#find方法:

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

您将返回<code>数据</code>,这将是一个字符串,或者至少这是在比较<code>根时所指示的。title==数据。您希望返回节点本身。

 类似资料:
  • 在这篇文章中,biziclop为非递归深度优先搜索算法插入了伪代码。 如果我们想使用递归DFS算法来检查节点的适当性,我们可以利用两个变体:pre-order(当一个节点在其子节点之前检查时)和post-order(当子节点在节点之前检查时),加上仅针对二叉树的第三个变体(顺序:左子树,然后节点,然后右子树)。 如果可能的话,我对这三个变体都很感兴趣,所以我试图修改biziclop的伪代码,以便获

  • 这是作业,不要贴代码。求你了,谢谢你。 我被指派创建一个计算BST中特定的深度的方法。 为此,我需要a方法。因此,要递归地找到它,我需要创建一个助手方法。 我知道我需要在树中搜索包含我要查找的数据的节点。为此,我编写了以下代码: 然而,这是行不通的,因为每次进行递归调用时,将保持;本质上,它是在重置深度值。我不知道如何在调用之间保持的值。

  • 有没有人看到上述算法存在缺陷?我不是图论方面的专家,但我认为我对递归和迭代有很好的把握,我相信这也是一样的。我想让它在功能上与递归算法等价。它应该维护第一个算法的所有属性,即使不需要这些属性。 当我开始写它的时候,我并不认为我会有三个循环,但结果就是这样。当我环顾谷歌时,我看到了其他的迭代算法,它们只有一个双重嵌套的循环,然而,它们似乎不是从多个来源出发的。(即他们不会发现不连通图的每一个顶点)

  • 假设我有下面的迷宫:(格式不正确) S 表示迷宫的起点,E 表示迷宫的终点。我有两个给定的课程;和 .我必须构建以下递归助手方法来找到迷宫的解决方案: 此方法递归地找到一条从当前迷宫的开始到结束的路径,该路径通过当前Cell。该路径是从迷宫的开始到当前单元格的单元格序列的ArrayList(即到目前为止探索的路径)。为了避免超过所需的路径,算法应避免重新访问已在此路径中的单元格。如果没有从当前到结

  • 这是我的代码: 首先,我从一个列表中做一个二叉查找树,并检查它是否是一个二叉查找树。 给定列表的第一个元素是根节点,后续元素成为子节点。到叶节点。 例如,调用 的结果为: 结果是二叉搜索树,因为左子节点小于父节点,右子节点大于父节点。因此,调用<code>bst_child</code>的结果是<code>True</code>。 然后我添加了寻找二叉查找树深度的代码。通过对第一个列表排序,我制作