Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

nikhil nikhilc2710

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