Skip to content

Commit

Permalink
initial commit assinment
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyKoshulap committed Jul 20, 2024
1 parent 6fd2944 commit f691881
Showing 1 changed file with 122 additions and 15 deletions.
137 changes: 122 additions & 15 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print((hash('your first name') % 3) + 1)"
"print((hash('Yuri') % 3) + 1)"
]
},
{
Expand Down Expand Up @@ -72,18 +80,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# 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",
"def is_duplicate(root: TreeNode) -> int:\n",
" # TODO"
" # TODO"
]
},
{
Expand Down Expand Up @@ -198,14 +205,17 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"\"\"\"Find if any number in a binary tree appears more than once with the following conditions: If a number is duplicated, return it. If there are several duplicated numbers, return the one nearest to the root. If no duplicates are found, return -1 to signify no duplicates.\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In this .ipynb file, there are examples that illustrate how the code should work (the examples provided above). Create 2 new examples for the question you have been assigned, that demonstrate you understand the problem. For question 1 and 2, you don't need to create the tree demonstration, just the input and output.\n"
"\n",
"- In the .md file containing your problem, there are examples that illustrate how the code should work. Create 2 new examples that demonstrate you understand the problem.\n"
]
},
{
Expand All @@ -214,7 +224,11 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Input: root = [0, 3, 5, 7, 2, 7, 12];\n",
"# Output is 7 as it is the duplicate value;\n",
"\n",
"# Input: root = [6, 2, 5, 1];\n",
"# Output is -1 as no duplicates were found;"
]
},
{
Expand All @@ -227,11 +241,75 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"from collections import deque\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",
"def is_duplicate(root: TreeNode) -> int:\n",
" if not root:\n",
" return -1\n",
" queue = deque([(root, 0)])\n",
" value_distance = {}\n",
" min_distance = float('inf')\n",
" result = -1\n",
" \n",
" while queue:\n",
" node, distance = queue.popleft()\n",
"\n",
" if node.val in value_distance and distance < min_distance:\n",
" min_distance = distance\n",
" result = node.val\n",
" else:\n",
" value_distance[node.val] = distance\n",
"\n",
" if node.left:\n",
" queue.append((node.left, distance + 1))\n",
" if node.right:\n",
" queue.append((node.right, distance + 1))\n",
"\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"# Creating nodes\n",
"root = TreeNode(0)\n",
"node1 = TreeNode(3)\n",
"node2 = TreeNode(5)\n",
"node3 = TreeNode(7)\n",
"node4 = TreeNode(2)\n",
"node5 = TreeNode(7)\n",
"node6 = TreeNode(12)\n",
"\n",
"\n",
"# Building the tree\n",
"root.left = node1\n",
"root.right = node2\n",
"node1.left = node3\n",
"node1.right = node4\n",
"node2.left = node5\n",
"node2.right = node6\n",
"\n",
"# Calling the function\n",
"print(is_duplicate(root))"
]
},
{
Expand All @@ -248,7 +326,21 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"The is_duplicate function searches the first duplicate value in a binary tree by performing the \"breadth-first search (BFS)\". At the initialization phase the function checks if the root of the tree is None. If it is, the function returns -1 (no duplicates).\n",
"\n",
"Next it initializes a queue with the root node and its distance from the root (\"0\"), a dictionary value_distance to store the distances of the node values from the root, and two variables min_distance and result. Thereby it keeps track of the minimum distance of a duplicate value from the root and the corresponding value. The BFS (while) loop runs until there are no more unchecked nodes in the queue.\n",
"\n",
"Subsequently the function checks if the value of the dequeued node is already in the value_distance dictionary (signals presence of a duplicate). If the distance of a duplicate is less than min_distance, the function updates min_distance and result with the current distance and value.\n",
"\n",
"The function then checks if the dequeued node has a left or right child and enqueues the child and its distance from the root (= the parent’s distance + 1).\n",
"\n",
"Finally, the function returns result: the value of the first duplicate node, or -1 if no duplicates were found.\n",
"\n",
"To sum it up, this solution works, because it systematically visits all the nodes of the tree in order of their distance from the root, and keeps track of the first duplicate value it encounters. \n",
"\n",
"\n",
"\n",
" However, please note that this function assumes that all node values are integers and that the tree is a binary tree. If these assumptions are not met, the function may not work as expected. Please make sure to adjust the function as needed to match your specific requirements and constraints.\"\"\""
]
},
{
Expand All @@ -265,7 +357,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"The time complexity of this function is O(n) (where \"n\" is the number of nodes), because the function visits each node once.\n",
"The space complexity is also O(n), because in the worst case, it needs to store all the nodes in the queue or the dictionary. This makes the function efficient for large trees.\"\"\""
]
},
{
Expand All @@ -282,7 +375,21 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"\"\"\"One such alternative is the depth-first search (DFS) with recursion. In this approach the function goes as deep as possible along each branch before backtracking.\n",
"\n",
"Step 1: Create an empty set to keep track of the values of the nodes that have been visited.\n",
"\n",
"Step 2: Define a recursive function that performs a DFS on the tree. The function should take a node and its depth as parameters.\n",
"\n",
"Step 3: If the node is 'None', return '-1'.\n",
"\n",
"Step 4: If the value of the node is already in the set, return the value.\n",
"\n",
"Step 5: Add the value of the node to the set.\n",
"\n",
"Step 6: Recursively call the DFS function on the left and right children of the node. If either call returns a value other than '-1', return that value.\n",
"\n",
"Step 7: If the DFS function has visited all the nodes and hasn't found a duplicate, return '-1'.\"\"\""
]
},
{
Expand Down Expand Up @@ -344,7 +451,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f691881

Please sign in to comment.