Beispiel #1
0
def coefficientOfRelationship(id1,id2):
    
        if( not idExists(id1)):
            return (id1 + " does not exist")
       
        if( not idExists(id2)):
            return (id2 + " does not exist")
    
        if (id1 == id2):
            return 1
        
        print("writing lineage list1")
        lineageList = idToLineageList(id1)
        print("filling tree1 from list")
        tree1 = lineageListToTree(lineageList)
        cleanUpTupleTree(tree1)      
        
        print("writing lineage list2")
        lineageList = idToLineageList(id2)
        print("filling tree2 from list")
        tree2 = lineageListToTree(lineageList)
        cleanUpTupleTree(tree2)  
        
        hypotheticalChild = BinaryTree("Hypothetical Child")
        hypotheticalChild.setLeft(tree1)
        hypotheticalChild.setRight(tree2)
        return 2 * COI(hypotheticalChild)
Beispiel #2
0
    def remove(self, item):
        """Removes and returns item from the tree."""

        # Helper function to adjust placement of an item
        def liftMaxInLeftSubtreeToTop(top):
            # Replace top's datum with the maximum datum in the left subtree
            # Pre:  top has a left child
            # Post: the maximum node in top's left subtree
            #       has been removed
            # Post: top.getRoot() = maximum value in top's left subtree
            parent = top
            currentNode = top.getLeft()
            while not currentNode.getRight().isEmpty():
                parent = currentNode
                currentNode = currentNode.getRight()
            top.setRoot(currentNode.getRoot())
            if parent == top:
                top.setLeft(currentNode.getLeft())
            else:
                parent.setRight(currentNode.getLeft())

        # Begin main part of the method
        if self.isEmpty(): return None

        # Attempt to locate the node containing the item
        itemRemoved = None
        preRoot = BinaryTree(None)
        preRoot.setLeft(self._tree)
        parent = preRoot
        direction = 'L'
        currentNode = self._tree
        while not currentNode.isEmpty():
            if currentNode.getRoot() == item:
                itemRemoved = currentNode.getRoot()
                break
            parent = currentNode
            if currentNode.getRoot() > item:
                direction = 'L'
                currentNode = currentNode.getLeft()
            else:
                direction = 'R'
                currentNode = currentNode.getRight()

        # Return None if the item is absent
        if itemRemoved == None: return None

        # The item is present, so remove its node

        # Case 1: The node has a left and a right child
        #         Replace the node's value with the maximum value in the
        #         left subtree
        #         Delete the maximium node in the left subtree
        if not currentNode.getLeft().isEmpty() \
           and not currentNode.getRight().isEmpty():
            liftMaxInLeftSubtreeToTop(currentNode)
        else:

            # Case 2: The node has no left child
            if currentNode.getLeft().isEmpty():
                newChild = currentNode.getRight()

        # Case 3: The node has no right child
            else:
                newChild = currentNode.getLeft()

        # Case 2 & 3: Tie the parent to the new child
            if direction == 'L':
                parent.setLeft(newChild)
            else:
                parent.setRight(newChild)

        # All cases: Reset the root (if it hasn't changed no harm done)
        #            Decrement the collection's size counter
        #            Return the item
        self._tree = preRoot.getLeft()
        self._size -= 1
        return itemRemoved
Beispiel #3
0
from binarytree import BinaryTree

# Create initial leaf nodes
a = BinaryTree("A")
b = BinaryTree("B")
c = BinaryTree("C")
d = BinaryTree("D")
e = BinaryTree("E")
f = BinaryTree("F")
g = BinaryTree("G")

# Build the tree from the bottom up, where
# d is the root node of the entire tree

# Build and set the left subtree of d
b.setLeft(a)
b.setRight(c)
d.setLeft(b)

# Build and set the right subtree of d
f.setLeft(e)
f.setRight(g)
d.setRight(f)

def size(tree):
    if tree.isEmpty():
        return 0
    else:
        return 1 + size(tree.getLeft()) + size(tree.getRight())

def frontier(tree):
    def remove(self, item):
        """Removes and returns item from the tree."""

        # Helper function to adjust placement of an item
        def liftMaxInLeftSubtreeToTop(top):
            # Replace top's datum with the maximum datum in the left subtree
            # Pre:  top has a left child
            # Post: the maximum node in top's left subtree
            #       has been removed
            # Post: top.getRoot() = maximum value in top's left subtree
            parent = top
            currentNode = top.getLeft()
            while not currentNode.getRight().isEmpty():
                parent = currentNode
                currentNode = currentNode.getRight()
            top.setRoot(currentNode.getRoot())
            if parent == top:
                top.setLeft(currentNode.getLeft())
            else:
                parent.setRight(currentNode.getLeft())

        # Begin main part of the method
        if self.isEmpty(): return None
        
        # Attempt to locate the node containing the item
        itemRemoved = None
        preRoot = BinaryTree(None)
        preRoot.setLeft(self._tree)
        parent = preRoot
        direction = 'L'
        currentNode = self._tree
        while not currentNode.isEmpty():
            if currentNode.getRoot() == item:
                itemRemoved = currentNode.getRoot()
                break
            parent = currentNode
            if currentNode.getRoot() > item:
                direction = 'L'
                currentNode = currentNode.getLeft()
            else:
                direction = 'R'
                currentNode = currentNode.getRight()
                
        # Return None if the item is absent
        if itemRemoved == None: return None
        
        # The item is present, so remove its node

        # Case 1: The node has a left and a right child
        #         Replace the node's value with the maximum value in the
        #         left subtree
        #         Delete the maximium node in the left subtree
        if not currentNode.getLeft().isEmpty() \
           and not currentNode.getRight().isEmpty():
            liftMaxInLeftSubtreeToTop(currentNode)
        else:
            
        # Case 2: The node has no left child
            if currentNode.getLeft().isEmpty():
                newChild = currentNode.getRight()
                
        # Case 3: The node has no right child
            else:
                newChild = currentNode.getLeft()
                
        # Case 2 & 3: Tie the parent to the new child
            if direction == 'L':
                parent.setLeft(newChild)
            else:
                parent.setRight(newChild)
            
        # All cases: Reset the root (if it hasn't changed no harm done)
        #            Decrement the collection's size counter
        #            Return the item
        self._tree = preRoot.getLeft()
        self._size -= 1
        return itemRemoved
Beispiel #5
0
from binarytree import BinaryTree

# Create initial leaf nodes
a = BinaryTree("A")
b = BinaryTree("B")
c = BinaryTree("C")
d = BinaryTree("D")
e = BinaryTree("E")
f = BinaryTree("F")
g = BinaryTree("G")

# Build the tree from the bottom up, where
# d is the root node of the entire tree

# Build and set the left subtree of d
b.setLeft(a)
b.setRight(c)
d.setLeft(b)

# Build and set the right subtree of d
f.setLeft(e)
f.setRight(g)
d.setRight(f)


def size(tree):
    if tree.isEmpty():
        return 0
    else:
        return 1 + size(tree.getLeft()) + size(tree.getRight())