101

GitHub - shivamMg/ppds: Pretty Print Data Structures

 5 years ago
source link: https://github.com/shivamMg/ppds
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

ppds godoc Build License: MIT

Pretty Print Data Structures

Stacks, queues, trees and linked lists are data structures that you might find yourself working with quite often. This library lets you pretty print these with minimum effort. Certain assumptions can be made for each data structure, for instance, a stack or a queue will have methods synonymous to Push (insert) and Pop (remove), a tree node will have links to its child nodes, or a linked list node will have a link to its next adjacent node. This library utilises those assumptions, and exports interfaces and functions to pretty print them.

The following data structures are supported:

Trees

tree

A type that satisfies the following interface can be printed using tree.Print.

type Node interface {
	Data() interface{}
	Children() []Node
}
Music
├──────────────────────────────────────────┐
Classical                                  Pop/Rock
├────────────────────┐                     ├───────────────────────────┐
Instrumental         Vocal                 Organic                     Electronic
├──────┐             ├─────────────┐       ├────────────┐              ├────────────────┐
Piano  Orchestra     Opera         Chorus  Rock         Country        Pop              Techno
       ├──────┐      ├─────┐               │            ├────────┐     ├─────────┐      ├────────────┐
       Light  Heavy  Male  Female          Heavy metal  Dancing  Soft  Late pop  Disco  Soft techno  Hard techno

If branching factor is high or data lengths are large, it would make more sense to print the tree horizontally. That can be done using tree.PrintHr function.

Music ┬─ Classical ┬─ Instrumental ┬─ Piano
      │            │               └─ Orchestra ┬─ Light
      │            │                            └─ Heavy
      │            └─ Vocal ┬─ Opera ┬─ Male
      │                     │        └─ Female
      │                     └─ Chorus
      └─ Pop/Rock ┬─ Organic ┬─ Rock ── Heavy metal
                  │          └─ Country ┬─ Dancing
                  │                     └─ Soft
                  └─ Electronic ┬─ Pop ┬─ Late pop
                                │      └─ Disco
                                └─ Techno ┬─ Soft techno
                                          └─ Hard techno

Inspired by tree, a popular directory listing command, tree.PrintHrn prints the tree in similar style. Every node is printed on a separate line making the output tree look less cluttered and more elegant. It does take up vertical space though.

Music
├─ Classical
│  ├─ Instrumental
│  │  ├─ Piano
│  │  └─ Orchestra
│  │     ├─ Light
│  │     └─ Heavy
│  └─ Vocal
│     ├─ Opera
│     │  ├─ Male
│     │  └─ Female
│     └─ Chorus
└─ Pop/Rock
   ├─ Organic
   │  ├─ Rock
   │  │  └─ Heavy metal
   │  └─ Country
   │     ├─ Dancing
   │     └─ Soft
   └─ Electronic
      ├─ Pop
      │  ├─ Late pop
      │  └─ Disco
      └─ Techno
         ├─ Soft techno
         └─ Hard techno

Linked Lists

list

type Node interface {
	Data() interface{}
	Next() Node
}

A counter is also printed below each node so one doesn't have to manually count them by pointing a finger at the screen. Also, a list can contain a million elements, in which case counting by simply shifting finger on screen might not even be possible.

┌───┐┌───┐┌───┐┌─┐┌────┐┌────┐┌────┐
│333├┤222├┤111├┤0├┤-111├┤-222├┤-333│
├───┤├───┤├───┤├─┤├────┤├────┤├────┤
│  1││  2││  3││4││   5││   6││   7│
└───┘└───┘└───┘└─┘└────┘└────┘└────┘

Stacks

stack

type Stack interface {
	Pop() (ele interface{}, ok bool)
	Push(ele interface{})
}
│ factorial (1) │7│
│ factorial (2) │6│
│ factorial (3) │5│
│ factorial (4) │4│
│ factorial (5) │3│
│ factorial (6) │2│
│ factorial (7) │1│
└───────────────┴─┘

Queues

queue

type Queue interface {
	Pop() (ele interface{}, ok bool)
	Push(ele interface{})
}
 ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐
→│Dec│→│Nov│→│Oct│→│Sep│→│Aug│→│Jul│→│Jun│→│May│→│Apr│→│Mar│→│Feb│→│Jan│→
 ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤ ├───┤
 │  1│ │  2│ │  3│ │  4│ │  5│ │  6│ │  7│ │  8│ │  9│ │ 10│ │ 11│ │ 12│
 └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘

Contribute

Bug reports, PRs, feature requests, all are welcome.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK