Posts

Post has attachment

**99 Clojure Problems – 64: Layout a Binary Tree (1)**

"Given a binary tree as defined before being either a vector of the form [v l r] or nil.

As a preparation for drawing the tree, a layout algorithm is required to

determine the position of each node in a rectangular grid. In this

layout strategy, the ...

Add a comment...

Post has attachment

Post has attachment

**99 Clojure Problems – 63: Construct a Complete Binary Tree**

"A complete binary tree with height H is defined as follows: The levels 1,2,3,...,H-1 contain the maximum number of nodes (i.e 2(i-1) at the level i, note that we start counting the levels from 1 at the root). In level H, which may contain less than the max...

Add a comment...

Post has attachment

Post has attachment

Add a comment...

Post has attachment

**99 Clojure Problems – 62B: Collect the Nodes at a Given Level in a List.**

"A node of a binary tree is at level N if the path from the root to the

node has length N-1. The root node is at level 1. Write a method

at-level to collect all nodes at a given level in a list." (deftest p62b-at-level

(is (= '(b c) (at-level '[a [b n...

Add a comment...

Post has attachment

**99 Clojure Problems – 61A: Collect the Leaves of a Binary Tree in a List**

"61A (*) Collect the leaves of a binary tree in a list.

A leaf is a node with no successors. Write a method leaves to

collect them in a list." (deftest p61a-leaves

(is (= '(b d e) (leaves '[a [b nil nil] [c [d nil nil] [e nil nil]]])))) A variation on...

Add a comment...

Post has attachment

**99 Clojure Problems – 62: Collect the Internal Nodes of Binary Tree in a List**

"An internal node of a binary tree has either one or two non-empty successors. Write a method internals to collect them in a list." (deftest p62-internals

(is (= '(a c) (internals '[a [b nil nil] [c [d nil nil] [e nil nil]]])))) I used the same idea as in...

Add a comment...

Post has attachment

**99 Clojure Problems – 61: Count the Leaves of a Binary Tree.**

"A leaf is a node with no successors. Write a method leafCount to count them." REPL Session ninety-nine-clojure.bintrees> (first (all-hbal-trees 5 'x))

[x [x [x nil nil] nil] [x [x nil nil] nil]]

ninety-nine-clojure.bintrees> (leaf-count *1)

2 I simple walk...

Add a comment...

Post has attachment

**99 Clojure Problems – 60 (alternative solution): Construct Height-balanced Binary Trees with a Given Number of Nodes.**

This post describes an alternative solution to problem 60 based on logic programming. Check out my previous post for a functional Clojure solution. The original 99 problems were compiled to teach Prolog. Clojure's core.logic implements a minimal logic DSL ...

Add a comment...

Wait while more posts are being loaded