-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added scripts for tree feature showcase.
- Loading branch information
1 parent
66adb5a
commit b7668b0
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from traverseCraft.world import CreateTreeWorld | ||
from traverseCraft.agent import TreeAgent | ||
from collections import deque | ||
|
||
def levelOrderTraversal(rootId: int, agent: TreeAgent, world: CreateTreeWorld): | ||
if rootId is None: | ||
return [] | ||
|
||
queue = deque([rootId]) | ||
result = [] | ||
|
||
while queue: | ||
level_size = len(queue) | ||
current_level = [] | ||
|
||
for _ in range(level_size): | ||
node_id = queue.popleft() | ||
agent.moveAgent(node_id, delay=0.5) | ||
current_level.append(node_id) | ||
|
||
for child in world.nodeMap[node_id].children: | ||
queue.append(child.id) | ||
|
||
result.append(current_level) | ||
|
||
return result | ||
|
||
if __name__ == "__main__": | ||
treeInfo = { | ||
'adj': { | ||
1: [2, 3], | ||
2: [4, 5], | ||
3: [6, 7], | ||
4: [], | ||
5: [8, 9], | ||
6: [], | ||
7: [], | ||
8: [], | ||
9: [] | ||
}, | ||
'root': 1, | ||
'position': { | ||
1: (400, 100), | ||
2: (200, 200), | ||
3: (600, 200), | ||
4: (100, 300), | ||
5: (300, 300), | ||
6: (500, 300), | ||
7: (700, 300), | ||
8: (200, 400), | ||
9: (400, 400) | ||
}, | ||
'goals': [] | ||
} | ||
world = CreateTreeWorld(worldName="Leetcode Binary Tree Level Order Traversal", worldInfo=treeInfo, radius=30) | ||
world.constructWorld() | ||
agent = TreeAgent(world, agentName="TraversalAgent") | ||
world.setAgent(agent) | ||
|
||
def sim(): | ||
print("Starting the simulation!") | ||
levels = levelOrderTraversal(1, agent, world) | ||
print("Level Order Traversal:") | ||
for level in levels: | ||
print(level) | ||
print("Simulation Completed!") | ||
|
||
agent.setAlgorithmCallBack(sim) | ||
world.showWorld() |
60 changes: 60 additions & 0 deletions
60
tutorials/Tree World/leetcode_lowest_common_ancestor_od_a_binary_tree.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from traverseCraft.world import CreateTreeWorld | ||
from traverseCraft.agent import TreeAgent | ||
|
||
def lowestCommonAncestor(rootId:int, p:int, q:int, agent:TreeAgent, world:CreateTreeWorld)->int: | ||
if(rootId == None): | ||
return None | ||
if(rootId == p or rootId == q): | ||
return rootId | ||
children = [] | ||
for child in world.nodeMap[rootId].children: | ||
agent.moveAgent(child.id, delay=0.5) | ||
children.append(lowestCommonAncestor(child.id, p, q, agent, world)) | ||
agent.moveAgent(rootId, delay=0.5) | ||
|
||
for child in children: | ||
if(child == None): | ||
children.remove(child) | ||
if(len(children) == 2): | ||
return rootId | ||
elif(len(children) == 1): | ||
return children[0] | ||
return None | ||
|
||
if __name__ == "__main__": | ||
treeInfo = { | ||
'adj': { | ||
1: [2, 3], | ||
2: [4, 5], | ||
3: [6, 7], | ||
4: [], | ||
5: [8, 9], | ||
6: [], | ||
7: [], | ||
8: [], | ||
9: [] | ||
}, | ||
'root': 1, | ||
'position': { | ||
1: (400, 100), | ||
2: (200, 200), | ||
3: (600, 200), | ||
4: (100, 300), | ||
5: (300, 300), | ||
6: (500, 300), | ||
7: (700, 300), | ||
8: (200, 400), | ||
9: (400, 400) | ||
}, | ||
'goals': [7, 9] | ||
} | ||
world = CreateTreeWorld(worldName = "Leetcode Lowest Common Ancestor of a Binary Tree", worldInfo = treeInfo, radius = 30) | ||
world.constructWorld() | ||
agent = TreeAgent(world, agentName = "Robot") | ||
world.setAgent(agent) | ||
def sim(): | ||
print("Starting the simulation!") | ||
print("Lowest Common Ancestor of 7 and 9: ", lowestCommonAncestor(1, 7, 9, agent, world)) | ||
print("Simulation Completed!") | ||
agent.setAlgorithmCallBack(sim) | ||
world.showWorld() |