Question: How can I implement the binary search Tree in python? Methods Your class should implement (at least) the following methods: __init__(self) Initializes a tree node

How can I implement the binary search Tree in python?

Methods

Your class should implement (at least) the following methods:

__init__(self)

Initializes a tree node to be empty. Attribute values are set to None.

add(self, val)

Adds a node with the value val to the tree so as to maintain the binary search tree property (see above).

find(self, val)

Returns: a reference to the tree node with value val if it exists, None otherwise.

__str__(self)

Returns a string representation of the tree. For the purposes of this assignment, this is defined as follows. Given a tree T:

If T is None (i.e., empty), return the string "None" (the quotation marks simply indicate that the value is a string; they are not part of the string itself).

Otherwise, suppose that T has a root node with value val and left and right subtrees Tleft and Tright. Return the string

"({:d} {} {})".format( val, str(Tleft), str(Tright))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!