Aan de slagGa gratis aan de slag

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?

Deze oefening maakt deel uit van de cursus

Data Structures and Algorithms in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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")
Code bewerken en uitvoeren