Example #1
0
    def test_llists_from_bst(self):
        '''
        Test the llists_from_bst function
        '''

        # Import packages
        from pytools.graphs_trees import trees
        from pytools.linked_lists import linked_lists

        # Init BST
        bst = trees.BinarySearchTree(12, 'brady')
        bst.insert(8, 'oldkobe')
        bst.insert(24, 'newkobe')
        bst.insert(21, 'kg')
        bst.insert(2, 'bigsmooth')
        bst.insert(11, 'bledsoe')
        bst.insert(87, 'gronk')

        # Init linked lists
        llist1 = linked_lists.LinkedList()
        llist1.insert('brady')

        llist2 = linked_lists.LinkedList()
        llist2.insert('oldkobe')
        llist2.insert('newkobe')

        llist3 = linked_lists.LinkedList()
        llist3.insert('kg')
        llist3.insert('gronk')
        llist3.insert('bigsmooth')
        llist3.insert('bledsoe')

        llists2 = [llist1, llist2, llist3]

        # Call function
        llists = utils.llists_from_bst(bst)

        # Verify contents
        for idx, llist in enumerate(llists):
            llist2 = llists2[idx]
            node = llist.head
            node2 = llist2.head
            while node:
                self.assertEqual(node.data, node2.data)
                node = node.next_node
                node2 = node2.next_node
Example #2
0
def llists_from_bst(bst):
    '''
    Create a linked list for all of the nodes at each depth in the
    BinarySearchTree
    '''

    # Import packages
    from pytools.graphs_trees import trees
    from pytools.linked_lists import linked_lists

    # Error checking
    if not bst or not isinstance(bst, trees.BinarySearchTree):
        return []

    # Init variables
    llists = []
    llist = linked_lists.LinkedList()

    # Populate first linked list from root
    llist.insert(bst)
    llists.append(llist)

    # Iterate through llists
    for llist in llists:
        node = llist.head
        llist2 = linked_lists.LinkedList()
        while node:
            if node.data.left_child:
                llist2.insert(node.data.left_child)
            if node.data.right_child:
                llist2.insert(node.data.right_child)
            node = node.next_node
        if not llist2.head:
            break
        llists.append(llist2)

    # Convert linked lists of bst's to values
    for llist in llists:
        node = llist.head
        while node:
            node.data = node.data.value
            node = node.next_node

    return llists
Example #3
0
    def setUp(self):
        '''
        Initialize test case with attributes
        '''

        # Init instance attributes
        # Populate
        # a: 1->3->5->6->None
        a_head = linked_lists.LinkedList()
        a_head.insert(6)
        a_head.insert(5)
        a_head.insert(3)
        a_head.insert(1)

        # b: 2->4->7->None
        b_head = linked_lists.LinkedList()
        b_head.insert(7)
        b_head.insert(4)
        b_head.insert(2)

        self.a_head = a_head
        self.b_head = b_head
Example #4
0
    def setUp(self):
        '''
        Initialize test case with attributes
        '''

        # Import packages
        from pytools.linked_lists import linked_lists
        llist = linked_lists.LinkedList()

        # Init instance attributes
        node_data = ['A', 'B', 'D', 'E', 'F', 'G', 'H', 'I']
        for char in node_data:
            llist.insert(char)

        self.llist = llist
Example #5
0
    def test_create_insert_node(self):
        '''
        Test that a linked list can be created and populated properly via
        the insert function
        '''

        # Init variables
        linked_list = linked_lists.LinkedList()
        linked_list.insert(1)
        linked_list.insert(2)

        # Assert head of linked_list has data
        head_node = linked_list.head
        head_data = head_node.data
        err_msg = 'node_data: %d does not equal head_data: %d' \
                   % (2, head_data)
        self.assertEqual(2, head_data, msg=err_msg)
Example #6
0
    def test_linked_lists_equal(self):
        '''
        Tests if two linked lists are equal
        :return:
        '''

        # Import packages
        from pytools.linked_lists import linked_lists
        # Populate
        # a: 1->3->5->6->None
        a_head = linked_lists.LinkedList()
        a_head.insert(6)
        a_head.insert(5)
        a_head.insert(3)
        a_head.insert(1)

        self.assertTrue(
            linked_lists.linked_lists_equal(a_head.head, self.a_head.head))
Example #7
0
    def test_reverse(self):
        '''
        Test the list can be reversed in-place
        '''

        # Init variables
        linked_list = linked_lists.LinkedList()
        node_data = ['A', 'B', 'C', 'D']
        for char in node_data:
            linked_list.insert(char)

        # Reverse linked list in-place
        linked_list.reverse()

        node = linked_list.head
        for char in node_data:
            self.assertEqual(node.data, char)
            node = node.next_node

        self.assertIsNone(node)
Example #8
0
    def test_find_node(self):
        '''
        Test that the find_node function returns node of interest
        '''

        # Init variables
        linked_list = linked_lists.LinkedList()
        linked_list.insert('a')
        linked_list.insert('b')
        linked_list.insert('c')
        linked_list.insert('a')

        # Find node
        a_node = linked_list.find_node('a')
        self.assertEqual(a_node.data,
                         'a',
                         msg='Found node: %s does not equal "a"' % a_node.data)
        c_node = linked_list.find_node('c')
        self.assertEqual(c_node.data,
                         'c',
                         msg='Found node: %s does not equal "c"' % c_node.data)
Example #9
0
    def test_merge_sorted_lists(self):
        '''
        Test the merging of two sorted link lists into one sorted linked list
        :return:
        '''

        # Import packages
        from pytools.linked_lists import linked_lists

        # c: 1->2->3->4->5->6->7->None
        c_head = linked_lists.LinkedList()
        c_head.insert(7)
        c_head.insert(6)
        c_head.insert(5)
        c_head.insert(4)
        c_head.insert(3)
        c_head.insert(2)
        c_head.insert(1)

        merged = linked_lists.merge_sorted_lists(self.a_head.head,
                                                 self.b_head.head)

        self.assertTrue(linked_lists.linked_lists_equal(c_head.head, merged))