Created
March 17, 2016 17:31
-
-
Save vr2262/64b506d7b04a8514a030 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Easy Pre-Order Traversal Using \"`yield from`\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"![Pre-order traversal diagram](https://upload.wikimedia.org/wikipedia/commons/d/d4/Sorted_binary_tree_preorder.svg \"Pre-order traversal diagram\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## F, B, A, D, C, E, G, I, H" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Wikipedia gives this algorithm:\n", | |
"> https://en.wikipedia.org/wiki/Tree_traversal#Pre-order\n", | |
">\n", | |
"> 1. Display the data part of the root (or current node).\n", | |
"> 2. Traverse the left subtree by recursively calling the pre-order function.\n", | |
"> 3. Traverse the right subtree by recursively calling the pre-order function.\n", | |
"\n", | |
"But this is specific to binary trees.\n", | |
"\n", | |
"In general, this is the algorithm for a pre-order traversal:\n", | |
"\n", | |
"1. Display the data part of the current node.\n", | |
"2. Traverse each subtree by recursively calling the pre-order function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# imports\n", | |
"from typing import Iterator, Iterable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Let's use a dict to represent this tree\n", | |
"#\n", | |
"# Each node looks like this:\n", | |
"# {\n", | |
"# 'node': <value>,\n", | |
"# 'branches': [list of nodes...],\n", | |
"# }\n", | |
"the_tree = {\n", | |
" 'node': 'F',\n", | |
" 'branches': [\n", | |
" {\n", | |
" 'node': 'B',\n", | |
" 'branches': [\n", | |
" {\n", | |
" 'node': 'A',\n", | |
" 'branches': [],\n", | |
" },\n", | |
" {\n", | |
" 'node': 'D',\n", | |
" 'branches': [\n", | |
" {\n", | |
" 'node': 'C',\n", | |
" 'branches': [],\n", | |
" },\n", | |
" {\n", | |
" 'node': 'E',\n", | |
" 'branches': [],\n", | |
" },\n", | |
" ],\n", | |
" },\n", | |
" ],\n", | |
" },\n", | |
" {\n", | |
" 'node': 'G',\n", | |
" 'branches': [\n", | |
" {\n", | |
" 'node': 'I',\n", | |
" 'branches': [\n", | |
" {\n", | |
" 'node': 'H',\n", | |
" 'branches': [],\n", | |
" },\n", | |
" ],\n", | |
" },\n", | |
" ],\n", | |
" },\n", | |
" ],\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def pre_order_sequentialize(tree: dict) -> Iterator[str]:\n", | |
" \"\"\"Yield the values of a tree in a pre-order traversal.\"\"\"\n", | |
" yield tree['node'] # display the data part of the current node\n", | |
" for branch in tree['branches']: # traverse each subtree\n", | |
" yield from pre_order_sequentialize(branch) # by recursively calling the pre-order function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<generator object pre_order_sequentialize at 0x7f96bc1dae60>\n", | |
"F\n", | |
"B\n", | |
"A\n" | |
] | |
} | |
], | |
"source": [ | |
"what_is_it = pre_order_sequentialize(the_tree)\n", | |
"print(what_is_it)\n", | |
"print(next(what_is_it))\n", | |
"print(next(what_is_it))\n", | |
"print(next(what_is_it))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def pretty_print_sequence(seq: Iterable) -> str:\n", | |
" \"\"\"Return a nice string representation of a sequence.\n", | |
" \n", | |
" Turn [1, 2, 3] into \"1, 2, 3\".\n", | |
" \"\"\"\n", | |
" return ', '.join(seq)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def pretty_print_tree(tree: dict) -> str:\n", | |
" \"\"\"Return a nice view of the pre-order sequentialization.\"\"\"\n", | |
" return pretty_print_sequence(pre_order_sequentialize(tree))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"F, B, A, D, C, E, G, I, H\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"sequentialization = pretty_print_tree(the_tree)\n", | |
"print(sequentialization)\n", | |
"print(sequentialization == 'F, B, A, D, C, E, G, I, H')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment