another interesting aspect of the implementation of treenode is that we use python' optional parameters optional parameters make it easy for us to create treenode under several different circumstances sometimes we will want to construct new treenode that already has both parent and child with an existing parent and childwe can pass parent and child as parameters at other times we will just create treenode with the key value pairand we will not pass any parameters for parent or child in this casethe default values of the optional parameters are used now that we have the binarysearchtree shell and the treenode it is time to write the put method that will allow us to build our binary search tree the put method is method of the binarysearchtree class this method will check to see if the tree already has root if there is not root then put will create new treenode and install it as the root of the tree if root node is already in place then put calls the privaterecursivehelper function _put to search the tree according to the following algorithmstarting at the root of the treesearch the binary tree comparing the new key to the key in the current node if the new key is less than the current nodesearch the left subtree if the new key is greater than the current nodesearch the right subtree when there is no left (or rightchild to searchwe have found the position in the tree where the new node should be installed to add node to the treecreate new treenode object and insert the object at the point discovered in the previous step below we show the python code for inserting new node in the tree the _put function is written recursively following the steps outlined above notice that when new child is inserted into the treethe current_node is passed to the new tree as the parent one important problem with our implementation of insert is that duplicate keys are not handled properly as our tree is implemented duplicate key will create new node with the same key value in the right subtree of the node having the original key the result of this is that the node with the new key will never be found during search better way to handle the insertion of duplicate key is for the value associated with the new key to replace the old value we leave fixing this bug as an exercise for you def put(selfkeyval)if self rootself _put(keyvalself rootelseself root treenode(keyvalself size self size def _put(selfkeyvalcurrent_node)if key current_node keyif current_node has_left_child()self _put(keyvalcurrent_node left_childelsecurrent_node left_child treenode(keyvalparent=current_nodeelseif current_node has_right_child() trees and tree algorithms