예제 #1
0
파일: RBT.py 프로젝트: omcevoy/UO_Classwork
 def insert(self, key):
     #add a key to the tree. Don't forget to fix up the tree and balance the nodes.
     #is there a root? if not, what do you have to do?
     #if there is, how does that change things?
     if self.root is None:  #checks to see if the root is none, if not the new node will become the root
         node = RB_Node(key)
         self.root = node
         node.parent = self.sentinel  #sets parents and children to sentinel nodes
         node.leftChild = self.sentinel
         node.rightChild = self.sentinel
         node.color = "black"  #root node is always black
     else:
         node = RB_Node(key)
         y = self.sentinel
         x = self.root
         while x != self.sentinel:  #x is the root, will go down the tree until x reaches a sentinel node
             y = x  #keeps track of the parent node
             if node.key < x.key:  #determining which path x will take
                 x = x.leftChild
             else:
                 x = x.rightChild
         node.parent = y  #sets the parent to y
         if y == self.sentinel:  #if y was a sentinel, the node must become the root since only
             self.root = node  #the root has a sentinel parent
         elif node.key < y.key:
             y.leftChild = node  #determining if it's a left or right child
         else:
             y.rightChild = node
         node.leftChild = self.sentinel  #sets the children to sentinels
         node.rightChild = self.sentinel
         node.color = "red"  #newly inserted nodes are red
         self._rbInsertFixup(node)
예제 #2
0
파일: RBT.py 프로젝트: kahosaka/coursework
    def insert(self, key):
        '''
            Add a key to the tree. Don't forget to fix up the tree ad balance the nodes.
        '''
        # Create new node
        z = RB_Node(key)

        # Set y to be sentinel
        y = self.sentinel

        # Set x to be root
        x = self.root

        # If the root is none, set x to be sentinel
        if (x == None):
            x = self.sentinel

        # While x is not the sentinel...
        while (x != self.sentinel):
            # Sentinel = root
            y = x

            # If z's key is greater than x's key...
            if (z.key < x.key):
                # Make x the left child
                x = x.leftChild
            else:
                # Make x the right child
                x = x.rightChild
        # Make z's parent y
        z.parent = y

        # If y is equal to the sentinel...
        if (y == self.sentinel):
            # Set the root to be z
            self.root = z
        # If z's key is less than y's key...
        elif (z.key < y.key):
            # Set y's left child equal to z
            y.leftChild = z
        else:
            # Else, set y's right child equal to z
            y.rightChild = z
        # Set z's left child to be sentinel
        z.leftChild = self.sentinel

        # Set z's right child to be sentinel
        z.rightChild = self.sentinel

        # Set z's color to be red
        z.color = 'red'

        # Call insert fixup
        self._rbInsertFixup(z)
예제 #3
0
    def insert(self, key):
        """ 
        Add a key to the tree. Don't forget to fix up the tree and balance the nodes.

        Insert always needs to search to the bottom of the tree to find where where
        the new key (node z) should be.

        You put z there and color it read to maintain the black-height, but then 
        you need to check if this causes a problem with two reds in a row, and
        if this is the case, then we fix this issue by color shifting/rotation which
        is seen later on.

        """
        z = RB_Node(key)  # creates node
        y = self.sentinel  # basically means null
        x = self.root

        if x is None:
            x = self.sentinel

        while x != self.sentinel:
            y = x
            if z.key < x.key:
                x = x.leftChild
            else:
                x = x.rightChild

        z.parent = y

        if y == self.sentinel:
            self.root = z

        elif z.key < y.key:
            y.leftChild = z

        else:
            y.rightChild = z

        z.leftChild = self.sentinel
        z.rightChild = self.sentinel
        z.color = "Red"
        self._rbInsertFixup(z)
예제 #4
0
    def insert(self, key):
        # add a key to the tree.Don't forget to fix up the tree and balance the nodes.
        #root does not exist yet

        y = self.sentinel
        x = self.root
        z = RB_Node(key, self.sentinel, self.sentinel, self.sentinel, 'red')

        if x == None:  #insert a node for root if the tree is empty
            self.root = z
            x = self.root

        while x != self.sentinel:  #move down the tree until hitting a sentinel node
            y = x
            if z.key < x.key:
                x = x.leftChild
            else:
                x = x.rightChild
        z.parent = y
        if y == self.sentinel:
            self.root = z  # case for tree being empty
        elif z.key < y.key:
            y.leftChild = z  #go left
            #print key,
            #print 'inserted as a left child of ',
            #print y.key
        else:
            y.rightChild = z  #go right
            #print key,
            #print ' inserted as a right child of',
            #print y.key
        z.leftChild = self.sentinel
        z.rightChild = self.sentinel
        z.color = 'red'
        self.size += 1
        self._rbInsertFixup(z)