Example #1
0
def get_each_depth(binary_tree):
    linked_lists = []
    current = LinkedList()
    current.insert(binary_tree.root)
    while not current.empty():
        linked_lists.append(current)
        children = LinkedList()
        for node in current:
            if node.left is not None:
                children.insert(node.left)
            if node.right is not None:
                children.insert(node.right)
        current = children
    return linked_lists
    def setUp(self):
        # Set up some Elements
        self.e1 = LinkedListElement(1)
        self.e2 = LinkedListElement(2)
        self.e3 = LinkedListElement(3)
        self.e4 = LinkedListElement(4)

        # Start setting up a LinkedList
        self.ll = LinkedList(self.e1)
        return 
Example #3
0
def all_paths_from(root, value):
    if root is None:
        return []

    left_paths = all_paths_from(root.left, value - root.value)
    for path in left_paths:
        path.insert(Node(root.key, root.value))
    right_paths = all_paths_from(root.right, value - root.value)
    for path in right_paths:
        path.insert(Node(root.key, root.value))

    if root.value == value:
        path = LinkedList()
        path.insert(Node(root.key, root.value))
        return left_paths + right_paths + [path]

    return left_paths + right_paths
Example #4
0
                slow = slow.next
            return head
    # No loop.
    return None


def find_loop_start2(linked_list):
    """Given a corrupt circular linked list returns the start of the loop.
    Probably not allowed to use Python set.
    """
    seen = set()
    current = linked_list.head
    while current is not None:
        if current in seen:
            return current
        seen.add(current)
        current = current.next
    return None


linked_list = LinkedList()
for i in xrange(10):
    linked_list.insert(Node(i))
# Set up a loop.
x = linked_list.search(0)
y = linked_list.search(5)
x.next = y
y.prev = x

print find_loop_start(linked_list).key
Example #5
0
 def __init__(self, key):
     self.key = key
     self.neighbours = LinkedList()
     self.discovered = False
Example #6
0
 def __init__(self):
     self.V = LinkedList()
Example #7
0
def sumLists(firstList, secondList):
    return LinkedList(sumListsHelper(firstList.head, secondList.head, 0))
Example #8
0
    while (i < position and cur is not None):
        i += 1
        cur = cur.next

    return cur.value


e1 = LinkedListElement(1)
e2 = LinkedListElement(2)
e3 = LinkedListElement(3)
e4 = LinkedListElement(4)
e5 = LinkedListElement(5)
e6 = LinkedListElement(6)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)
ll.append(e6)

print("Test K To Last")
print(getLength(e1))
print(kToLast(ll, 0))
print(kToLast(ll, 1))
print(kToLast(ll, 2))
print(kToLast(ll, 3))
print(kToLast(ll, 4))
print(kToLast(ll, 5))
Example #9
0
def size(linked_list):
    size = 0
    x = linked_list.head
    while x is not None:
        size += 1
        x = x.next
    return size


def check_palindrome2(linked_list):
    """Relies on doubly linked list."""
    x = linked_list.head
    while x.next is not None:
        x = x.next
    y = linked_list.head
    while y is not None:
        if x.key != y.key:
            return False
        x = x.prev
        y = y.next
    return True


a = LinkedList()
for i in xrange(9):
    a.insert(Node(i))
for i in reversed(xrange(9)):
    a.insert(Node(i))
a.traverse()
print check_palindrome_recursive(a)