def __repr__(self): level = 0 q = Queue() visit_order = list() node = self.get_root() q.enq( (node,level) ) while(len(q) > 0): node, level = q.deq() if node == None: visit_order.append( ("<empty>", level)) continue visit_order.append( (node, level) ) if node.has_left_child(): q.enq( (node.get_left_child(), level +1 )) else: q.enq( (None, level +1) ) if node.has_right_child(): q.enq( (node.get_right_child(), level +1 )) else: q.enq( (None, level +1) ) string = "Tree\n" previous_level = -1 for i in range(len(visit_order)): node, level = visit_order[i] if level == previous_level: string += " | " + str(node) else: string += "\n" + str(node) previous_level = level return string
def __repr__(self): q = Queue() root = self.get_root() q.enq(root) res = str(root) + "\n" level_node_count = 2 current_count = 0 while q: node = q.deq() if node.has_left_child(): q.enq(node.get_left_child()) if current_count != 0: if current_count == level_node_count - 1: res += " " + str(node.get_left_child()) else: res += " " + str(node.get_left_child()) + " | " else: res += str(node.get_left_child()) + " | " else: if current_count != 0: if current_count == level_node_count - 1: res += " <empty>" else: res += " <empty> | " else: res += "<empty> | " current_count += 1 if node.has_right_child(): q.enq(node.get_right_child()) if current_count != 0: if current_count == level_node_count - 1: res += " " + str(node.get_right_child()) else: res += " " + str(node.get_right_child()) + " | " else: res += str(node.get_right_child()) + " | " else: if current_count != 0: if current_count == level_node_count - 1: res += " <empty>" else: res += " <empty> | " else: res += "<empty> | " current_count += 1 if current_count == level_node_count: level_node_count *= 2 current_count = 0 res += "\n" return res
def bfs(self): visit_order = list() q = Queue() node = self.get_root() q.enq(node) while len(q) > 0: node = q.deq() visit_order.append(node) if node.has_left_child(): q.enq(node.get_left_child()) if node.has_right_child(): q.enq(node.get_right_child()) return visit_order
def bfs(tree): q = Queue() root = tree.get_root() q.enq(root) visit_order = [] while q: node = q.deq() visit_order.append(node.value) if node.has_left_child(): q.enq(node.get_left_child()) if node.has_right_child(): q.enq(node.get_right_child()) print(visit_order)
y = b liste.append([x,y]) if durum == 3: y = b + 1 x = a liste.append([x,y]) x, y = 0, 0 durum += 1 return liste mat = [[12,34,52,99,23], [12,1,14,21,23], [12,18,22,23,19], [16,28,24,44,23], [8,12,23,46,34]] x ,y = 2,2 noktalar.enq([x,y]) gezilen =[] while not noktalar.isEmpty() and y<3: nokta = noktalar.deq() x, y = nokta[0], nokta[1] komsular = komsuBul(x,y) for i in komsular: if mat[x][y] - 5 <= mat[i[0]][i[1]] <= mat[x][y] + 5 : if [i[0], i[1]] not in gezilen: gezilen.append([i[0], i[1]]) noktalar.enq([i[0], i[1]]) for i in gezilen: mat[i[0]][i[1]]= 0
self.q = deque() def enq(self, value): self.q.appendleft(value) def deq(self): if len(self.q) > 0: return self.q.pop() else: return None def __len__(self): return len(self.q) def __repr__(self): if len(self.q) > 0: s = "<enqueue here>\n_____________________\n" s += "\n_____________________\n".join( [str(item) for item in self.q]) s += "\n_____________________\n<dequeue here>" return s else: return "<queue is empty>" q = Queue() q.enq("apple") q.enq("banana") q.enq("cherry") print(q)
class Simulator: # time quantum, processes def __init__(self, tq=2): self.quantum = tq self.procs = [] self.clock = 0 self.queue = Queue() self.events = [] self.timer = 0 # time quantum def quantize(self, tq=2): self.quantum = tq # create procces and add to list def process(self, pname, pat, pbt): self.procs.append(Process(pname, pat, pbt)) # create process from csv ';' file and add to list def load(self, path): with open(path) as file: content = file.read() lines = content.split('\n') for line in lines: if line.strip(): data = line.strip().split(';') n = data[0].strip() at = int(data[1].strip()) bt = int(data[2].strip()) self.process(n, at, bt) # listen for arriving def catch(self): temp = [] # to store enqueued processed to be removed from list for proc in self.procs: if proc.arrive == self.clock: self.queue.enq(proc) temp.append(proc) for proc in temp: self.procs.remove(proc) def run(self): proc = self.queue.deq() complete = False if proc.burst > self.quantum: time = self.quantum else: time = proc.burst complete = True event = Event(proc.name, self.timer, self.timer + time) self.events.append(event) self.timer += time if not complete: proc.burst -= time self.queue.enq(proc) def schedule(self): while self.clock < MAX_CLOCK: self.catch() if not self.queue.empty(): self.run() self.clock += 1