Binärbaumsuche ergänzt
This commit is contained in:
parent
7b8c458b63
commit
6ae7a34f1a
117
tk_binsearch/tk_binsearch.py
Normal file
117
tk_binsearch/tk_binsearch.py
Normal file
@ -0,0 +1,117 @@
|
||||
import tkinter as tk
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
TESTLIST = ["Apfel", "Birne", "Cassis", "Dattel", "Erdbeere",
|
||||
"Felsenbirne", "Granatapfel", "Heidelbeere", "Immergrün",
|
||||
"Johannisbeere", "Kamille", "Lauch", "Meerrettich",
|
||||
"Nahrung", "Obst", "Pastinake", "Quellwasser", "Rettich",
|
||||
"Salat", "Tomate", "Unkraut", "Vogelbeere", "Walderdbeere",
|
||||
"Xylit", "Ysop", "Zwetschge"]
|
||||
|
||||
def binsearch(list_for_search, keyword, depth:int=0):
|
||||
l = len(list_for_search)
|
||||
print("BS Tiefe {}, Restlänge {}: {}".format(depth,l,list_for_search))
|
||||
|
||||
if l == 1 and list_for_search[0] == keyword:
|
||||
return {"result": 1, "word": list_for_search[0]}
|
||||
elif l > 1:
|
||||
w = list_for_search[l // 2]
|
||||
if keyword < w :
|
||||
newlist = list_for_search[: l // 2]
|
||||
return binsearch(newlist, keyword, depth+1)
|
||||
else:
|
||||
newlist = list_for_search[l // 2:]
|
||||
return binsearch(newlist, keyword, depth+1)
|
||||
else:
|
||||
return {"result": 0, "word": "not found"}
|
||||
|
||||
class Controller(object):
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
self.list_for_search = TESTLIST
|
||||
self.keyword = "Nothing"
|
||||
|
||||
def fill_list(self):
|
||||
self.app.fill_input_list(self.list_for_search)
|
||||
|
||||
def fill_entry(self, text: str = None):
|
||||
if text is None:
|
||||
text = "Apfel"
|
||||
self.keyword = text
|
||||
self.app.fill_entry(text)
|
||||
|
||||
def run_search(self):
|
||||
print("Run")
|
||||
self.keyword = self.app.get_keyword_entry()
|
||||
result = binsearch(self.list_for_search, self.keyword)
|
||||
if result["result"] == 1:
|
||||
# success
|
||||
print("word found")
|
||||
print("The word is: {}".format(result["word"]))
|
||||
self.app.set_canvas_color("green")
|
||||
return 1
|
||||
else:
|
||||
# failure
|
||||
print("word not found.")
|
||||
self.app.set_canvas_color("red")
|
||||
return -1
|
||||
return 0
|
||||
|
||||
|
||||
class Application(tk.Tk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.con = Controller(self)
|
||||
self.create_control_variables()
|
||||
self.create_widgets()
|
||||
self.con.fill_list()
|
||||
self.con.fill_entry()
|
||||
|
||||
def create_control_variables(self):
|
||||
self.e_search_var = tk.StringVar()
|
||||
|
||||
def fill_input_list(self, list_for_display: list = None):
|
||||
if list_for_display is None:
|
||||
list_for_display = [1,2,3,4,5]
|
||||
else:
|
||||
pass
|
||||
for e in list_for_display:
|
||||
self.lb_input.insert(tk.END, e)
|
||||
|
||||
def fill_entry(self, text):
|
||||
self.e_search_var.set(text)
|
||||
|
||||
def get_keyword_entry(self):
|
||||
return self.e_search_var.get()
|
||||
|
||||
def set_canvas_color(self, color:str=None):
|
||||
if color is None:
|
||||
color = "black"
|
||||
self.c.configure(background=color)
|
||||
|
||||
def create_widgets(self):
|
||||
self.lf_input = ttk.Labelframe(master=self, labelanchor="n", text="Eingabe")
|
||||
self.lf_input.grid(row=0, column=0, columnspan=2)
|
||||
|
||||
self.lb_input = tk.Listbox(master=self.lf_input)
|
||||
self.lb_input.grid(row=0, column=0)
|
||||
|
||||
self.lf_search = ttk.Labelframe(master=self, labelanchor="n", text="Suche")
|
||||
self.lf_search.grid(row=1, column=0)
|
||||
|
||||
self.e_search = ttk.Entry(master=self.lf_search, textvariable=self.e_search_var)
|
||||
self.e_search.grid(row=0, column=0)
|
||||
|
||||
self.b = ttk.Button(master=self, text="Suchen", command=self.con.run_search)
|
||||
self.b.grid(row=1, column=1)
|
||||
|
||||
self.c = tk.Canvas(master=self, background="black",width=150, height=30)
|
||||
self.c.grid(row=2, column=0, columnspan=2)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Execution
|
||||
# --------------------------
|
||||
if __name__ == "__main__":
|
||||
app = Application()
|
||||
app.mainloop()
|
Loading…
Reference in New Issue
Block a user