def insert(self, anItem): ''' Answer None if the BTree already contains a matching item. If not, insert a deep copy of anItem and answer anItem. ''' searchResult = self.searchTree(anItem) #search for anItem if searchResult['found']: #if anItem already exists in the tree, return None return None else: #if anItem doesn't exist in the tree insertNode = self.readFrom(searchResult['fileIndex']) #get the node anItem should belong in if insertNode.isFull(): #if the node is full, need to split the node toParent = deepcopy(anItem) #make a deepcopy of the item we want to insert leftChild = None #since inserting into a leaf node, left and right children are None rightChild = None while insertNode.isFull(): splitNode = insertNode.addItemAndSplit(toParent, leftChild, rightChild) #split the full node splitNode.setIndex(self.freeIndex) #set the index of the new node to be the next available index self.freeIndex += 1 #increment the free index number toParent = deepcopy(insertNode.getItems()[insertNode.getNumberOfKeys()-1]) #get the item that will go to the parent node insertNode.getItems()[insertNode.getNumberOfKeys()-1] = None #get rid of the record of the parent item insertNode.getChild()[insertNode.getNumberOfKeys()] = None #get rid of the right child of the parent in the insertNode insertNode.setNumberOfKeys(insertNode.getNumberOfKeys()-1) leftChild = insertNode.getIndex() #get the left and right indices of the parent rightChild = splitNode.getIndex() self.writeAt(leftChild, insertNode) #write both nodes to the file self.writeAt(rightChild, splitNode) insertNode = self.stackOfNodes.pop() #get the parent node if insertNode == None: #if there is no parent, create a new root node insertNode = BTreeNode(self.degree) #create a new node insertNode.setIndex(self.freeIndex) #set the index of the new root node self.rootNode = insertNode self.rootIndex = self.freeIndex #set the root index self.freeIndex += 1 #increment the free index number insertNode.insertItem(toParent, leftChild, rightChild)#if the parent node isn't full, just insert the item self.writeAt(insertNode.getIndex(), insertNode) #write the final node to the file else: #there is room for anItem, so just perform an insert insertNode.insertItem(deepcopy(anItem)) self.writeAt(searchResult['fileIndex'], insertNode) #write the node to the file return anItem #return the item
def insert(self, anItem): ''' Answer None if the BTree already contains a matching item. If not, insert a deep copy of anItem and answer anItem. ''' searchResult = self.searchTree(anItem) #search for anItem if searchResult[ 'found']: #if anItem already exists in the tree, return None return None else: #if anItem doesn't exist in the tree insertNode = self.readFrom(searchResult['fileIndex'] ) #get the node anItem should belong in if insertNode.isFull( ): #if the node is full, need to split the node toParent = deepcopy( anItem) #make a deepcopy of the item we want to insert leftChild = None #since inserting into a leaf node, left and right children are None rightChild = None while insertNode.isFull(): splitNode = insertNode.addItemAndSplit( toParent, leftChild, rightChild) #split the full node splitNode.setIndex( self.freeIndex ) #set the index of the new node to be the next available index self.freeIndex += 1 #increment the free index number toParent = deepcopy(insertNode.getItems()[ insertNode.getNumberOfKeys() - 1]) #get the item that will go to the parent node insertNode.getItems()[ insertNode.getNumberOfKeys() - 1] = None #get rid of the record of the parent item insertNode.getChild()[insertNode.getNumberOfKeys( )] = None #get rid of the right child of the parent in the insertNode insertNode.setNumberOfKeys(insertNode.getNumberOfKeys() - 1) leftChild = insertNode.getIndex( ) #get the left and right indices of the parent rightChild = splitNode.getIndex() self.writeAt(leftChild, insertNode) #write both nodes to the file self.writeAt(rightChild, splitNode) insertNode = self.stackOfNodes.pop() #get the parent node if insertNode == None: #if there is no parent, create a new root node insertNode = BTreeNode(self.degree) #create a new node insertNode.setIndex( self.freeIndex ) #set the index of the new root node self.rootNode = insertNode self.rootIndex = self.freeIndex #set the root index self.freeIndex += 1 #increment the free index number insertNode.insertItem( toParent, leftChild, rightChild ) #if the parent node isn't full, just insert the item self.writeAt(insertNode.getIndex(), insertNode) #write the final node to the file else: #there is room for anItem, so just perform an insert insertNode.insertItem(deepcopy(anItem)) self.writeAt(searchResult['fileIndex'], insertNode) #write the node to the file return anItem #return the item