Get startedGet started for free

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?

This exercise is part of the course

Data Structures and Algorithms in Python

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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")
Edit and Run Code