2019-10-09 13:13:56 +02:00
|
|
|
;;; tree.el --- Working with Trees -*- lexical-binding: t -*-
|
2020-08-31 15:44:53 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-08-31 15:44:53 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
|
|
|
|
;; Package-Requires: ((emacs "25.1"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Some friendly functions that hopefully will make working with trees cheaper
|
|
|
|
;; and therefore more appealing!
|
|
|
|
;;
|
|
|
|
;; Tree terminology:
|
|
|
|
;; - leaf: node with zero children.
|
|
|
|
;; - root: node with zero parents.
|
|
|
|
;; - depth: measures a node's distance from the root node. This implies the
|
|
|
|
;; root node has a depth of zero.
|
|
|
|
;; - height: measures the longest traversal from a node to a leaf. This implies
|
|
|
|
;; that a leaf node has a height of zero.
|
|
|
|
;; - balanced?
|
|
|
|
;;
|
|
|
|
;; Tree variants:
|
|
|
|
;; - binary: the maximum number of children is two.
|
|
|
|
;; - binary search: the maximum number of children is two and left sub-trees are
|
|
|
|
;; lower in value than right sub-trees.
|
|
|
|
;; - rose: the number of children is variable.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 'prelude)
|
|
|
|
(require 'list)
|
|
|
|
(require 'set)
|
|
|
|
(require 'tuple)
|
|
|
|
(require 'series)
|
|
|
|
(require 'random)
|
|
|
|
(require 'maybe)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(cl-defstruct tree xs)
|
|
|
|
|
|
|
|
(cl-defstruct node value children)
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(cl-defun tree-node (value &optional children)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Create a node struct of VALUE with CHILDREN."
|
|
|
|
(make-node :value value
|
|
|
|
:children children))
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-reduce-breadth (acc f xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Reduce over XS breadth-first applying F to each x and ACC (in that order).
|
|
|
|
Breadth-first traversals guarantee to find the shortest path in a graph.
|
|
|
|
They're typically more difficult to implement than DFTs and may also incur
|
|
|
|
higher memory costs on average than their depth-first counterparts.")
|
|
|
|
|
|
|
|
;; TODO: Support :order as 'pre | 'in | 'post.
|
|
|
|
;; TODO: Troubleshoot why I need defensive (nil? node) check.
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-reduce-depth (acc f node)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Reduce over NODE depth-first applying F to each NODE and ACC.
|
|
|
|
F is called with each NODE, ACC, and the current depth.
|
|
|
|
Depth-first traversals have the advantage of typically consuming less memory
|
|
|
|
than their breadth-first equivalents would have. They're also typically
|
|
|
|
easier to implement using recursion. This comes at the cost of not
|
|
|
|
guaranteeing to be able to find the shortest path in a graph."
|
|
|
|
(cl-labels ((do-reduce-depth
|
|
|
|
(acc f node depth)
|
|
|
|
(let ((acc-new (funcall f node acc depth)))
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (or (maybe-nil? node)
|
2020-08-31 15:44:53 +02:00
|
|
|
(tree-leaf? node))
|
2019-10-09 13:13:56 +02:00
|
|
|
acc-new
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reduce
|
2019-10-09 13:13:56 +02:00
|
|
|
acc-new
|
|
|
|
(lambda (node acc)
|
2020-08-31 15:44:53 +02:00
|
|
|
(tree-do-reduce-depth
|
2019-10-09 13:13:56 +02:00
|
|
|
acc
|
|
|
|
f
|
|
|
|
node
|
2020-09-01 11:17:43 +02:00
|
|
|
(number-inc depth)))
|
2019-10-09 13:13:56 +02:00
|
|
|
(node-children node))))))
|
|
|
|
(do-reduce-depth acc f node 0)))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Helpers
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-height (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the height of tree XS.")
|
|
|
|
|
|
|
|
;; TODO: Troubleshoot why need for (nil? node). Similar misgiving
|
|
|
|
;; above.
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-leaf-depths (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a list of all of the depths of the leaf nodes in XS."
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reverse
|
2020-08-31 15:44:53 +02:00
|
|
|
(tree-reduce-depth
|
2019-10-09 13:13:56 +02:00
|
|
|
'()
|
|
|
|
(lambda (node acc depth)
|
2020-08-31 15:59:48 +02:00
|
|
|
(if (or (maybe-nil? node)
|
2020-08-31 15:44:53 +02:00
|
|
|
(tree-leaf? node))
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-cons depth acc)
|
2019-10-09 13:13:56 +02:00
|
|
|
acc))
|
|
|
|
xs)))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Generators
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; TODO: Consider parameterizing height, forced min-max branching, random
|
|
|
|
;; distributions, etc.
|
|
|
|
|
|
|
|
;; TODO: Bail out before stack overflowing by consider branching, current-depth.
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(cl-defun tree-random (&optional (value-fn (lambda (_) nil))
|
2019-10-09 13:13:56 +02:00
|
|
|
(branching-factor 2))
|
|
|
|
"Randomly generate a tree with BRANCHING-FACTOR using VALUE-FN to compute the
|
|
|
|
node values. VALUE-FN is called with the current-depth of the node. Useful for
|
|
|
|
generating test data. Warning this function can overflow the stack."
|
|
|
|
(cl-labels ((do-random
|
|
|
|
(d vf bf)
|
|
|
|
(make-node
|
|
|
|
:value (funcall vf d)
|
2020-09-01 11:17:43 +02:00
|
|
|
:children (->> (series/range 0 (number-dec bf))
|
|
|
|
(list-map
|
2019-10-09 13:13:56 +02:00
|
|
|
(lambda (_)
|
2020-08-31 15:51:27 +02:00
|
|
|
(when (random-boolean?)
|
2019-10-09 13:13:56 +02:00
|
|
|
(do-random d vf bf))))))))
|
|
|
|
(do-random 0 value-fn branching-factor)))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Predicates
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-instance? (tree)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if TREE is a tree struct."
|
|
|
|
(node-p tree))
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-leaf? (node)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if NODE has no children."
|
2020-08-31 15:59:48 +02:00
|
|
|
(maybe-nil? (node-children node)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defun tree-balanced? (n xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if the tree, XS, is balanced.
|
|
|
|
A tree is balanced if none of the differences between any two depths of two leaf
|
|
|
|
nodes in XS is greater than N."
|
|
|
|
(> n (->> xs
|
2020-08-31 15:44:53 +02:00
|
|
|
tree-leaf-depths
|
2020-09-01 11:17:43 +02:00
|
|
|
set-from-list
|
|
|
|
set-count
|
|
|
|
number-dec)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Tests
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-08-31 15:44:53 +02:00
|
|
|
(defconst tree-enable-testing? t
|
2019-10-09 13:13:56 +02:00
|
|
|
"When t, test suite runs.")
|
|
|
|
|
|
|
|
;; TODO: Create set of macros for a proper test suite including:
|
|
|
|
;; - describe (arbitrarily nestable)
|
|
|
|
;; - it (arbitrarily nestable)
|
|
|
|
;; - line numbers for errors
|
|
|
|
;; - accumulated output for synopsis
|
|
|
|
;; - do we want describe *and* it? Why not a generic label that works for both?
|
2020-08-31 15:44:53 +02:00
|
|
|
(when tree-enable-testing?
|
|
|
|
(let ((tree-a (tree-node 1
|
|
|
|
(list (tree-node 2
|
|
|
|
(list (tree-node 5)
|
|
|
|
(tree-node 6)))
|
|
|
|
(tree-node 3
|
|
|
|
(list (tree-node 7)
|
|
|
|
(tree-node 8)))
|
|
|
|
(tree-node 4
|
|
|
|
(list (tree-node 9)
|
|
|
|
(tree-node 10))))))
|
|
|
|
(tree-b (tree-node 1
|
|
|
|
(list (tree-node 2
|
|
|
|
(list (tree-node 5)
|
|
|
|
(tree-node 6)))
|
|
|
|
(tree-node 3)
|
|
|
|
(tree-node 4
|
|
|
|
(list (tree-node 9)
|
|
|
|
(tree-node 10)))))))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; instance?
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-assert (tree-instance? tree-a))
|
|
|
|
(prelude-assert (tree-instance? tree-b))
|
|
|
|
(prelude-refute (tree-instance? '(1 2 3)))
|
|
|
|
(prelude-refute (tree-instance? "oak"))
|
2019-10-09 13:13:56 +02:00
|
|
|
;; balanced?
|
2020-08-31 18:05:31 +02:00
|
|
|
(prelude-assert (tree-balanced? 1 tree-a))
|
|
|
|
(prelude-refute (tree-balanced? 1 tree-b))
|
2019-10-09 13:13:56 +02:00
|
|
|
(message "Tests pass!")))
|
|
|
|
|
|
|
|
(provide 'tree)
|
|
|
|
;;; tree.el ends here
|