IniziaInizia gratis

Correcting bugs in a tree implementation

You have been given a program that is supposed to create the following binary tree:

Graphical representation of a tree.

Testing it, you realize that the program is not correct. Could you correct it so that it works correctly?

Questo esercizio fa parte del corso

Data Structures and Algorithms in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Correct the mistakes in the init() method.
  • Correct the mistake in the creation of the root_node.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

class TreeNode:
  
  def __init__(self, data, left=None, right=None):
    # Correct the mistakes
    self.data = None
    self.left_child = None
    self.right_child = None

node1 = TreeNode("B")
node2 = TreeNode("C")
# Correct the mistake
root_node = TreeNode(node1, node2, "A")
Modifica ed esegui il codice