Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 89 additions & 9 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\" The Question Two \"Path to Leaves\" is to find all the unique paths from the root to the leaves in a binary tree. The binary tree is given by its root node. The paths should be returned in any sequence.\n",
"\n",
"The task is to identify what kind of traversal method this is and to return all the paths from the root to the leaves. In the first example the expected output for the binary tree [1, 2, 2, 3, 5, 6, 7] would be [[1, 2, 3], [1, 2, 5], [1, 2, 6], [1, 2, 7]]. In another example, if the binary tree has root node values as [10, 9, 7, 8], the expected output would be [[10, 7], [10, 9, 8]].\"\"\""
]
},
{
Expand All @@ -54,7 +56,17 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\" Let the new example have root node 5 with two branches: 4 and 8. The branch 4 nodes into 11, splitting into 7 and 2. The branch 8 splits into 13 and 4, and 4 leads to 1.\n",
"\n",
" 5\n",
" / \\\n",
" 4 8\n",
" / / \\\n",
" 11 13 4\n",
" / \\ /\n",
" 7 2 1\n",
"\n",
"In other words input is root = [5, 4, 8, 11, 13, 4, 7, 2, 1] The paths from the root to the leaves are [[5, 4, 11, 7], [5, 4, 11, 2], [5, 8, 13], [5, 8, 4, 1]].\"\"\""
]
},
{
Expand All @@ -71,7 +83,58 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Definition for a binary tree node.\n",
"class TreeNode(object):\n",
" def __init__(self, val = 0, left = None, right = None):\n",
" self.val = val\n",
" self.left = left\n",
" self.right = right\n",
"\n",
"root = TreeNode(val = 1)\n",
"root.left = TreeNode(val = 2)\n",
"root.right = TreeNode(val = 2)\n",
"root.left.left = TreeNode(val = 3)\n",
"root.left.right = TreeNode(val = 5)\n",
"root.right.left = TreeNode(val = 6)\n",
"root.right.right = TreeNode(val = 7)\n",
"\n",
"# preorder traversal\n",
"\n",
"def bt_path(root: TreeNode) -> list[list[int]]:\n",
" # TODO\n",
"\n",
" def dfs (node:TreeNode, path, allpaths):\n",
"\n",
" if node:\n",
" path.append(node.val)\n",
" # remove the comments to see what is happening on each recursive call\n",
" #print (\"node\",node.val)\n",
" #print (\"path:\",path)\n",
" #print (\"all paths:\",allpaths)\n",
"\n",
" if node.left:\n",
" dfs(node.left, path, allpaths)\n",
" path.pop() #remove the leaf number\n",
"\n",
" if node.right:\n",
" dfs(node.right, path, allpaths)\n",
" path.pop() #remove the leaf number\n",
" \n",
" if not node.right and not node.left:\n",
" allpaths.append (path[:])\n",
" \n",
" return allpaths\n",
" \n",
" path=[]\n",
" allpaths=[]\n",
" path = dfs (root, path, allpaths)\n",
"\n",
" return allpaths\n",
"\n",
"\n",
"btree_paths = []\n",
"btree_paths = bt_path(root)\n",
"print (\"final list of all paths:\", btree_paths)"
]
},
{
Expand All @@ -88,7 +151,11 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"This solution works by using a Depth-First Search (DFS) algorithm. The DFS algorithm starts at the root of the tree and explores as far as possible along each branch before backtracking. \n",
"\n",
"The main f(x) (bt_path) initializes two empty lists (path and allpaths). \"path\" stores the current path from the root to a node, and \"allpaths\" accumulates all such paths. The DFS f(x) takes three arguments: the current \"node\" \"path\" and the list of \"allpaths\". If the \"node\" is not \"None\", its value is appended to the current \"path\". If the current `node` has a right child, the DFS f(x) is recursively called removing the last node from the \"path\" (path.pop()). The same applies for a right child in the node. If the \"node\" ends (no children), the current \"path\" is added to \"allpaths\". Finally, the DFS f(x) returns \"allpaths\" with all the paths in the tree.\n",
"\n",
"This solution works because DFS systematically explores all paths down the binary tree, and keeps track of all paths.\"\"\""
]
},
{
Expand All @@ -105,7 +172,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"Each node is visited once, therefore the time complexity of this solution is O(N) (where N is the number of nodes in the tree).\n",
"The space complexity is also O(N), because in the worst case scenario (the tree is a straight line), the maximum length of the path could be N.\"\"\""
]
},
{
Expand All @@ -122,7 +190,17 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"As as in anything else there are stronger and weaker sides to this solution.\n",
"\n",
"- The DFS used in this solution is an efficient way to queue a binary tree and explore all paths.\n",
"- Also the use of path and allpaths lists is a common practice to keep track of the current path and all paths respectively.\n",
"- In addition the use of path.pop() after each recursive call ensures that the path list only contains nodes from the current path.\n",
"\n",
"- On the other hand, the comments could be more descriptive to better explain what each part of the code does. For example, it would be helpful to explain why path.pop() is used and why a copy of path is added to allpaths.\n",
"- The function name \"dfs\" can be misleading. A more descriptive name e.g. \"preorder_traversal\" or \"explore_paths\" might be more informative.\n",
"- It is a good practice to handle the case where the root node is None. And it is (excuse my tautology) not the case.\n",
"\n",
"To sum it up the areas of improvement are optional and do not affect the efficient and correct solution provided by Mario.\"\"\""
]
},
{
Expand All @@ -131,8 +209,8 @@
"source": [
"\n",
"## Part 3:\n",
"\n",
"Please write a 200 word reflection documenting your process from assignment 1, and your presentation and review experience with your partner at the bottom of the Jupyter Notebook under a new heading \"Reflection.\" Again, export this Notebook as pdf.\n"
"Please write a 200 word reflection documenting your process from assignment 1, and your presentation and review experience with your partner at the bottom of the Jupyter Notebook under a new heading \"Reflection.\" Again, export\n",
" this Notebook as pdf.\n"
]
},
{
Expand All @@ -148,7 +226,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"The assignment at the algorithms and data structures module were surprising in several ways, but overall, I enjoyed the idea creators' had when developing this assignment. \n",
"The element of game in playing the lottery to be allotted a task, and reviewing the peer work added the element of excitement to the whole process. However, even more important was a step-by-step unrolling of the assignment, which made me conceptualize the problem first, without jumping into the code headlong. Although as a “visual” person I would draw the algorithm diagram or sketch pseudo-code, describing the task in my own words allowed me to comprehend the task better.\n",
"The review of my partner's work was quite interesting. It showed a different approach to traverse the binary tree, and raised question on efficiency, use cases and differences in implementation of the BFS and DFS algorithms. It also made me reflect on my own coding habits. I must admit that some points of improvement to Mario's code (not enough comments, confusing choice of f(x)/variables names) applies to me in a greater extent. The only upsetting experience was the prolonged struggle to convert ipynb to pdf. To sum it up, I think it was a great learning experience.\"\"\""
]
},
{
Expand Down
Binary file added 02_activities/assignments/assignment_2.pdf
Binary file not shown.
Loading