|
7 | 7 | "# Trees\n", |
8 | 8 | "<a href=\"https://colab.research.google.com/github/rambasnet/Python-Fundamentals/blob/master/notebooks/Ch23-Trees.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n", |
9 | 9 | "\n", |
10 | | - "http://openbookproject.net/thinkcs/python/english3e/trees.html\n", |
11 | | - "- like linked lists, trees are made up of nodes\n", |
| 10 | + "## External Resources\n", |
| 11 | + "- Think Python - [http://openbookproject.net/thinkcs/python/english3e/trees.html](http://openbookproject.net/thinkcs/python/english3e/trees.html)\n", |
| 12 | + "- YouTube Video - [https://youtu.be/8ZUsDn98hNY](https://youtu.be/8ZUsDn98hNY)\n", |
| 13 | + "- YouTube Podcast - [https://youtu.be/wj0s8OIzcX4](https://youtu.be/wj0s8OIzcX4)\n", |
| 14 | + "- NotebookLM learning resources - [https://notebooklm.google.com/notebook/eb7913fe-030f-43aa-a2f3-1c15cabac10c](https://notebooklm.google.com/notebook/eb7913fe-030f-43aa-a2f3-1c15cabac10c)\n", |
| 15 | + "\n", |
| 16 | + "## Overview\n", |
| 17 | + "\n", |
| 18 | + "- like linked lists, trees are made up of nodes or objects that contain data and references to other nodes\n", |
| 19 | + "- unlike linked lists, trees can have multiple references to other nodes\n", |
| 20 | + "- trees are hierarchical data structures that are used to represent relationships between data items\n", |
| 21 | + "- trees are widely used in computer science for various applications such as searching, sorting, and organizing data\n", |
| 22 | + "- common types of trees include binary trees, binary search trees, AVL trees, and B-trees, etc.\n", |
12 | 23 | "\n", |
13 | 24 | "## Binary Tree\n", |
14 | 25 | " - is a commonly used tree in which each node contains a reference to atmost two other nodes (possibly None)\n", |
15 | 26 | "- these references are referred to as the left and right subtrees\n", |
16 | 27 | "- like the node of linked list, each node also contains data/cargo\n", |
17 | 28 | "- like linked lists, trees are recursive data structures that are defined recursively:\n", |
18 | 29 | " 1. the empty tree, represented by None, or\n", |
19 | | - " 2. a node that contains a data and two tree references (left and right subtree)" |
| 30 | + " 2. a node that contains a data and two tree references (left and right subtree)\n", |
| 31 | + "- the topmost node in the tree is called the root node\n", |
| 32 | + "- nodes with no children are called leaf nodes\n", |
| 33 | + "- nodes that are not leaf nodes are called internal nodes\n", |
| 34 | + "- the height of a tree is the length of the longest path from the root to a leaf\n", |
| 35 | + "- trees can be traversed in different ways such as inorder, preorder, and postorder traversal" |
20 | 36 | ] |
21 | 37 | }, |
22 | 38 | { |
|
111 | 127 | "metadata": {}, |
112 | 128 | "source": [ |
113 | 129 | "## traversing tree\n", |
114 | | - "- natural way to traverse a tree is recursively!" |
| 130 | + "\n", |
| 131 | + "- natural way to traverse a tree is recursively!\n", |
| 132 | + "- for each node, visit the node and then recursively visit its left and right subtrees\n", |
| 133 | + "- three common ways to traverse a binary tree:\n", |
| 134 | + " - inorder traversal\n", |
| 135 | + " - preorder traversal\n", |
| 136 | + " - postorder traversal\n", |
| 137 | + "- each traversal visits the nodes in a different order and is useful for different applications\n" |
115 | 138 | ] |
116 | 139 | }, |
117 | 140 | { |
|
201 | 224 | "metadata": {}, |
202 | 225 | "source": [ |
203 | 226 | "### pre-order tree traversal\n", |
| 227 | + "\n", |
204 | 228 | "- contents of the root appear before the contents of the children\n", |
205 | 229 | "- recursive algorithm:\n", |
206 | 230 | " - visit the node\n", |
207 | | - " - visit left subtree\n", |
208 | | - " - visit right subtree" |
| 231 | + " - visit left subtree \n", |
| 232 | + " - visit right subtree\n" |
209 | 233 | ] |
210 | 234 | }, |
211 | 235 | { |
|
0 commit comments