Пример #1
0
class Stack:
    def __init__(self):
        self.__l = SinglyLinkedList()
        self.top = -1

    def push(self, element):
        self.__l.append(element)
        self.top += 1

    def pop(self):
        if self.top == -1:
            raise Exception("Cannot pop from an empty stack")
        self.top -= 1
        return self.__l.pop()

    @property
    def top_item(self):
        if self.top == -1:
            return
        return self.__l.tail

    def __iter__(self):
        for i in self.__l:
            yield i

    def __str__(self):
        return "[" + ", ".join([str(_) for _ in self]) + "]"
Пример #2
0
class MaxBinaryHeap:
    def __init__(self):
        self.values = SinglyLinkedList()

    def swap(self, node_1, node_2):
        node_1.data, node_2.data = node_2.data, node_1.data

    def __bubble_up(self):
        if len(self.values) == 1:
            return
        index = len(self.values) - 1
        parent = (index - 1) // 2
        while (self.values[index].data >
               self.values[parent].data) and index > 0:
            self.swap(self.values[index], self.values[parent])
            index = parent
            parent = (index - 1) // 2

    def insert(self, values):
        for value in values:
            self.values.append(value)
            self.__bubble_up()

    def __sink_down(self):
        if len(self.values) == 1:
            return
        index = 0
        l_child, r_child = 2 * index + 1, 2 * index + 2
        if len(self.values) == 2:
            if self.values[index].data > self.values[l_child].data:
                self.swap(self.values[index], self.values[l_child])

        while (r_child < len(self.values) and l_child < len(self.values)) and (
            (self.values[index].data < self.values[l_child].data) or
            (self.values[index].data < self.values[r_child].data)):
            if self.values[l_child].data > self.values[r_child].data:
                self.swap(self.values[index], self.values[l_child])
                index = l_child
            else:
                self.swap(self.values[index], self.values[r_child])
                index = r_child
            l_child, r_child = 2 * index + 1, 2 * index + 2

    def extract_max(self):
        if self.values.is_empty:
            return
        max_element = self.values.head.data
        self.values.head.data = self.values.tail.data
        self.values.pop()
        self.__sink_down()
        return max_element

    def __str__(self):
        return str(self.values)
def test_append():
    """
    can we insert data at the end?
    """
    nums = SinglyLinkedList()
    nums.push(1)
    nums.push(3)

    nums.append(55)
    expected = 55
    actual = nums.get_at(2)
    assert actual == expected
Пример #4
0
 def breadth_first_search(self, node):
     if node is None:
         return
     queue = Queue()
     visited = SinglyLinkedList()
     queue.enqueue(node)
     while not queue.is_empty:
         node = queue.dequeue().data
         visited.append(node.data)
         if node.l_child:
             queue.enqueue(node.l_child)
         if node.r_child:
             queue.enqueue(node.r_child)
     return visited
Пример #5
0
class Queue:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__l = SinglyLinkedList()
        self.__front = -1
        self.__rear = -1

    @property
    def is_empty(self):
        return self.__front == self.__rear

    @property
    def front(self):
        return self.__front

    @property
    def rear(self):
        return self.__rear

    def enqueue(self, element):
        self.__l.append(element)
        self.__rear += 1

    def dequeue(self):
        self.__front += 1
        if self.is_empty:
            self.__front, self.__rear = -1, -1
        return self.__l.shift()

    def peek(self):
        return self.__l.head

    def __iter__(self):
        for i in self.__l:
            yield i

    def __str__(self):
        return "[" + ", ".join([str(_) for _ in self]) + "]"
Пример #6
0
        curr = self.head
        while len(self) > 1:
            count = 1
            while count != step:
                curr = curr.next
                count += 1
            self.remove_node(curr)
            curr = curr.next

    def is_circular(self, another_list):
        curr = another_list.head
        while curr and curr.next:
            curr = curr.next
            if curr.next == another_list.head:
                return True
        return False


clist = CircularLinkedList()
clist.append(1)
clist.append(2)
clist.append(3)
clist.append(4)

llist = SinglyLinkedList()
llist.append(1)
llist.append(2)
llist.append(3)
llist.append(4)
print(clist.is_circular(llist))