Skip to content

Instantly share code, notes, and snippets.

View nikhilc2710's full-sized avatar
🏠
Working from home

nikhil nikhilc2710

🏠
Working from home
View GitHub Profile
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()
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))
if maze[y][x] == goal:
return True
import collections
def bfs(maze,start):
queue = collections.deque()
queue.append(start)
seen = set([start])
width,height=8,8
#width,height=map(int,input().split()) if inputs are Dynamic
wall, clear, goal = 0, 1, 9
#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)
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
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
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