Example #1
0
def intersection(llist_1, llist_2):

    #edge cases
    if llist_1.size() == 0 or llist_2.size() == 0:
        return LinkedList()

    # Insert elements of llist_1 into bt1
    bt1 = BinaryTree()
    node = llist_1.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Insert elements of llist_2 into bt2
    bt2 = BinaryTree()
    node = llist_2.head
    while node:
        bt2.insert(node.value)
        tail2 = node
        node = node.next

    #link head of llist1 to the tail of llist2
    tail2.next = llist_1.head
    node = llist_2.head
    bt = BinaryTree()
    while node:
        if bt1.search(node.value) and bt2.search(node.value):
            bt.insert(node.value)
        node = node.next

    flatten(bt.get_root())

    # Empty btree into llist and return
    llist = LinkedList()
    node = bt.get_root()
    while node:
        llist.append(node.value)
        node = node.right
    return llist
Example #2
0
def union(llist_1, llist_2):
    # Insert elements of llist_1 into linked llist_1
    bt1 = BinaryTree()
    node = llist_1.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Fist turn elements of llist_2 into the same btree
    node = llist_2.head
    while node:
        bt1.insert(node.value)
        node = node.next

    # Flatten the binary tree
    flatten(bt1.get_root())

    # Empty btree into llist and return
    llist = LinkedList()
    node = bt1.get_root()
    while node:
        llist.append(node.value)
        node = node.right
    return llist