21

2

# Background

A binary tree is a rooted tree whose every node has at most two children.

A labelled binary tree is a binary tree whose every node is labelled with a positive integer; moreover, all labels are distinct.

A BST (binary search tree) is a labelled binary tree in which the label of each node is greater than the labels of all the nodes in its left subtree, and smaller than the labels of all the nodes in its right subtree. For instance, the following is a BST:

The pre-order traversal of a labelled binary tree is defined by the following pseudo-code.

function preorder(node)
if node is null then
return
else
print(node.label)
preorder(node.left)
preorder(node.right)


See the following image to get a better intuition:

The vertices of this binary tree are printed in the following order:

F, B, A, D, C, E, G, I, H


# Challenge

Given a list of integers $$\a\$$, your task is to determine whether there is a BST whose pre-order traversal prints exactly $$\a\$$.

## Input

• A non-empty list of distinct positive integers $$\a\$$.
• Optionally, the length of $$\a\$$.

## Output

• A truthy value if $$\a\$$ is the pre-order traversal of some BST.
• A falsey value otherwise.

# Rules

• Standard rules for valid submissions, I/O, loopholes apply.
• This is , so shortest solution (in bytes) wins. As usual, don't let ridiculously short solutions in golfy languages discourage you from posting a longer answer in your language of choice.
• This is not a rule, but your answer will be better received if it includes a link to test the solution and an explanation of how it works.

# Examples

Input                   ---->   Output

[1]                     ---->   True
[1,2,3,4]               ---->   True
[5,1,4,2,3]             ---->   True
[5,4,3,2,1,6,7,8,9]     ---->   True
[4,2,1,3,6,5,7]         ---->   True
[8,3,1,6,4,7,10,14,13]  ---->   True
[2,3,1]                 ---->   False
[6,3,2,4,5,1,8,7,9]     ---->   False
[1,2,3,4,5,7,8,6]       ---->   False
[3,1,4,2]               ---->   False


Check out this link (courtesy of Kevin Cruijssen) to have a visual look at the examples.

1Here a pastebin of the test cases to have a more visual look as to how they are truthy/falsey. Nice challenge btw, +1 from me. – Kevin Cruijssen – 2018-10-19T08:21:11.287

May we assume all integers are positive? – G B – 2018-10-19T10:46:14.107

@GB Yes. I will edit the post now. – Delfad0r – 2018-10-19T11:01:12.603

11

# JavaScript (Node.js), 49 bytes

a=>!a.some((p,i)=>a.some((q,j)=>q>p&a[j+=j>i]<p))


Try it online!

Using the fact that for array $$\a_0 ... a_{n-1}\$$, $$\a\$$ is the pre-order traversal of some BST iff $$\ \forall 0\le i holds.

Thanks to Arnauld, save 1 byte.

8

# Jelly, 7 bytes

ŒPŒ¿€4ḟ


Try it online!

Returns [4] for traversals, otherwise [].

Essentially uses tsh's algorithm: the "disqualifying" condition for a pre-order traversal is a subsequence of 3 elements that looks like [mid, high, low]. (For example, [20, 30, 10].)

We equivalently check for any subsequences of any length that have index 4 in their permutation lists, which are all lists sorted like [a1 … ak c d b] where the ai are sorted and ai < b < c < d. (Each such list is disqualifying if we look at the last three elements, and each disqualifying list is obviously of this form.)

ŒP          All subsequences.
Œ¿€       Permutation index of each.
4ḟ     Set difference of {4} and this list.


# Proof

## A pre-order traversal contains no disqualifying subsequences

Base case: traversal(•) is the empty list. ✓

Induction: traversal(t) is: t.root ++ traversal(t.left) ++ traversal(t.right).

Let [a, b, c] be a subsequence hereof. We'll show c < a < b is impossible.

• If t.root = a, then c < a < b requires c ∈ t.left and b ∈ t.right, so [a, b, c] is the wrong order.
• If a, b, c ∈ t.left or a, b, c ∈ t.right, use the induction hypothesis.
• If a ∈ t.left and c ∈ t.right then c > a.

## A list of distinct integers without disqualifying subsequences is the pre-order traversal of a BST

If the list is empty, it's the traversal of the trivial BST •.

If the list is head followed by tail:

• Let less be the longest prefix of tail of elements less than head, and let more be the rest of the list.
• Then more[1] > head, and all other more[i] are greater than head too (otherwise [head, more[1], more[i]] would be a disqualifying subsequence).
• Recurse: turn less and more into BSTs.
• Now our list is the traversal of

                 head
/    \
BST(less)   BST(more),


and this tree is a valid BST.

1Nice proof. Actually I didn’t proved the formula when I post the answer. I just felt that it is correct after some attempts to construct the BST from input. – tsh – 2018-10-20T03:27:25.327

5

# Java 10, 94 bytes

a->{var r=0>1;for(int j=a.length-1,i;j-->0;)for(i=0;i<j;)r|=a[j]>a[i]&a[j+1]<a[i++];return!r;}


Try it online.

Explanation:

a->{                      // Method with integer-array parameter and boolean return-type
var r=0>1;              //  Result-boolean, starting at false
for(int j=a.length-1,i;j-->0;)
//  Loop j in the range (length-1, 0]:
for(i=0;i<j;)         //   Inner loop i in the range [0, j):
r|=                 //    If any are true, change the result to true as well:
a[j]>a[i]        //     The j'th item is larger than the i'th item
&a[j+1]<a[i++];  //     And the j+1'th item is smaller than the i'th item
return!r;}              //  After the nested loop, check if the boolean is still false


1TIL that Java booleans can be reassigned with |=. I assume &= would also work? – J. Sallé – 2018-10-19T13:14:08.400

@J.Sallé Yep, both |= and &= work as shortcuts for b = b | condition and b = b & condition (where the & and | are shortcuts for && and || in most cases of course). – Kevin Cruijssen – 2018-10-19T13:33:22.653

5

# Ruby, 46 40 38 bytes

f=->r{a,*b=r;!a||b==b&[*0..a]|b&&f[b]}


Try it online!

This works by recursively taking the first element a as a pivot, and checking if the rest of the array can be split in two (using intersection and union: first remove all elements >a, then add them again on the right and check if something has changed).

3

# Retina 0.8.2, 31 bytes

\d+
$* M\b((1+)1+,).*\1\2\b ^0  Try it online! Link includes test cases. Uses @tsh's algorithm. Explanation: \d+$*


Convert to unary.

M\b((1+)1+,).*\1\2\b


Find numbers that lie between two subsequent consecutive descending numbers.

^0


Check that the number of matches is zero.

3

# Perl 6, 38 bytes

!*.combinations(3).grep:{[>] .[1,0,2]}


Try it online!

### Explanation

 *.combinations(3)  # All combinations of 3 elements a,b,c
!                 .grep:{            }  # Return false if check succeeds for any combination
[>] .[1,0,2]   # Check whether b>a>c, that is b>a and c<a


3

f[]=1<3
f(x:r)|(a,b)<-span(<x)r=all(>x)b&&f a&&f b


Try it online!

3

# Scala (68 67 Bytes)

def%(i:Seq[Int])= !i.combinations(3).exists(c=>c(0)<c(1)&c(0)>c(2))


Try it online

# Scala (122 103 bytes)

def f(i:Seq[Int]):Boolean=if(i.size<1)1>0 else{val(s,t)=i.tail.span(_<i(0));t.forall(_>i(0))&f(s)&f(t)}


Thanks to @Laikoni for the suggestions to make both solutions shorter.

Try it online

### Explanation:

1. slice (using Scala's span) the array using array's head as the slicing criteria.
2. Confirm that the first slice of the array is less than the head and the second slice is greater than the head.
3. recursively check that each slice also satisfies (2)

1I think you don't need the space in val (s,t), true can be 1>0 and you can drop s.forall(_<i(0))& as this should already be insured by span. – Laikoni – 2018-10-20T00:46:16.640

1You can call the function % and drop the space: def%(i:Seq[Int])= – Laikoni – 2018-10-20T13:07:33.270

Your solutions contain declaration of the function unlike some other. Pure expressions are quite shorter. ;) – Dr Y Wit – 2018-10-23T10:13:39.487

I was trying to port tsh's answer, but did not manage to get it short enough. Version 1. l.zipWithIndex.foldLeft(1>0){case(r,v,i)=>r&l.zip(l.tail).slice(i+1,l.length).forall(x=>l(i)>x._1|l(i)<x._2)}. Version 2. (for(i<-l.indices)yield l.zip(l.tail).slice(i+1,l.length).forall(x =>l(i)>x._1|l(i)<x._2)).forall(x=>x). Any ideas how to make these shorter? – Dr Y Wit – 2018-10-23T10:26:13.803

Algorithm in plain English: for each element do a check with all the pairs of elements going next to each other. – Dr Y Wit – 2018-10-23T10:29:58.537

Correction case(r,v,i) should be case(r,(v,i)). – Dr Y Wit – 2018-10-23T10:34:15.547

Last try is 100 byte long expression l.indices.foldLeft(1>0)((r,i)=>r&(l.zip(l.tail).slice(i+1,l.length).forall(x=>l(i)>x._1|l(i)<x._2))). Sorry for spam. :) – Dr Y Wit – 2018-10-23T10:54:50.930

@DrYWit, nice one. I am not 100% sure if I should include the function declaration or not in the character count. I think either some form of reading input (a full program) or a function with the full body should be used for the count. – jrook – 2018-10-23T14:50:13.323

Why simply not def%(i:Seq[Int])={val(s,t)=i.tail.span(_<i(0));t.forall(_>i(0))&f(s)&f(t)}? It works fine for empty lists as well as for lists shorter than 3 elems. – Dr Y Wit – 2018-10-23T15:46:01.137

@DrYWit, because Scala needs result type for recursive functions or it won't compile. – jrook – 2018-10-23T15:49:15.690

Sorry, yup result type is required but not an if expression. – Dr Y Wit – 2018-10-23T16:01:59.960

2

# 05AB1E, 15 10 bytes

ŒεD{3.IÊ}P


-5 bytes thanks to @Emigna.

Explanation:"

Œ             # Take all sublists of the (implicit) input-list
#  i.e. [2,3,1] → [[2],[2,3],[2,3,1],[3],[3,1],[1]]
#  i.e. [1,2,3,4]
#   → [[1],[1,2],[1,2,3],[1,2,3,4],[2],[2,3],[2,3,4],[3],[3,4],[4]]
ε      }     # Map each to:
D           #  Duplicate the current sublist on the stack
{          #  Sort the copy
#   i.e. [2,3,1] → [1,2,3]
#   i.e. [2,3,4] → [2,3,4]
3.I       #  Get the 4th (3rd 0-indexed) permutation of this list
#   i.e. [1,2,3] → [2,3,1]
#   i.e. [2,3,4] → [3,4,2]
Ê      #  Check that the lists are NOT equal
#   i.e. [2,3,1] and [2,3,1] → 0 (falsey)
#   i.e. [2,3,4] and [3,4,2] → 1 (truthy)
P    # Check whether all are truthy (and output implicitly)
#  i.e. [1,1,0,1,1,1] → 0 (falsey)
#  i.e. [1,1,1,1,1,1,1,1,1,1] → 1 (truthy)


1How about ŒεD{3.IÊ}P ? – Emigna – 2018-10-19T14:20:27.613

1@Emigna Yeah, that would be indeed a lot easier... >.> Thanks! :) (And have a nice weekend.) – Kevin Cruijssen – 2018-10-19T18:39:41.123

2

f(h:t)=t==[]||all(>h)(snd$span(<h)t)&&f t  Try it online! Uses Lynn's observation that it suffices to check that there's no subsequence of the for mid..high..low. This means that for each element h, the list of elements t that comes after is a block of elements <h followed by a block of elements >h (both blocks may be empty). So, the code checks that after we drop the prefix of elements <h in t, the remaining elements are all >h. Recursing checks this for each initial element h until the list is length 1. A potential simplification is that it suffices to check for subpatterns mid..high,low where the last two are consecutive. Unfortunately, Haskell's doesn't have an short way to extract the last two elements, as can be done from the front with a pattern match a:b:c. I found a shorter solution that checks for mid,high..low, but this fails to reject inputs like [3,1,4,2]. Formatted test cases taken from Laikoni. 1 # Japt, 14 bytes d@sY ð_§XÃxÈ-Y  Japt Interpreter Outputs false for BST, true for no BST. Explanation: d@ Run on each item X, return true if any aren't 0: sY Ignore the numbers before this index ð_§XÃ Get the indexes of numbers less than or equal to X If it is a BST, this list will be e.g. [0,1,2...] -Y Subtract the position within the index list from each index eg. [0,1,2] -> [0,0,0] , [0,1,4] -> [0,0,2] xÈ Sum the resulting array  1 # Scala All approaches are implementations of the rule shown by tsh. # 109 l.zipWithIndex.foldLeft(1>0){case(r,(v,i))=>r&l.zip(l.tail).slice(i+1,l.size).forall(x=>l(i)>x._1|l(i)<x._2)}  # 101 (for(i<-l.indices)yield l.zip(l.tail).slice(i+1,l.size).forall(x =>l(i)>x._1|l(i)<x._2)).forall(x=>x)  # 98 l.indices.foldLeft(1>0)((r,i)=>r&(l.zip(l.tail).slice(i+1,l.size).forall(x=>l(i)>x._1|l(i)<x._2)))  # 78 (for(i<-l.indices;j<-i+1 to l.size-2)yield l(i)>l(j)|l(i)<l(j+1)).forall(x=>x)  If it must be a function and not just an expression then each line have to start with (17 bytes) def%(l:Seq[Int])=  0 # Oracle SQL, 177 bytes with r(i,v)as(select rownum,value(t)from table(a)t) select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r,(select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i  Since there is no boolean type in Oracle SQL, query returns either 1 or 0. # Oracle SQL 12c, 210 bytes with function f(n ku$_objnumset,i int)return int as begin return n(i);end;
select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end)from(select value(t)c from table(powermultiset_by_cardinality(a,3))t)


It's not possible access element of the array in SQL the same way as in PL/SQL - i.e. a(i), thus function f was declared in with clause for that purpose. Otherwise solution would have been much shorter.

Other limitations

• throws an exception for arrays shorter than 3 elements (instead of returning 1)
• there is an assumption that powermultiset_by_cardinality preserves order even though it's not explicitly stated in the documentation

sqlplus listing

SQL> set heading off
SQL> with r(i,v)as(select rownum,value(t)from table(ku$_objnumset(6,3,2,4,5,1,8,7,9))t) 2 select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r, 3 (select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i 4 / 0 SQL> with function f(n ku$_objnumset,i int)return int as begin return n(i);end;
2  select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end)
3  from(select value(t)c from table(powermultiset_by_cardinality(ku$_objnumset(6,3,2,4,5,1,8,7,9),3))t) 4 / 0 SQL> with r(i,v)as(select rownum,value(t)from table(ku$_objnumset(8,3,1,6,4,7,10,14,13))t)
2  select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r,
3  (select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i
4  /

1

SQL> with function f(n ku$_objnumset,i int)return int as begin return n(i);end; 2 select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end) 3 from(select value(t)c from table(powermultiset_by_cardinality(ku$_objnumset(8,3,1,6,4,7,10,14,13),3))t)
4  /

1


Online verification apex.oracle.com

Update

# Oracle SQL, 155 bytes

with r(i,v)as(select rownum,value(t)from table(a)t)select nvl(min(case when a.v<b.v and a.v>c.v then 0end),1)r from r a,r b,r c where a.i<b.i and b.i+1=c.i


0

# C, 823 bytes (not counting whitespace characters); 923 bytes (including whitespace)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tree
{struct tree * left;struct tree * right;int val;}tree;static int * test_p = 0;
void insert_root(tree ** root, int in)
{if (*root == NULL){*root = (tree *)calloc(1,sizeof(tree));(*root)->val = in;return;}else if (in < (*root)->val){insert_root(&((*root)->left),in);}else{insert_root(&((*root)->right),in);}}
void preorder(tree * root)
{if ( root == 0x0 ) { return; }*test_p++ = root->val;preorder(root->left);preorder(root->right);}
int main(int argc, char ** argv)
{int test_list[argc-1];memset(test_list,0,argc*sizeof(int));test_p = test_list;tree * root = (tree *)calloc(1,sizeof(tree));root->val = strtol(argv[1],0x0,10);int i = 1;while ( argv[++i] != 0x0 ){insert_root(&root,strtol(argv[i],0x0,10));}preorder(root);test_p = test_list;i = 1;while ( ( i < argc ) ){if ( *test_p != strtol(argv[i],0x0,10) ){return 0;}test_p++;i++;}return 1;}


The readable version of the program is below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct tree
{
struct tree * left;

struct tree * right;

int val;

} tree;

static int * test_p = 0;

void insert_root(tree ** root, int in)
{
if (*root == NULL)
{
*root = (tree *)calloc(1,sizeof(tree));

(*root)->val = in;

return;
}

else if (in < (*root)->val)
{
insert_root(&((*root)->left),in);
}

else
{
insert_root(&((*root)->right),in);
}
}

void preorder(tree * root)
{
if ( root == 0x0 ) {  return; }

*test_p++ = root->val;

preorder(root->left);

preorder(root->right);

}

int main(int argc, char ** argv)
{
int test_list[argc-1];

memset(test_list,0,argc*sizeof(int));

test_p = test_list;

tree * root = (tree *)calloc(1,sizeof(tree));

root->val = strtol(argv[1],0x0,10);

int i = 1;

while ( argv[++i] != 0x0 )
{
insert_root(&root,strtol(argv[i],0x0,10));
}

preorder(root);

test_p = test_list;

i = 1;

while ( ( i < argc ) )
{
if ( *test_p != strtol(argv[i],0x0,10) )
{
return 0;
}

test_p++;

i++;
}

return 1;
}


The main method in this program reads the list of numbers that are (allegedly) a legitimate preorder traversal.

The function insert_root that is called inserts the integers into a binary search tree where previous nodes contain lesser values and next nodes contain larger int values.

The function preorder(root) traverses the tree in a preorder traversal and simultaneously concatenates each integer the algorithm comes across to the int array test_list.

A final while loop tests if each of the int values in the stdin list and those in test_list are equivalent at each index. If there is a list element from stdin that fails to match the corresponding element in test_list at the respective index, the main function returns 0. Else the main method returns 1.

To determine what main returned, type echo \$status into the bash terminal. BASH will either print out a 1 or a 0.

2Your score is the one counting whitespace. – Post Rock Garf Hunter – 2019-08-03T10:18:06.377