Exemple #1
0
def test_three_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(data=1), Node(data=3), 2)
    arrlinkedResult = [LinkedList(), LinkedList()]
    arrlinkedResult[0].add(2)
    arrlinkedResult[1].add(1)
    arrlinkedResult[1].add(3)

    assert arrlinkedResult == getDepthLists(binaryTree)
Exemple #2
0
def test_five_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(left=Node(data=1), data=2),
                           Node(right=Node(data=5), data=4), 3)
    arrlinkedResult = [LinkedList(), LinkedList(), LinkedList()]
    arrlinkedResult[0].add(3)
    arrlinkedResult[1].add(2)
    arrlinkedResult[1].add(4)
    arrlinkedResult[2].add(1)
    arrlinkedResult[2].add(5)

    assert arrlinkedResult == getDepthLists(binaryTree)
Exemple #3
0
    def test_three_elements(self):
        three_elem = LinkedList()
        three_elem.add(1)
        three_elem.add(2)
        three_elem.add(3)
        node = three_elem.root.next
        deleteMiddle(node)

        result = LinkedList()
        result.add(1)
        result.add(3)

        self.assertEqual(result, three_elem)
Exemple #4
0
def test_seven_nodes():
    binaryTree = BinaryTree()
    binaryTree.root = Node(Node(Node(data=1), Node(data=3), 2),
                           Node(Node(data=5), Node(data=7), 6), 4)

    arrlinkedResult = [LinkedList(), LinkedList(), LinkedList()]
    arrlinkedResult[0].add(4)
    arrlinkedResult[1].add(2)
    arrlinkedResult[1].add(6)
    arrlinkedResult[2].add(1)
    arrlinkedResult[2].add(3)
    arrlinkedResult[2].add(5)
    arrlinkedResult[2].add(7)

    assert arrlinkedResult == getDepthLists(binaryTree)
Exemple #5
0
def test_merge_node_noextraspace():
    ll = LinkedList()
    ll.push(3)
    ll.push(1)
    ll.push(2)

    ll_one = LinkedList()
    ll_one.push(10)
    ll_one.push(20)

    ll_two = LinkedList()
    ll_two.push(5)

    ll_one.get(1).next = ll.head
    ll_two.get(0).next = ll.head
Exemple #6
0
    def test_partition_four_elem(self):
        l1 = LinkedList()
        result = LinkedList()

        result.add(2)
        result.add(1)
        result.add(4)
        result.add(3)

        l1.add(1)
        l1.add(3)
        l1.add(4)
        l1.add(2)

        self.assertEqual(result, partitionList(l1, 3))
Exemple #7
0
    def test_partition_sorted_list(self):
        l1 = LinkedList()
        result = LinkedList()

        result.add(1)
        result.add(4)
        result.add(3)
        result.add(2)

        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(result, partitionList(l1, 2))
Exemple #8
0
    def test_four_elements(self):
        four_elem = LinkedList()
        four_elem.add(1)
        four_elem.add(2)
        four_elem.add(3)
        four_elem.add(4)

        node = four_elem.root.next.next
        deleteMiddle(node)

        result = LinkedList()
        result.add(1)
        result.add(2)
        result.add(4)

        self.assertEqual(result, four_elem)
Exemple #9
0
def test_reorg_one():
    ll = LinkedList()
    ll.push(0)
    ll.push(1)
    ll.push(2)
    ll.push(3)
    ll.push(4)
    ll.push(5)
    ll.push(6)
    ll.push(7)
    ll.push(8)
    ll.push(9)
    ll.push(10)
    ll.push(11)

    print " "

    result = _get_all_data(ll.head)
    print result

    head = reorg(ll.head)
    result = _get_all_data(head)
    print result
    
    assert(result == [0,2,1,5,4,3,9,8,7,6,11,10])
Exemple #10
0
    def test_three_letter(self):
        l1 = LinkedList()
        l1.add('a')
        l1.add('b')
        l1.add('a')

        self.assertEqual(True, isPalindrome(l1, len(l1), "3"))
Exemple #11
0
def test_one_node():
    binaryTree = BinaryTree()
    binaryTree.root = Node(data=1)
    arrlinkedResult = [LinkedList()]
    arrlinkedResult[0].add(1)

    assert arrlinkedResult == getDepthLists(binaryTree)
Exemple #12
0
    def _get_sample_linkedlist(self, size=4):
        """Create a sample linkedlist and return the linkedlist obj."""
        ll = LinkedList()

        for item in range(size):
            ll.push(item)

        return ll
Exemple #13
0
    def test_different_nums(self):
        diff_nums = LinkedList()
        diff_nums.add(3)
        diff_nums.add(4)
        diff_nums.add(2)
        diff_nums.add(1)
        diff_nums.add(7)

        node = diff_nums.root.next.next
        deleteMiddle(node)

        result = LinkedList()
        result.add(3)
        result.add(4)
        result.add(1)
        result.add(7)
        self.assertEqual(result, diff_nums)
Exemple #14
0
def recurDepthLists(node, depth, arr):
    if not node:
        return
    if depth >= len(arr):
        arr.append(LinkedList())
    arr[depth].add(node.data)
    recurDepthLists(node.left, depth + 1, arr)
    recurDepthLists(node.right, depth + 1, arr)
Exemple #15
0
    def test_no_cycle(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(None, findCycleStart(l1))
Exemple #16
0
def test_merge_node():
    ll = LinkedList()
    ll.push(3)
    ll.push(1)
    ll.push(2)

    ll_one = LinkedList()
    ll_one.push(10)
    ll_one.push(20)

    ll_two = LinkedList()
    ll_two.push(5)

    ll_one.get(1).next = ll.head
    ll_two.get(0).next = ll.head

    assert (mergenode_extraspace(ll_one.head, ll_two.head).data == 3)
Exemple #17
0
    def test_four_elements(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        self.assertEqual(2, kFromEnd(l1, 2))
Exemple #18
0
    def test_cycle_immediately(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        l1.root.next.next.next.next = l1.root
Exemple #19
0
    def test_cycle(self):
        l1 = LinkedList()
        l1.add(1)
        l1.add(2)
        l1.add(3)
        l1.add(4)

        l1.root.next.next.next.next = l1.root.next
def test_mergetwolists_one():
    ll1 = LinkedList()
    ll1.push(-10)
    ll1.push(-10)
    ll1.push(-9)
    ll1.push(-4)
    ll1.push(1)
    ll1.push(6)
    ll1.push(6)

    ll2 = LinkedList()
    ll2.push(-7)

    solution = Solution()
    head = solution.mergeTwoLists(ll1.head, ll2.head)

    result = _get_all_data(head)
    assert(result==[-10,-10,-9,-7,-4,1,6,6])
Exemple #21
0
    def test_three_digit(self):
        l1 = LinkedList()
        l2 = LinkedList()

        l1.add(1)
        l1.add(2)
        l1.add(3)

        l2.add(4)
        l2.add(5)
        l2.add(6)

        result = LinkedList()
        result.add(5)
        result.add(7)
        result.add(9)

        self.assertEqual(result, addLists(l1, l2))
Exemple #22
0
    def test_overflow_ones_place(self):
        l1 = LinkedList()
        l2 = LinkedList()

        l1.add(2)
        l1.add(1)
        l1.add(4)

        l2.add(4)
        l2.add(1)
        l2.add(6)

        result = LinkedList()
        result.add(6)
        result.add(3)
        result.add(0)

        self.assertEqual(result, addLists(l1, l2))
Exemple #23
0
    def test_long_short_parameters(self):
        l1 = LinkedList()
        l2 = LinkedList()

        l1.add(1)
        l1.add(0)
        l1.add(0)
        l1.add(0)

        l2.add(1)

        result = LinkedList()
        result.add(1)
        result.add(0)
        result.add(0)
        result.add(1)

        self.assertEqual(result, addLists(l1, l2))
def test_insert_sorted_three():
    ll = LinkedList()
    ll.push(3)
    ll.push(4)
    ll.push(5)
    ll.push(10)

    head = insertsorted(ll.head, 1)

    assert(_get_all_data(head)==[1,3,4,5,10])
def test_insert_sorted_two():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(5)
    ll.push(10)

    head = insertsorted(ll.head, 3)

    assert(_get_all_data(head)==[1,2,3,5,10])
Exemple #26
0
def test_deleteduplicates_one():
    ll = LinkedList()
    ll.push(1)
    ll.push(1)
    ll.push(1)

    head = deleteduplicates(ll.head)
    result = _get_all_data(head)

    assert (result == [1])
Exemple #27
0
def partitionList(list, val):
    lowerList = LinkedList()
    upperList = LinkedList()
    current = list.root
    while current:
        if current.data < val:
            lowerList.insertAtHead(current.data)
        else:
            upperList.insertAtHead(current.data)
        current = current.next

    if lowerList.root:
        current = lowerList.root
        while (current.next):
            current = current.next
        current.next = upperList.root
        return lowerList
    else:
        return upperList
def test_insert_sorted_five():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(3)
    ll.push(4)

    head = insertsorted(ll.head, 10)

    assert(_get_all_data(head)==[1,2,3,4,10])
Exemple #29
0
    def test_overflow_result_longer(self):
        l1 = LinkedList()
        l2 = LinkedList()

        l1.add(9)
        l1.add(1)
        l1.add(1)

        l2.add(1)
        l2.add(9)
        l2.add(9)

        result = LinkedList()
        result.add(1)
        result.add(1)
        result.add(1)
        result.add(0)

        self.assertEqual(result, addLists(l1, l2))
Exemple #30
0
def test_deleteduplicates_two():
    ll = LinkedList()
    ll.push(1)
    ll.push(2)
    ll.push(3)

    head = deleteduplicates(ll.head)
    result = _get_all_data(head)

    assert (result == [1, 2, 3])