Skip to content

Commit

Permalink
Merge pull request #61 from UBC-MDS/toturial-list-to-tree
Browse files Browse the repository at this point in the history
function list-to-tree also added to our tutorial
  • Loading branch information
AzinPiran authored Jan 26, 2025
2 parents e2eeb91 + d877de0 commit 70d68b0
Showing 1 changed file with 89 additions and 36 deletions.
125 changes: 89 additions & 36 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.1.0\n"
]
}
],
"source": [
"import datastructpy\n",
"\n",
"from datastructpy.non_linear.trees.binary_search_tree import BinarySearchTree\n",
"print(datastructpy.__version__)"
]
},
Expand Down Expand Up @@ -65,21 +73,47 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## The First Step: Building the Treasure Map"
"## Building the Treasure Map (Building Your Tree)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first instruction on the map is to insert some random numbers, at first you don't know why but the map just asked you to insert these numbers. For example you chose these four numbers [10, 5, 15, 8]. Here we are going to use list_to_tree(). "
"The first instruction on the map is to insert some random numbers, at first, you don't know why but the map just asks you to insert these numbers. \n",
"To start our journey, we'll create our first BST from a list of numbers. You can use the `list_to_tree()` function to convert a list into a Binary Search Tree:"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tree Structure After Creation:\n",
"10\n",
"5\n",
"15\n",
"8\n"
]
}
],
"source": [
"### Step 1: Building Your Tree"
"# List of random treasures (numbers)\n",
"treasures = [10, 5, 15, 8]\n",
"\n",
"# Create a Binary Search Tree from the list\n",
"bst = BinarySearchTree.list_to_tree(treasures)\n",
"\n",
"# Check the structure of the tree\n",
"print(\"Tree Structure After Creation:\")\n",
"print(bst.root.key) # Output: 10\n",
"print(bst.root.left.key) # Output: 5\n",
"print(bst.root.right.key) # Output: 15\n",
"print(bst.root.left.right.key) # Output: 8\n"
]
},
{
Expand All @@ -96,18 +130,19 @@
"Apparently, there are other pirates that have the same map with you! You discovered that you can enter some random fake treasures to fool these other pirates. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: Inserting New Keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 has been added to the map!\n"
]
}
],
"source": [
"# Insert a random fake treasure 2 to fool other pirates\n",
"bst.insert(2)\n",
Expand Down Expand Up @@ -136,18 +171,19 @@
"Let's use the **search** method to locate this key in our BST."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: Searching for Keys"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The key was not found... Perhaps it was a decoy? 🤔\n"
]
}
],
"source": [
"# Searching for the lost treasure key (12)\n",
"search_result = bst.search(12)\n",
Expand Down Expand Up @@ -182,9 +218,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"# Removing a treasure from the map\n",
"bst.delete(5)\n",
Expand All @@ -211,15 +255,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Final Tree Structure:\n",
"10\n",
"8\n"
]
}
],
"source": [
"# Final tree structure\n",
"# Final structure of the tree\n",
"print(\"Final Tree Structure:\")\n",
"print(bst.root.key) # Output: 15 (new root)\n",
"print(bst.root.left.key) # Output: 8 (left child of 15)\n",
"print(bst.root.right.left.key) # Output: 12 (left child of 15)"
"print(bst.root.key) # Output: 15\n",
"print(bst.root.left.key) # Output: 8\n"
]
},
{
Expand Down Expand Up @@ -250,9 +303,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:pycounts]",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "conda-env-pycounts-py"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -264,7 +317,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 70d68b0

Please sign in to comment.