This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
wall, clear, goal = 0, 1, 9 #change notations according to your usecase | |
width,height=map(int,input().split()) | |
def bfs(maze, start): | |
queue = collections.deque() | |
queue.append(start) | |
seen = set([start]) | |
while queue: | |
path = queue.popleft() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for x2, y2 in ((x+1,y), (x-1,y), (x,y+1), (x,y-1)): | |
if ( 0 <= x2 < width and #X-axis in range | |
0 <= y2 < height and #y-axis in range | |
grid[y2][x2] != wall and #not a wall | |
(x2, y2) not in seen): #not visited | |
queue.append( (x2, y2)) | |
seen.add((x2, y2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if maze[y][x] == goal: | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
def bfs(maze,start): | |
queue = collections.deque() | |
queue.append(start) | |
seen = set([start]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
width,height=8,8 | |
#width,height=map(int,input().split()) if inputs are Dynamic | |
wall, clear, goal = 0, 1, 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Our Tree structure | |
class Node: | |
def __init__(self,value): | |
self.left=None | |
self.data=value | |
self.right=None | |
class Tree: | |
def __init__(self,root): | |
self.root=Node(root) | |
tree=Tree(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def postorder(root,visited): | |
if root: #check if node exits | |
visited=postorder(root.left,traversal) #go to left subtree (L) | |
visited=postorder(root.right,traversal) #go to right subtree (R) | |
visited.append(root.data) #append node value to visted stack (V) | |
return visited #return visited stack | |
preorder(root,[]) #call postorder function with visited stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inorder(root,visited): | |
if root: #check if node exits | |
visited=inorder(root.left,traversal) #go to left subtree (L) | |
visited.append(root.data) #append node value to visted stack (V) | |
visited=inorder(root.right,traversal) #go to right subtree (R) | |
return visited #return visited stack | |
inorder(root,[]) #call inorder function with visited stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def preorder(root,visited): | |
if root: #check if node exits | |
visited.append(root.data) #append node value to visted stack (V) | |
visited=preorder(root.left,traversal) #go to left subtree (L) | |
visited=preorder(root.right,traversal) #go to right subtree (R) | |
return visited #return visited stack | |
preorder(root,[]) #call preorder function with visited stack |