def traverseBFS(self): keys = [] bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: #print("cur.key = %s"%cur.key) keys.append(cur.key) #print("%s added to keys" % str(cur.key)) if (cur.left): bfsQ.enQueue(cur.left) #print('cur.left.key = %s' % cur.left.key) if (cur.right): bfsQ.enQueue(cur.right) #print("cur.right.key = %s"%cur.right.key) cur = bfsQ.deQueue() #print("--------------------------------") return keys
def traverseBFS(self): string = '' bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += ('(%s)<-' % cur.parent.key if cur.parent else '(None)<-') string += str(cur.key) string += ('->(%s,' % cur.left.key if cur.left else '->(None,') string += ('%s)\n' % cur.right.key if cur.right else 'None)\n') if (cur.left): bfsQ.enQueue(cur.left) if (cur.right): bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == '': string = '(Empty)' return string
def traverseBFS(self): keys = [] bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: if (cur != self.root): keys.append(cur.key) for node in cur.next: bfsQ.enQueue(node) cur = bfsQ.deQueue() return ', '.join([str(key) for key in keys])
def traverseBFS(self): string = '' bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += ('\n(%s)\n' % cur.parent.key if cur.parent else '\n(None)\n') string += ('<%s>' % str(cur.key)) string += ('(BF: %s)' % (cur.BF)) string += ('\n(%s,' % cur.left.key if cur.left else '\n(None,') string += ('%s)\n____' % cur.right.key if cur.right else 'None)\n____') if (cur.left): bfsQ.enQueue(cur.left) if (cur.right): bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == '': string = '(Empty)' return string
def traverseBFS(self): string = '' bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += ('(%s)<-'%cur.parent.key if cur.parent else '(None)<-') string += str(cur.key) string += ('->(%s,'%cur.left.key if cur.left else '->(None,') string += ('%s)\n'%cur.right.key if cur.right else 'None)\n') if(cur.left): bfsQ.enQueue(cur.left) if(cur.right): bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == '': string = '(Empty)' return string
def traverseBFS(self): keys = [] bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: #print("cur.key = %s"%cur.key) keys.append(cur.key) #print("%s added to keys" % str(cur.key)) if(cur.left): bfsQ.enQueue(cur.left) #print('cur.left.key = %s' % cur.left.key) if(cur.right): bfsQ.enQueue(cur.right) #print("cur.right.key = %s"%cur.right.key) cur = bfsQ.deQueue() #print("--------------------------------") return keys
def outputTesting(self): string = "" bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += "(%s)<-" % cur.parent.key if cur.parent else "(None)<-" string += str(cur.key) string += "(BF:%s)" % (cur.BF) string += "->(%s," % cur.left.key if cur.left else "->(None," string += "%s)\n" % cur.right.key if cur.right else "None)\n" if cur.left: bfsQ.enQueue(cur.left) if cur.right: bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == "": string = "(Empty)" return string
def traverseBFS(self): string = "" bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += "\n(%s)\n" % cur.parent.key if cur.parent else "\n(None)\n" string += "<%s>" % str(cur.key) string += "(BF: %s)" % (cur.BF) string += "\n(%s," % cur.left.key if cur.left else "\n(None," string += "%s)\n____" % cur.right.key if cur.right else "None)\n____" if cur.left: bfsQ.enQueue(cur.left) if cur.right: bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == "": string = "(Empty)" return string
def traverseBFS(self): keys = [] bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: if(cur != self.root): keys.append(cur.key) for node in cur.next: bfsQ.enQueue(node) cur = bfsQ.deQueue() return ', '.join([str(key) for key in keys])
def outputTesting(self): string = '' bfsQ = Queue() bfsQ.enQueue(self.root) cur = bfsQ.deQueue() while cur: string += ('(%s%s)<-'%(cur.parent.key,'R' if cur.parent.color else 'B') if cur.parent else '(NoneB)<-') string += str(cur.key) string += ('%s'%'R' if cur.color else 'B') string += ('->(%s%s,'%(cur.left.key,'R' if cur.left.color else 'B') if cur.left else '->(NoneB,') string += ('%s%s)\n'%(cur.right.key,'R' if cur.right.color else 'B') if cur.right else 'NoneB)\n') if(cur.left): bfsQ.enQueue(cur.left) if(cur.right): bfsQ.enQueue(cur.right) cur = bfsQ.deQueue() if string == '': string = '(Empty)' return string
def traverseBFS(self,source): bfsQ = Queue() distanceTo = {} if not source in self.vertexList: print('Vertex %s does not exist'%source) return False bfsQ.enQueue(source) cur = bfsQ.deQueue() distanceTo[cur] = 0 while cur!=None: #Used to index into the adjacency matrix index = self.vertexList[cur] destIndex = 0 for edge in self.matrix[index]: if edge: for key,value in self.vertexList.items(): if value == destIndex: #If vertex is undiscovered if not key in distanceTo: bfsQ.enQueue(key) distanceTo[key] = distanceTo[cur]+1 destIndex+=1 cur = bfsQ.deQueue() return distanceTo
def traverseBFS(self, source): path = '' for key, vertex in self.vertexList.items(): vertex.visit = None vertex.dist = float('inf') vertex.predecessor = None self.vertexList[source].visit = False self.vertexList[source].dist = 0 self.vertexList[source].predecessor = None bfsQ = Queue() bfsQ.enQueue(self.vertexList[source]) curVertex = bfsQ.deQueue() while curVertex: for vertex in curVertex.next: if vertex.visit == None: vertex.visit = False vertex.dist = curVertex.dist + 1 vertex.predecessor = curVertex bfsQ.enQueue(vertex) curVertex.visit = True path += ('Vertex %s, Distance: %s\n' % (curVertex.name, curVertex.dist)) curVertex = bfsQ.deQueue() return path
def traverseBFS(self,source): path = '' for key, vertex in self.vertexList.items(): vertex.visit = None vertex.dist = float('inf') vertex.predecessor = None self.vertexList[source].visit = False self.vertexList[source].dist = 0 self.vertexList[source].predecessor = None bfsQ = Queue() bfsQ.enQueue(self.vertexList[source]) curVertex = bfsQ.deQueue() while curVertex: for vertex in curVertex.next: if vertex.visit == None: vertex.visit = False vertex.dist = curVertex.dist+1 vertex.predecessor = curVertex bfsQ.enQueue(vertex) curVertex.visit = True path+= ('Vertex %s, Distance: %s\n'%(curVertex.name,curVertex.dist)) curVertex = bfsQ.deQueue() return path