## Nth Smallest Item In Binary Search Tree

### February 24, 2017

We begin by writing a simple implementation of a binary search tree represented as a list of three items with the item value in the `car`

, the left subtree in the `cadr`

, and the right subtree in the `caddr`

. Here are `insert`

and `member?`

:

(define (insert lt? x tree) (cond ((null? tree) (list x (list) (list))) ((lt? x (car tree)) (list (car tree) (insert lt? x (cadr tree)) (caddr tree))) ((lt? (car tree) x) (list (car tree) (cadr tree) (insert lt? x (caddr tree)))) (else tree)))

(define (member? lt? x tree) (cond ((null? tree) #f) ((lt? x (car tree)) (member? lt? x (cadr tree))) ((lt? (car tree) x) (member? lt? x (caddr tree))) (else #t)))

Here’s a sample tree and a demonstration of `insert`

and `member?`

:

(define tree (insert < 4 (insert < 6 (insert < 0 (insert < 7 (insert < 1 (insert < 5 (insert < 2 (insert < 3 (list))))))))))

3 | +----+--------+ | | 2 5 | | +----+ +---+------+ | | | 1 4 7 | | +---+ +--+ | | 0 6

> tree (3 (2 (1 (0 () ()) ()) ()) (5 (4 () ()) (7 (6 () ()) ()))) > (do ((ns (range 9) (cdr ns))) ((null? ns)) (assert (member? < (car ns) tree) #t)) failed assertion: (member? < (car ns) tree) expected: #t returned: #f

The failed assertion results from searching for 8, which is not present in the tree, so the failure is expected.

An easy solution to the exercise flattens the tree into a list, then takes the *n*th item in the list:

(define (enlist tree) (cond ((null? tree) (list)) ((and (null? (cadr tree)) (null? (caddr tree))) (list (car tree))) (else (append (enlist (cadr tree)) (list (car tree)) (enlist (caddr tree))))))

(define (nth n tree) (list-ref (enlist tree) n))

> (enlist tree) (0 1 2 3 4 5 6 7) > (do ((ns (range 9) (cdr ns))) ((null? ns)) (assert (nth (car ns) tree) (car ns))) Exception in list-ref: index 8 is out of range for list (0 1 2 3 4 5 ...) Type (debug) to enter the debugger.

Instead of a failed assertion, this function throws an error when *n* is out of range, which is unfriendly.

That algorithm takes quadratic time. A better solution uses depth-first search to perform an in-order traversal of the tree, counting nodes each time it visits the root of a subtree, and returning when it sees the desired item:

(define (nth n tree) (call-with-current-continuation (lambda (return) (let ((n n)) (let loop ((tree tree)) (when (pair? tree) (loop (cadr tree)) (when (zero? n) (return (car tree))) (set! n (- n 1)) (loop (caddr tree)))) (return #f)))))

> (do ((ns (range 9) (cdr ns))) ((null? ns)) (assert (nth (car ns) tree) (car ns))) failed assertion: (nth (car ns) tree) expected: 8 returned: #f

The failed assertion is expected when *n* is beyond the range of the tree.

We don’t often see `call-with-current-continuation`

, though it is a valid and important part of Scheme. Here we use `call-with-current-continuation`

to break the depth-first recursion and `return`

the desired node as soon as it is seen. Time complexity of the algorithm is O(*n* log *N*), where *N* is the unknown number of nodes in the tree and *n* is the requested node.

You can run the program at http://ideone.com/lrdIfu.

In Common Lisp. Here is a trivial solution. Here nth-element is O(n). To obtain a solution in O(log(n)), we could add an index to the node, and use an insertion function to build a balanced tree O(log(n)), and a function to update the indices (O(n)).

I managed to find a version of a Python-like generator mechanism in Scheme that I wrote some years back. This can turn any code that calls a given procedure repeatedly into a procedure that returns the successive values on demand. Here that code is the inorder walk which can then be run step by step.

Test results show the full inorder traversal and then each value obtained by running a similar walk to generate only the desired number of values:

@Jussi: There is a generator in the Standard Prelude.

@Praxis, here’s a comparison where I use first my generator, then your Standard Prelude define-generator to display the first two values from a separately implemented inorder traversal (from my previous entry above). First I thought you would need to wrap your yield keyword in a lambda, whereas I don’t have any keywords at all:

but then I realized that there are no restrictions to where in the body it can occur, and indeed this works:

Nice.

Same in Python. It’s more colourful, but there’s no way to just use an existing higher-order traversal for this. There would also be no way to implement the generator-function mechanism if it wasn’t built in. On the other hand, it is built in. And the “yield from” feature was added when the need was felt.

A solution in Racket.