Skip to content

Traversing of tree nodes faileure at node E and node B at level L1 #1

Description

@mandalorian99

graph

In above directed graph ,each node represent a town . Each node is represent by a object . In each node object there is a array called adjacent_nodes which contains the child/adjacent nodes of each node . like this
objects

There is method called find_paths (its an class method) and it is expected to return no of paths b/w any to points x and y . In our case we are finding no_of_paths b/w node A to C .

Node.find_paths('A','C') #app.rb line 40

code for function find_paths

#find count of how many paths there exists
#for any given point X and Y
#node.rb
`
def self.find_paths(source,terminal)

p "----------#{source}---#{@@count}---------------"
root = Node.get_obeject(source)
p root
p "obect catched.."
#p root
if root == nil or source == terminal
  p " path terinated..."
  has_child = false 
else 
  p "has child..."
  child_stack = root.children 
  p child_stack
  has_child = true
end

if source == terminal 
  p "source equals to terminal , incrementing..."
  @@count += 1
end

if has_child
  loop do 
    child = child_stack.pop 
    #p "loop for child #{child}----"
    if child != nil 
      Node.find_paths(child , terminal)
    else 
      break 
    end
  end
end 
"no of paths b/w #{source} to #{terminal} = #{@@count}"

end
`
Now bug is that when ever we traversing for node A to C
bus-path-tre

at level L2 it is failing at node E and then node B because in line 137 in node.rb we fetching object of E and in this object adjacent_nodes array is empty mean magically childs for node E are vanished from array and the traversing halted due to this , similar issue with node B . Look below object we getting for E and B
on child objects

since there is no child for node E and node B ,we couldn't traverse and due to this it shows possible path b/w A to C is 2 routes .. but expected output is 4 routes

NOTE : find_paths method uses two methods get_object to get object of any node and find_value for getting name of node .
Ruby version 2.6.3 running on windows x86 machine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions