Question: Q 2 ( 5 0 points ) . Find a path between two nodes in a binary search tree. Implement function find _ path (
Q points Find a path between two nodes in a binary search tree. Implement function findpathelementkey, elementkey The function takes two keys as input and returns the path between the corresponding two nodes including both ends In case of either key does not exist, return an empty list. You must use TreeNode object from treenode.py You are not allowed to change treenode.py
Class TreeNode:
def initself key, val, leftNone, rightNone, parentNone:
self.key key
self.payload val
self.leftChild left
self.rightChild right
self.parent parent
def hasLeftChildself:
return self.leftChild
def hasRightChildself:
return self.rightChild
def strself:
return strselfkey strselfpayload
Below is the template for Question
UMass ECE Advanced Programming
Homework # Fall
questionpy Binary Search Tree
from treenode import TreeNode
class BinarySearchTree:
def initself:
self.root None
def putself key, val:
if self.root:
self.putkey val, self.root
else:
self.root TreeNodekey val
def putself key, val, node:
if key node.key:
if node.hasLeftChild:
self.putkey val, node.leftChild
else:
node.leftChild TreeNodekey val, parentnode
else:
if node.hasRightChild:
self.putkey val, node.rightChild
else:
node.rightChild TreeNodekey val, parentnode
def setitemself k v:
self.putk v
def getself key:
res self.getkey self.root
if res is None:
return None
return res
def getself key, node:
if node is None:
return None
if node.key key:
return node
if key node.key:
return self.getkey node.leftChild
else:
return self.getkey node.rightChild
def getitemself key:
return self.getkey
def findpathself elementkey, elementkey:
TODO: Fill in this function to get a path from element to element
pass
def main:
mytree BinarySearchTree
mytree "red"
mytree "yellow"
mytree "blue"
mytree "pew"
printmytree
printmytree
path mytree.findpath
for i node in enumeratepath:
printnode end if i lenpath else
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
