Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e386ef

Browse files
Trees
1 parent 0c27eb6 commit 7e386ef

File tree

19 files changed

+337
-10
lines changed

19 files changed

+337
-10
lines changed

‎Images/bintree.png‎

203 KB
Loading[フレーム]

‎Images/bst1.png‎

5.34 KB
Loading[フレーム]

‎Images/bst2.png‎

3.07 KB
Loading[フレーム]

‎Images/bt1.png‎

29.2 KB
Loading[フレーム]

‎Images/bt2.png‎

7.1 KB
Loading[フレーム]

‎Images/bt3.png‎

317 KB
Loading[フレーム]

‎Images/trees.png‎

83.2 KB
Loading[フレーム]

‎Trees and Its Types/1.Trees/.ipynb_checkpoints/1. Tree-checkpoint.ipynb‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "3bcf452c-a6e9-48fe-9650-73b4277c2761",
6+
"metadata": {},
7+
"source": [
8+
"--- \n",
9+
"<img align=\"left\" width=\"110\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg\"> \n",
10+
"\n",
11+
"<h1 align=\"center\">Journey from Begginer to Expert</h1>\n",
12+
"<h1 align=\"center\">Course: Data Structures and Algorithms</h1>\n",
13+
"\n",
14+
"---\n",
15+
"<h1><div align=\"right\">Muhammad Sheraz (Python Programmer)</div></h1>\n",
16+
"<h1><div align=\"center\">Lecture (Trees)</div></h1>\n",
17+
"<img height = '500px' src='../../Images/trees.png'>"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"id": "b0571c3f-c814-490d-9bbd-2bfc796f77f6",
24+
"metadata": {},
25+
"outputs": [],
26+
"source": []
27+
},
328
{
429
"cell_type": "code",
5-
"execution_count": 35,
30+
"execution_count": 1,
631
"id": "0f0b8afb",
732
"metadata": {},
833
"outputs": [
@@ -75,7 +100,6 @@
75100
}
76101
],
77102
"source": [
78-
"# linked nodes based code for implementation of TREE basics\n",
79103
"class Tree:\n",
80104
" class Node:\n",
81105
" def __init__(self, data=None, parent=None):\n",
@@ -745,7 +769,7 @@
745769
"name": "python",
746770
"nbconvert_exporter": "python",
747771
"pygments_lexer": "ipython3",
748-
"version": "3.10.9"
772+
"version": "3.10.6"
749773
}
750774
},
751775
"nbformat": 4,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Tree Implementation
2+
3+
This Python implementation provides a basic Tree structure with various functionalities using linked nodes.
4+
5+
## Tree
6+
7+
A Tree is a hierarchical data structure consisting of nodes connected by edges. Each node has a parent (except for the root) and zero or more children.
8+
9+
<img height = '500px' src='../../Images/trees.png'>
10+
11+
## Class Structure
12+
13+
### Node
14+
- **Attributes:**
15+
- `data`: The value of the node.
16+
- `children`: List of child nodes.
17+
18+
### Tree
19+
- **Attributes:**
20+
- `root`: Reference to the root node.
21+
22+
### Functions and Time Complexity (Worst and Best Cases) and Space Complexity
23+
24+
| Function | Description | Worst Case Time Complexity | Best Case Time Complexity | Space Complexity (Auxiliary) |
25+
|---------------------------------------|-----------------------------------------------|----------------------------|---------------------------|-------------------------------|
26+
| `addnode(n, p)` | Adds a node as a child to an existing node. | O(n) | O(1) | O(1) |
27+
| `Display()` | Displays the nodes of the Tree. | O(n) | O(n) | O(h) |
28+
| `size()` | Returns the number of nodes in the Tree. | O(n) | O(n) | O(h) |
29+
| `pre_order_traversal()` | Pre-order traversal of the Tree. | O(n) | O(n) | O(h) |
30+
| `post_order_traversal()` | Post-order traversal of the Tree. | O(n) | O(n) | O(h) |
31+
| `in_order_traversal()` | In-order traversal of the Tree. | O(n) | O(n) | O(h) |
32+
| `leaves_count()` | Returns the number of leaves in the Tree. | O(n) | O(n) | O(h) |
33+
| `search_node(v)` | Searches for a node with a given value. | O(n) | O(1) | O(1) |
34+
| `remove(v)` | Removes a node with a given value. | O(n) | O(n) | O(h) |
35+
| `cut(copy)` | Cuts a subtree rooted at a specified node. | O(n) | O(n) | O(h) |
36+
| `update(old, new)` | Updates the value of a node in the Tree. | O(n) | O(n) | O(h) |
37+
| `cut_paste(copy, paste)` | Cuts a subtree and pastes it as a child to another node. | O(n) | O(n) | O(h) |
38+
| `traverse_by_queue()` | Level-order traversal of the Tree using a queue. | O(n) | O(n) | O(h) |
39+
40+
**Note:**
41+
- Time complexity is given in terms of the number of nodes (n) or the height of the tree (h).
42+
- Space complexity is specified for auxiliary space, not including the space required by the input and output.
43+
44+
Feel free to use this Tree implementation for your projects and modify it as needed!

‎Trees and Its Types/1.Trees/1. Tree.ipynb‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "3bcf452c-a6e9-48fe-9650-73b4277c2761",
6+
"metadata": {},
7+
"source": [
8+
"--- \n",
9+
"<img align=\"left\" width=\"110\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg\"> \n",
10+
"\n",
11+
"<h1 align=\"center\">Journey from Begginer to Expert</h1>\n",
12+
"<h1 align=\"center\">Course: Data Structures and Algorithms</h1>\n",
13+
"\n",
14+
"---\n",
15+
"<h1><div align=\"right\">Muhammad Sheraz (Python Programmer)</div></h1>\n",
16+
"<h1><div align=\"center\">Lecture (Trees)</div></h1>\n",
17+
"<img height = '500px' src='../../Images/trees.png'>"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"id": "b0571c3f-c814-490d-9bbd-2bfc796f77f6",
24+
"metadata": {},
25+
"outputs": [],
26+
"source": []
27+
},
328
{
429
"cell_type": "code",
5-
"execution_count": 35,
30+
"execution_count": 1,
631
"id": "0f0b8afb",
732
"metadata": {},
833
"outputs": [
@@ -75,7 +100,6 @@
75100
}
76101
],
77102
"source": [
78-
"# linked nodes based code for implementation of TREE basics\n",
79103
"class Tree:\n",
80104
" class Node:\n",
81105
" def __init__(self, data=None, parent=None):\n",
@@ -745,7 +769,7 @@
745769
"name": "python",
746770
"nbconvert_exporter": "python",
747771
"pygments_lexer": "ipython3",
748-
"version": "3.10.9"
772+
"version": "3.10.6"
749773
}
750774
},
751775
"nbformat": 4,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /