Bash-style Tree Structure in Python

CHEN TSU PEI
Python-Review-en
Published in
1 min readDec 31, 2019

Introduce a package for output bash-style tree structure in Python

Introduction

Sometimes, we want to visualize the tree structure such as trie, or our tree structure in real world. I wrote a small pip package for doing such task. Here is a trie visualization example.

├── a│   └── p│       └── p│           ├── l│           │   ├── [e]│           │   │   └── p│           │   │       └── i│           │   │           └── [e]│           │   └── i│           │       └── c│           │           └── a│           │               └── t│           │                   └── i│           │                       └── o│           │                           └── [n]│           └── [s]└── b   └── a       └── l          └── [l]

Usage

It’s simple to use. Each node could invoke visualize() function to visualize its subtree structure.

from treeviz.treeviz import Noderoot = Node("Jason")child1 = Node("Mary")child2 = Node("John")grandson1 = Node("Kevin")grandson2 = Node("Doris")grandson3 = Node("James")grandson4 = Node("Momo")grandgrandson1 = Node("Baby")
root.add_child(child1)root.add_child(child2)child1.add_child(grandson1)child1.add_child(grandson2)child2.add_child(grandson3)child2.add_child(grandson4)grandson1.add_child(grandgrandson1)root.visualize()# VisualizeJason├── Mary│ ├── Kevin│ │ └── Baby│ └── Doris└── John ├── James └── Momo

Learn more

Please check my github project for more detailed instruction. Thanks!

--

--

CHEN TSU PEI
Python-Review-en

這邊停止更新了!麻煩移駕到https://tsupei.github.io,有持續更新更多NLP的文章唷!