What do k and d represent in the find function? I'm still confused about the find function, can you describe the iterations in more detail, step by step?

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

What do k and d represent in the find function?

I'm still confused about the find function, can you describe the iterations in more detail, step by step?

 

 

This is the Python Code:

class TreeNode:
   def __init__(self, v, l=None, r=None):
       self.value = v
       self.left = l
       self.right = r
def find(n, v, k, d):
   if v == n.value:
       return 1 
   if k < d:
       if n.left and find(n.left, v, k+1, d):
           return 1
       if n.right and find(n.right, v, k+1, d):
           return 1
       return 0
def deepening(n, v, d):
   for k in range(0, d):
       if find(n, v, k, d):
           return 1
       else:
           return 0
def build_tree():
   root = TreeNode('A')
   root.left = TreeNode('B')
   root.left.left = TreeNode('C')
   root.right = TreeNode('D')
   root.right.left = TreeNode('E')
   root.right.right = TreeNode('F')
   root.right.right.right = TreeNode('G')
   return root
root = build_tree()
for v in ['C', 'E', 'F', 'G']:
   print(deepening(root, v, 2))

 

output: 

1

1

1

0

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning