Course Solutions Uncategorized (Solved) : Class Treenode Def Init Self Key Val Left None Right None Parent None Selfkey Key Selfpayl Q38174599 . . . .

(Solved) : Class Treenode Def Init Self Key Val Left None Right None Parent None Selfkey Key Selfpayl Q38174599 . . . .

 

Problem 5 Binary Search Trees (30 points) Modify the books implementation of the binary search tree described in chapters 6.1

class TreeNode:
def __init__(self,key,val,left=None,right=None,parent=None):
self.key = key
self.payload = val
self.leftChild = left
self.rightChild = right
self.parent = parent

def hasLeftChild(self):
return self.leftChild

def hasRightChild(self):
return self.rightChild

def isLeftChild(self):
return self.parent and self.parent.leftChild == self

def isRightChild(self):
return self.parent and self.parent.rightChild == self

def isRoot(self):
return not self.parent

def isLeaf(self):
return not (self.rightChild or self.leftChild)

def hasAnyChildren(self):
return self.rightChild or self.leftChild

def hasBothChildren(self):
return self.rightChild and self.leftChild

def replaceNodeData(self,key,value,lc,rc):
self.key = key
self.payload = value
self.leftChild = lc
self.rightChild = rc
if self.hasLeftChild():
self.leftChild.parent = self
if self.hasRightChild():
self.rightChild.parent = self

class BinarySearchTree:

def __init__(self):
self.root = None
self.size = 0

def length(self):
return self.size

def __len__(self):
return self.size

def put(self,key,val):
if self.root:
self._put(key,val,self.root)
else:
self.root = TreeNode(key,val)
self.size = self.size +

OR

PayPal Gateway not configured

OR

PayPal Gateway not configured

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

(Solved) : Exercise 4 Powerset Construction 2 P Transform Following Nfa Dfa Using Powerset Constructi Q26159990 . . . .(Solved) : Exercise 4 Powerset Construction 2 P Transform Following Nfa Dfa Using Powerset Constructi Q26159990 . . . .

<br/><img src="https://media.cheggcdn.com/media%2F103%2F103cf72c-816f-4b38-a173-ae97f565c2cb%2Fimage" alt="Exercise 4 (Powerset construction) (2 P Transform the following NFA into a DFA by using the powerset construction. Hint: the resulting DFA has 6 reachable states. a, b