I wrote a code for Simple Tree Matching for matching two DOM nodes. It is showing some error. I am not able to figure it out.
I am posting the code and the error trace.
def tree_match(node1,node2)
# ------- Implementation of Simple Tree matching algorithm.
if node1.name!=node2.name
puts 'in base case'
return 0
elsif node1.children.empty? or node2.children.empty?
return 0
else
dp = Array.new(node1.children.size) {Array.new(node2.children.size,0)}
i=1
j=0
j = j+1
node1.children.each do |child1|
node2.children.each do |child2|
Line 180th. main error is in the code segment below.
dp[i][j] = [dp[i-1][j],dp[i][j-1],(dp[i-1][j-1] + tree_match(child1,child2))].max
j = j + 1
end
i = i+1
end
return 1+dp[dp[0].size-1][dp[1].size-1]
end
end
ERROR:
in block (2 levels) in tree_match': undefined method[]' for nil:NilClass (NoMethodError)
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:187:in block in each'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:inupto'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in each'
from /<>/DomTreeParser.rb:179:inblock in tree_match'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:187:in block in each'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:inupto'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in each'
from /<path>/DomTreeParser.rb:178:intree_match'
from /<>/DomTreeParser.rb:180:in block (2 levels) in tree_match'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:187:inblock in each'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in upto'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:ineach'
from /<>/DomTreeParser.rb:179:in block in tree_match'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:187:inblock in each'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:in upto'
from /usr/local/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.6.2/lib/nokogiri/xml/node_set.rb:186:ineach'
from /<>/DomTreeParser.rb:178:in tree_match'
from testDriver.rb:11:in'
EDIT: nil class error is on line 180. I have marked it.
jbefore the inner loop (i.e. before line 179). You are incrementing it over the whole 2D array. - undur_gongorj? - undur_gongor