Exemple #1
0
 def move_down(self, treap):
     animation_handler = ah.AnimationHandler()
     tmp_graph = tr.TreapGraph(treap)
     if self.left_node.priority > self.right_node.priority:
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 6)
         self.left_node.color = "orange"
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 7)
         self.left_node.rotate_right(treap)
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 8)
     elif self.right_node.priority > self.left_node.priority:
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 9)
         self.right_node.color = "orange"
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 10)
         self.right_node.rotate_left(treap)
         animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 11)
Exemple #2
0
    def find(self, key, treap):
        animation_handler = ah.AnimationHandler()
        tmp_graph = tr.TreapGraph(treap)
        if self.key is None:
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 0)
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 8)
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 9)
            return self
        if self.parent_node is None:
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 0)
        if self.key == key:
            tmp = self
            tmp.color = "salmon"
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 2)
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 3)
            return tmp
        elif key <= self.key:
            if self.left_node:
                self.color = "dimgrey"
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 4)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 5)
                tmp = self.left_node.find(key, treap)
            else:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 8)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 9)
                return self
        else:
            if self.right_node:
                self.color = "dimgrey"
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 6)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 7)
                tmp = self.right_node.find(key, treap)
            else:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 8)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_search.txt", 9)
                return self

        return tmp
Exemple #3
0
    def rotate_right(self, treap):
        animation_handler = ah.AnimationHandler()
        tmp_graph = tr.TreapGraph(treap)
        self.color = "salmon"
        if self.parent_node:
            self.parent_node.color = "orange"
        if self.right_node:
            self.right_node.color = "peachpuff"
        animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_rotate_right.txt", 0)

        if self.parent_node.parent_node is not None:
            if self.parent_node == self.parent_node.parent_node.left_node:
                self.parent_node.parent_node.left_node = self
            elif self.parent_node == self.parent_node.parent_node.right_node:  # Parent ist right child
                self.parent_node.parent_node.right_node = self

        if self.right_node is not None:
            self.right_node.parent_node = self.parent_node

        self.parent_node.left_node = self.right_node
        self.right_node = self.parent_node
        self.parent_node = self.parent_node.parent_node
        self.right_node.parent_node = self

        if self.parent_node is None:
            treap.root = self

        animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_rotate_right.txt", 0)
        self.right_node.color = "palegreen"
        animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_rotate_right.txt", 0)
        if self.parent_node is not None and self.priority > self.parent_node.priority:
            if self.parent_node.left_node == self:
                self.rotate_right(treap)
            else:
                self.rotate_left(treap)
        # self.color = "palegreen"
        self.clear_colors()
        return self
Exemple #4
0
    def delete(self, value):

        # Checking if the value can be deleted
        if not self.search(value):
            return
        animation_handler = ah.AnimationHandler()
        self.clear_colors()
        compare_color = "orange"
        current_color = "salmon"
        path_color = "dimgrey"
        self.number_of_elements -= 1
        search_level = self.max_level - 1  # Array starts at 0
        tmp = self.root
        tmp_graph = sl.SkipListGraph()
        animation_handler.insert_first(tmp_graph.create_graph_delete(self),
                                       "skip_list",
                                       "./res/skip_list_delete.txt", 0)

        while search_level >= 0:
            tmp.colors[
                search_level] = current_color  # Set the starting node ( top left corner ) to salmon
            animation_handler.push(tmp_graph.create_graph_delete(self),
                                   "skip_list", "./res/skip_list_delete.txt",
                                   11)
            while value > tmp.value:
                tmp.list[search_level].colors[
                    search_level] = compare_color  # Set the next skip node color
                animation_handler.push(tmp_graph.create_graph_delete(self),
                                       "skip_list",
                                       "./res/skip_list_delete.txt", 5)

                if value == tmp.list[search_level].value:
                    tmp.list[search_level].colors[
                        search_level] = "peachpuff"  # Node to be deleted has been found
                    animation_handler.push(tmp_graph.create_graph_delete(self),
                                           "skip_list",
                                           "./res/skip_list_delete.txt", 6)
                    tmp.colors[search_level] = path_color
                    if search_level == 0:
                        animation_handler.push(
                            tmp_graph.create_graph_delete(self), "skip_list",
                            "./res/skip_list_delete.txt", 7)
                        animation_handler.push(
                            tmp_graph.create_graph_delete(self), "skip_list",
                            "./res/skip_list_delete.txt", 8)
                        tmp.list[search_level] = tmp.list[search_level].list[
                            search_level]  # Overwritten the old pointer
                        animation_handler.push(
                            tmp_graph.create_graph_delete(self), "skip_list",
                            "./res/skip_list_delete.txt", 9)
                        self.clear_colors()
                        self.delete_position_list(value)
                        animation_handler.push(
                            tmp_graph.create_graph_delete(self), "skip_list",
                            "./res/skip_list_delete.txt", 0)
                        return 0
                    """tmp.list[search_level].height -= 1
                    tmp.list[search_level].list.pop() #Consider this"""
                    tmp.list[search_level] = tmp.list[search_level].list[
                        search_level]  #Overwritten the old pointer
                    animation_handler.push(tmp_graph.create_graph_delete(self),
                                           "skip_list",
                                           "./res/skip_list_delete.txt", 7)
                    break
#                elif math.inf == tmp.list[search_level].value:
                elif tmp.list[search_level].value > value:

                    tmp.colors[search_level] = path_color
                    tmp.list[search_level].colors[search_level] = "palegreen"
                    break

                tmp.colors[
                    search_level] = path_color  # And the last one back to the standard color
                tmp = tmp.list[search_level]
                tmp.colors[
                    search_level] = current_color  # And the last one back to the standard color
                animation_handler.push(tmp_graph.create_graph_delete(self),
                                       "skip_list",
                                       "./res/skip_list_delete.txt", 10)

            search_level -= 1
        return 0
Exemple #5
0
    def insert(self, value):
        if self.search(value):
            return
        self.insert_position_list(value)
        self.clear_colors()
        compare_color = "orange"
        current_color = "salmon"
        path_color = "dimgrey"
        animation_handler = ah.AnimationHandler()
        tmp_graph = sl.SkipListGraph()
        self.number_of_elements += 1  # Increasing the Skip Node count
        self.max_level = self.recalculate_max_level(
        )  # Recalculating the new max level
        skip_node = SkipNode(value, self.max_level)
        search_level = skip_node.height - 1  # We cannot insert it above its height
        tmp = self.root  # Using tmp to find the right spot to insert
        tmp.colors[
            search_level] = current_color  # Set the starting node ( top left corner ) to salmon
        animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                               "./res/skip_list_insert.txt", 4)
        while search_level >= 0:
            tmp.colors[
                search_level] = current_color  # Set the starting node ( top left corner ) to salmon
            animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                                   "./res/skip_list_insert.txt", 5)
            while value > tmp.list[
                    search_level].value:  # Go right until overshoot
                tmp.list[search_level].colors[
                    search_level] = compare_color  # Set the next skip node color
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_insert.txt", 6)
                tmp.colors[
                    search_level] = path_color  # Set the starting node ( top left corner ) to salmon
                tmp = tmp.list[search_level]
                tmp.colors[
                    search_level] = current_color  # Set the starting node ( top left corner ) to salmon
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_insert.txt", 7)
            animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                                   "./res/skip_list_insert.txt", 8)
            skip_node.list[search_level] = tmp.list[search_level]
            tmp.colors[search_level] = path_color
            tmp.list[
                search_level] = skip_node  # TODO Doing the necessary pointer stuff
            skip_node.colors[
                search_level] = "peachpuff"  # Set the starting node ( top left corner ) to salmon
            animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                                   "./res/skip_list_insert.txt", 9)
            #tmp.list[search_level] = skip_node  # Doing the necessary pointer stuff
            if search_level > 0:
                tmp.colors[
                    search_level -
                    1] = current_color  # Set the starting node ( top left corner ) to salmon
            search_level -= 1
            if search_level < 0:
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_insert.txt", 0)
                return
            animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                                   "./res/skip_list_insert.txt", 10)

        self.clear_colors()
        animation_handler.insert_first(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_insert.txt", 0)
        return
Exemple #6
0
    def find(self, value):
        self.clear_colors()
        compare_color = "orange"
        current_color = "salmon"
        path_color = "dimgrey"
        animation_handler = ah.AnimationHandler()
        search_level = self.max_level - 1  # Array starts at 0
        tmp = self.root  # Tmp keeps track of current list
        tmp_graph = sl.SkipListGraph()
        tmp.colors[
            search_level] = current_color  # Set the starting node ( top left corner ) to salmon
        animation_handler.push(tmp_graph.create_graph(self), "skip_list",
                               "./res/skip_list_search.txt", 2)

        while True:  #until value is either found or not
            while value >= tmp.list[
                    search_level].value:  # The next skip node is smaller or equal to value
                tmp.list[search_level].colors[
                    search_level] = compare_color  # Set the next skip node color
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_search.txt", 4)
                tmp.list[search_level].colors[
                    search_level] = current_color  # Set the current node to salmon
                tmp.colors[
                    search_level] = path_color  # And the last one back to the standard color
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_search.txt", 5)
                tmp = tmp.list[search_level]  # Go right until overshoot
            if search_level == 0:  # At this point we should have found the value
                if value == tmp.value:
                    tmp.colors[search_level] = "peachpuff"
                    animation_handler.push(tmp_graph.create_graph(self),
                                           "skip_list",
                                           "./res/skip_list_search.txt", 0)
                    return animation_handler.get_instance(
                    ).skip_list_graph_list
                else:
                    tmp.list[search_level].colors[
                        search_level] = compare_color  # Set the next skip node color
                    animation_handler.push(tmp_graph.create_graph(self),
                                           "skip_list",
                                           "./res/skip_list_search.txt", 9)
                    tmp.list[search_level].colors[
                        search_level] = current_color  # Set the current node to salmon
                    tmp.colors[
                        search_level] = path_color  # And the last one back to the standard color
                    animation_handler.push(tmp_graph.create_graph(self),
                                           "skip_list",
                                           "./res/skip_list_search.txt", 10)
                    return 0
            else:  # The case in which were going down a level
                tmp.list[search_level].colors[
                    search_level] = compare_color  # Set the next skip node color
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_search.txt", 11)
                tmp.list[search_level].colors[
                    search_level] = "palegreen"  # If a node was compared but is not used in the path
                tmp.colors[search_level] = path_color
                search_level -= 1
                tmp.colors[
                    search_level] = current_color  # Set the current node to salmon
                animation_handler.push(tmp_graph.create_graph(self),
                                       "skip_list",
                                       "./res/skip_list_search.txt", 4)
Exemple #7
0
    # Frame that contains every button
    animation_frame = tk.Frame(master=button_frame, **style_sheet["animation_frame"])

    data_structure_frame = tk.Frame(master=button_frame, bg=background_color)
    key_structure_frame = tk.Frame(master=data_structure_frame, bg=background_color)
    operator_frame = tk.Frame(master=data_structure_frame, bg=background_color)

    graph_structure_frame = tk.Frame(master=button_frame, bg=background_color)
    switch_algorithm_frame = tk.Frame(master=graph_structure_frame, bg=background_color)
    graph_operation_frame = tk.Frame(master=graph_structure_frame, bg=background_color)
    # Pseudocode canvas
    pseudocode_frame = tk.Frame(master=interface_frame, bg="#2b2b2b", highlightthickness=5,
                                highlightbackground=background_color)

    animation_handler = ah.AnimationHandler(pseudocode_frame)
    skip_list = sl.SkipList()
    skip_list_graph = SkipListGraph(skip_list)

    # generating Pseudocode Obj
    pseudocode_obj = pw.PseudocodeWidget(pseudocode_frame)

    skip_list_graph.draw(skip_list, plot, canvas)

    # Dropdown menu for choosing the algorithm
    algorithms = ["Skip List", "Treap"]
    algorithm = tk.StringVar(root)
    algorithm.set(algorithms[0])  # default value

    # Buttons
Exemple #8
0
    def insert(self, key, treap, parent=None):
        global log_message
        animation_handler = ah.AnimationHandler()
        tmp_graph = tr.TreapGraph(treap)
        tmp = self

        if self.parent_node is None:
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 0)

        if key == self.key:
            self.color = "salmon"
            log_widget = log.LogWidget()
            log_widget.push(f"{key} ALREADY IN TREAP")
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 0)
            return

        if self.key is None:
            self.color = "salmon"
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 2)
            self.key = key
            self.color = "palegreen"
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 3)

        elif key <= self.key:
            self.color = "dimgrey"
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 4)
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 5)

            if not self.left_node:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 2)
                self.left_node = Node(key, self)
                self.left_node.color = "salmon"
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 3)
                self.left_node.color = "palegreen"
                if self.left_node.priority > self.priority:
                    animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 6)
                    tmp = self.left_node.rotate_right(treap)
                self.default_color()
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 0)
            else:
                self.left_node.insert(key, treap, self)

        else:
            self.color = "dimgrey"
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 8)
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 9)

            if not self.right_node:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 2)
                self.right_node = Node(key, self)
                self.right_node.color = "salmon"
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 3)
                self.right_node.color = "palegreen"
                if self.right_node.priority > self.priority:
                    animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 8)
                    tmp = self.right_node.rotate_left(treap)
                self.default_color()
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_insert.txt", 0)
            else:
                self.right_node.insert(key, treap, self)
        while tmp.parent_node:
            tmp = tmp.parent_node
        tmp.default_color()
Exemple #9
0
    def delete(self, key, treap):
        animation_handler = ah.AnimationHandler()
        tmp_graph = tr.TreapGraph(treap)
        animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 0)
        animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 2)
        if self.find2(key) is False:
            return
        tmp = self.find_ohne(key)
        tmp.color = "salmon"

        # Case 1 : node to be deleted is a Leaf
        if tmp.left_node is None and tmp.right_node is None:
            # Node to be deleted is not Root
            if tmp.parent_node is not None:
                # Node is left-Node from Parents perspective
                tmp.color = "salmon"
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 4)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 5)

                if tmp.parent_node.left_node == tmp:
                    tmp.parent_node.left_node = None
                    tmp.color = "palegreen"
                    tmp.default_color()
                    tmp.clear_colors()
                    animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 0)

                    return
                # Node is right_node from Parents perspective
                else:
                    tmp.parent_node.right_node = None
                    animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 5)
                    tmp.default_color()
                    tmp.clear_colors()
                    animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 0)
                    return
            # Node is Root
            else:
                tmp.key = None
                tmp.priority = random.randint(1, 1001)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 5)
                tmp.default_color()
                tmp.clear_colors()
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 0)
                return
        else:
            tmp.priority = -math.inf
            animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 3)
            if tmp.left_node is None:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 9)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 10)
                tmp.right_node.rotate_left(treap)
                tmp.delete(key, treap)
                return
            elif tmp.right_node is None:
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 6)
                animation_handler.push(tmp_graph.create_graph(), "treap", "./res/treap_delete.txt", 7)
                tmp.left_node.rotate_right(treap)
                tmp.delete(key, treap)
                return
            else:
                tmp.move_down(treap)
                tmp.delete(key, treap)
                return
        return