You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
491 B

4 years ago
  1. package huffman
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. func printNode(w io.Writer, n *Node) {
  7. if n.typ == MID {
  8. fmt.Fprintf(w, "\"%v\" -> {\"%v\" \"%v\"}\n", n.String(), n.l.String(), n.r.String())
  9. printNode(w, n.l)
  10. printNode(w, n.r)
  11. } else if n.typ == LEAF {
  12. fmt.Fprintf(w, "\"%v\" [style=filled];\n", n.String())
  13. }
  14. }
  15. func printTree(w io.Writer, n *Node) {
  16. fmt.Fprintf(w, `digraph hierarchy {
  17. node [fontname=Monospace,fontsize=10,shape=box]
  18. `)
  19. printNode(w, n)
  20. fmt.Fprintf(w, "}\n")
  21. }