<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pallab Pain on Medium]]></title>
        <description><![CDATA[Stories by Pallab Pain on Medium]]></description>
        <link>https://medium.com/@pallabpain?source=rss-b3ea1d259ced------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XFtUhkQyVULEOIPDA0iSiw@2x.jpeg</url>
            <title>Stories by Pallab Pain on Medium</title>
            <link>https://medium.com/@pallabpain?source=rss-b3ea1d259ced------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:17:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pallabpain/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use a trie to rebuild a directory tree in Python]]></title>
            <link>https://medium.com/analytics-vidhya/if-youre-given-the-path-to-a-directory-you-can-easily-perform-a-walk-on-it-and-generate-the-bf321a8bdd63?source=rss-b3ea1d259ced------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf321a8bdd63</guid>
            <category><![CDATA[trie]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Pallab Pain]]></dc:creator>
            <pubDate>Sun, 31 May 2020 12:12:43 GMT</pubDate>
            <atom:updated>2020-06-02T15:52:39.875Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/650/0*AafE2DcKGHyO-EnW.png" /><figcaption>Source: <a href="https://www.raywenderlich.com/892-swift-algorithm-club-swift-trie-data-structure">https://www.raywenderlich.com/892-swift-algorithm-club-swift-trie-data-structure</a></figcaption></figure><p>If you’re given the path to a directory, you can easily perform a walk on it and generate the directory tree as a list such that each directory in the tree is followed by its files and sub-directories and so on. Let me show it to you using an example.</p><p>Here’s how the output of the tree looks like for a directory named pcache on my computer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tTF4MUwQCZb5FRkVBolikA.png" /></figure><p>Let’s try to generate this list using in Python. We’ll use a method from the os standard library called os.walk()</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6137ad610bdb92fcaf19b3be394e926/href">https://medium.com/media/c6137ad610bdb92fcaf19b3be394e926/href</a></iframe><p>When I print the result of generate_directory_tree with./pcache as the argument, I get the following result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eeMEWXndBF_i0jhzvvFEZA.png" /></figure><p>If you look at the result, you’ll notice that any directory is followed by its contents before moving on to the next directory at the same level, just like how the output of tree looks like.</p><p>Now, here’s the problem. What happens when the situation is reversed? You’re given a random list of file and directory path entries, and you have to sort the entries in a directory tree-like structure where a directory is immediately followed by its contents. Consider the following list that has been scrambled.</p><h4>Understanding how trie can help us solve our problem</h4><blockquote>A trie (also known as radix tree or prefix tree) is a tree-based data structure that is used to store strings for faster re<strong>trie</strong>val. Each node stores some character/word and traversing along a branch from the root to a leaf builds up the entire string/text.</blockquote><figure><img alt="http://theoryofprogramming.com/2015/01/16/trie-tree-implementation/" src="https://cdn-images-1.medium.com/max/791/0*rHz4fY4Ru_QWh7cc.jpg" /><figcaption>Source: <a href="http://theoryofprogramming.com/2015/01/16/trie-tree-implementation/">http://theoryofprogramming.com/2015/01/16/trie-tree-implementation/</a></figcaption></figure><p>Now if we consider our paths and split them on the / path separator, we essentially have keys that can be stored in every node. Consider the following diagram for reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*1rCbra5eSQsDNOOZPj2DhA.png" /></figure><p>Now, if we perform a pre-order traversal on this trie and print the path when we hit a leaf node, we’ll practically get a list of path entries that will be arranged in a directory-tree like structure. Let’s see this in action with some Python code.</p><p>There’s a great library available on PyPI called <a href="https://github.com/mina86/pygtrie"><strong>pygtrie</strong></a><strong> </strong>that makes things simpler for us. That means we don’t have to worry about reinventing the wheel and writing the entire trie implementation from scratch.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14cb6dc94487ef10db8767fd580478de/href">https://medium.com/media/14cb6dc94487ef10db8767fd580478de/href</a></iframe><p>So, we have with us a list of paths along with their type i.e. whether its a file or a directory. We feed this list to our trie which is an instance of pygtrie.StringTrie which has a cool feature where it can accept a separator to tokenize the string and use the tokens in the trie node. The path is stored as the key and the type as the value. The reason we convert the type to a bool is that we want to determine during our traversal whether we have hit a leaf or an intermediate node. We then call the traverse method on our trie that performs a pre-order traversal and the callback helps us print the paths as we traverse our trie. Once we run the code, we get the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qWbkgnQwoWbhTd5xnQo0LA.png" /></figure><p>And this is exactly how we wanted it.</p><p>You can tweak the implementation to store more than just bool in the value and do a lot of things based on your requirements. Hope this article has helped you.</p><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf321a8bdd63" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/if-youre-given-the-path-to-a-directory-you-can-easily-perform-a-walk-on-it-and-generate-the-bf321a8bdd63">How to use a trie to rebuild a directory tree in Python</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>