<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.categories.acsl.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Williamhu</id>
	<title>ACSL Category Descriptions - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://www.categories.acsl.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Williamhu"/>
	<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Special:Contributions/Williamhu"/>
	<updated>2026-06-10T03:12:42Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=640</id>
		<title>LISP</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=640"/>
		<updated>2019-07-15T00:07:02Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: Fixed inconsistencies with curly/straight quotes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful.  It was developed in the mid-1950&amp;#039;s by John McCarthy at M.I.T. as a &amp;quot;LISt Processing language.&amp;quot;  It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment.  LISP presents a very different way to think about programming from the &amp;quot;algorithmic&amp;quot; languages, such as Python, C++, and Java.&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
As its name implies, the basis of LISP is a list.  One constructs a list by enumerating elements inside a pair of parentheses.  For example, here is a list with four elements (the second element is also a list):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;(23 (this is easy) hello 821)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The elements in the list, which are not lists, are called &amp;quot;atoms.&amp;quot;  For example, the atoms in the list above are: 23, &amp;#039;this, &amp;#039;hello, 821, &amp;#039;easy, and &amp;#039;is.  Literals are identified with a single leading quote.  Everything in LISP is either an atom or a list (but not both).  The only exception is &amp;quot;NIL,&amp;quot; which is both an atom and a list.  It can also be written as &amp;quot;()&amp;quot; – a pair of parentheses with nothing inside.&lt;br /&gt;
&lt;br /&gt;
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn).  To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments.  For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35.  Some functions have an arbitrary number of arguments; others require a fixed number.  All statements return a value, which is either an atom or a list.&lt;br /&gt;
&lt;br /&gt;
== Basic Functions (SET, SETQ, EVAL, ATOM) ==&lt;br /&gt;
&lt;br /&gt;
We may assign values to variables using the function SET.  For example, the statement (SET &amp;#039;test 6) would have a value of a 6, and (more importantly, however) would also cause the atom &amp;#039;test to be bound to the atom 6.  The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted.  &lt;br /&gt;
Observe the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SET &amp;#039;a (MULT 2 3))&lt;br /&gt;
|6&lt;br /&gt;
|a is an atom with a value of 6&lt;br /&gt;
|-&lt;br /&gt;
|(SET &amp;#039;a &amp;#039;(MULT 2 3))&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|a is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SET &amp;#039;b &amp;#039;a)&lt;br /&gt;
|a&lt;br /&gt;
|b is an atom with a value of the character a&lt;br /&gt;
|-&lt;br /&gt;
|(SET &amp;#039;c a)&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|c is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ EX (ADD 3 (MULT 2 5)))&lt;br /&gt;
|13&lt;br /&gt;
|The variable EX has a value of 13&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ VOWELS &amp;#039;(A E I O U))&lt;br /&gt;
|(A E I O U)&lt;br /&gt;
|VOWELS is a list of 5 elements&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The function EVAL returns the value of its argument, after it has been evaluated.  For example, (SETQ z &amp;#039;(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL &amp;#039;z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed).  In this last example, you can think of z being &amp;quot;resolved&amp;quot; twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments.  The function ATOM can be used to determine whether an item is an atom or a list. It returns either &amp;quot;true&amp;quot;, or &amp;quot;NIL&amp;quot; for false. Consider the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SETQ p &amp;#039;(ADD 1 2 3 4))&lt;br /&gt;
|(ADD 1 2 3 4)&lt;br /&gt;
|p is a list with 5 elements&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM &amp;#039;p)&lt;br /&gt;
|true&lt;br /&gt;
|The argument to ATOM is the atom p&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM p)&lt;br /&gt;
|NIL&lt;br /&gt;
|Because p is not quoted, it is evaluated to the 5-element list.&lt;br /&gt;
|-&lt;br /&gt;
|(EVAL p)&lt;br /&gt;
|10&lt;br /&gt;
|The argument to EVAL is the value of p; the value of p is 10. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== List Functions (CAR, CDR, CONS, REVERSE)==&lt;br /&gt;
&lt;br /&gt;
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed.  The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list).  The function CONS takes two arguments, of which the second must be a list.  It returns a list which is composed by placing the first argument as the first element in the second argument&amp;#039;s list.  The function REVERSE returns a list which is its arguments in reverse order.  &lt;br /&gt;
&lt;br /&gt;
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there&amp;#039;s a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.&lt;br /&gt;
&lt;br /&gt;
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: &lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(CAR &amp;#039;(This is a list))&lt;br /&gt;
|This&lt;br /&gt;
|-&lt;br /&gt;
|(CDR &amp;#039;(This is a list))&lt;br /&gt;
|(is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(CONS &amp;#039;red &amp;#039;(white blue))&lt;br /&gt;
|(red white blue)&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ z (CONS &amp;#039;(red white blue) (CDR &amp;#039;(This is a list))))&lt;br /&gt;
|((red white blue) is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(REVERSE z)&lt;br /&gt;
|(list a is (red white blue))&lt;br /&gt;
|-&lt;br /&gt;
|(CDDAR z)&lt;br /&gt;
|(blue)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Arithmetic Functions (ADD, MULT, ...)==&lt;br /&gt;
&lt;br /&gt;
As you have probably already figured out, the function ADD simply summed its arguments.  We&amp;#039;ll also be using the following arithmetic functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Function&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Result&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD x1 x2 …)&lt;br /&gt;
|sum of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(SUB a b)&lt;br /&gt;
|a-b&lt;br /&gt;
|-&lt;br /&gt;
|(MULT x1 x2 …)&lt;br /&gt;
|product of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(DIV a b)&lt;br /&gt;
|a/b&lt;br /&gt;
|-&lt;br /&gt;
|(SQUARE a)&lt;br /&gt;
|a*a&lt;br /&gt;
|-&lt;br /&gt;
|(EXP a n)&lt;br /&gt;
|a&amp;lt;sup&amp;gt;n&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|(EQ a b)&lt;br /&gt;
|true if a and b are equal, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(POS a)&lt;br /&gt;
|true if a is positive, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(NEG a)&lt;br /&gt;
|true if a is negative, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /.  Here are some examples of these functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))&lt;br /&gt;
|24.5&lt;br /&gt;
|-&lt;br /&gt;
|(- (* 3 2) (- 12 (+ 1 2 1)))&lt;br /&gt;
| -2&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (SQUARE 3) (SQUARE 4))&lt;br /&gt;
|25&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== User-defined Functions ==&lt;br /&gt;
&lt;br /&gt;
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.)  For example,&lt;br /&gt;
 (DEF SECOND (args) (CAR (CDR args)))&lt;br /&gt;
defines a new function called SECOND which operates on a single parameter named &amp;quot;args&amp;quot;.  SECOND will take the CDR of the parameter and then the CAR of that result.  So, for example:&lt;br /&gt;
(SECOND &amp;#039;(a b c d e))&lt;br /&gt;
would first CDR the list to give (b c d e), and then CAR that value returning the single character &amp;quot;b&amp;quot;.  Consider the following program fragment:&lt;br /&gt;
 (SETQ X &amp;#039;(a c s l))&lt;br /&gt;
 (DEF WHAT(args) (CONS args (REVERSE (CDR args))))&lt;br /&gt;
 (DEF SECOND(args) (CONS (CAR (CDR args)) NIL))&lt;br /&gt;
&lt;br /&gt;
The following chart illustrates the use of the user-defined functions WHAT and SECOND:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT X) || ((a c s l) l s c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND X) || (c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND (WHAT X)) || (l)&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT (SECOND X)) || ((c))&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Online Interpreters ==&lt;br /&gt;
&lt;br /&gt;
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from   [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, &amp;lt;code&amp;gt;(CAR (CDR x))&amp;lt;/code&amp;gt; is legal as is &amp;lt;code&amp;gt;(car (cdr x))&amp;lt;/code&amp;gt; One drawback of this interpreter is the print function changes lowercase input into uppercase. &lt;br /&gt;
 &lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression. &amp;lt;code&amp;gt;(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 (MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))&lt;br /&gt;
 (MULT 11 20  (DIV 6 -3))&lt;br /&gt;
 (MULT 11 20 -2)&lt;br /&gt;
 -440&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:  &amp;lt;code&amp;gt;(CDR &amp;#039;((2 (3))(4 (5 6) 7)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed.  The first element of the list is (2  (3)) and the list without this element is&lt;br /&gt;
((4 (5 6) 7)), a list with one element.&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Consider the following program fragment:&lt;br /&gt;
 (SETQ X &amp;#039;(RI VA FL CA TX))&lt;br /&gt;
 (CAR (CDR (REVERSE X)))&lt;br /&gt;
What is the value of the CAR expression? &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The first statement binds variable X to the list &amp;#039;(RI VA FL CA TX).&lt;br /&gt;
The REVERSE of this list is &amp;#039;(TX CA FL VA RI)&lt;br /&gt;
whose CDR is &amp;#039;(CA FL VA RI).  The CAR of this list is just the atom CA (without the quote).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
&lt;br /&gt;
Too scary of a sample problem! --marc 9/3/2018&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Given the function definitions for HY and FY as follows:&lt;br /&gt;
   (DEFUN HY(PARM) (REVERSE (CDR PARM)))&lt;br /&gt;
   (DEFUN FY(PARM) (CAR (HY (CDR PARM))))&lt;br /&gt;
What is the value of the following?&lt;br /&gt;
     (FY &amp;#039;(DO RE (MI FA) SO))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
To evaluate (FY &amp;#039;(DO RE (MI FA) SO)), we must evaluate&lt;br /&gt;
     (CAR (HY (CDR &amp;#039;(DO RE (MI FA) SO)))).&lt;br /&gt;
Thus, HY is invoked with &lt;br /&gt;
     PARM = &amp;#039;(RE (MI FA) SO), and we evaluate&lt;br /&gt;
     (REVERSE (CDR &amp;#039;(RE (MI FA) SO)))&lt;br /&gt;
This has a value of (SO (MI FA)) which is returned to FY.  FY now takes the CAR of this which is SO (without the quotes).&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jWFgmE279eQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jWFgmE279eQ &amp;#039;&amp;#039;LISP very gentle intro&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/mRpbbss48sw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/mRpbbss48sw &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/50wj_f51kBM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/50wj_f51kBM &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Completes the problem that was started in the above video. &lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=630</id>
		<title>LISP</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=630"/>
		<updated>2019-01-23T20:19:30Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* User-defined Functions */ code box&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful.  It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language.”  It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment.  LISP presents a very different way to think about programming from the “algorithmic” languages, such as Python, C++, and Java.&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
As its name implies, the basis of LISP is a list.  One constructs a list by enumerating elements inside a pair of parentheses.  For example, here is a list with four elements (the second element is also a list):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;(23 (this is easy) hello 821)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The elements in the list, which are not lists, are called &amp;quot;atoms.&amp;quot;  For example, the atoms in the list above are: 23, &amp;#039;this, &amp;#039;hello, 821, &amp;#039;easy, and &amp;#039;is.  Literals are identified with a single leading quote.  Everything in LISP is either an atom or a list (but not both).  The only exception is &amp;quot;NIL,&amp;quot; which is both an atom and a list.  It can also be written as &amp;quot;()&amp;quot; – a pair of parentheses with nothing inside.&lt;br /&gt;
&lt;br /&gt;
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn).  To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments.  For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35.  Some functions have an arbitrary number of arguments; others require a fixed number.  All statements return a value, which is either an atom or a list.&lt;br /&gt;
&lt;br /&gt;
== Basic Functions (SET, SETQ, EVAL, ATOM) ==&lt;br /&gt;
&lt;br /&gt;
We may assign values to variables using the function SET.  For example, the statement (SET ’test 6) would have a value of a 6, and (more importantly, however) would also cause the atom ‘test to be bound to the atom 6.  The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted.  &lt;br /&gt;
Observe the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ( MULT 2 3))&lt;br /&gt;
|6&lt;br /&gt;
|a is an atom with a vaue of 6&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ’(MULT 2 3))&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|a is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’b ’a)&lt;br /&gt;
|a&lt;br /&gt;
|b is an atom with a value of the character a&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’c a)&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|c is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ EX (ADD 3 (MULT 2 5)))&lt;br /&gt;
|13&lt;br /&gt;
|The variable EX has a value of 13&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ VOWELS ’(A E I O U))&lt;br /&gt;
|(A E I O U)&lt;br /&gt;
|VOWELS is a list of 5 elements&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The function EVAL returns the value of its argument, after it has been evaluated.  For example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed).  In this last example, you can think of z being “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments.  The function ATOM can be used to determine whether an item is an atom or a list. It returns either &amp;quot;true&amp;quot;, or &amp;quot;NIL&amp;quot; for false. Consider the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SETQ p &amp;#039;(ADD 1 2 3 4))&lt;br /&gt;
|(ADD 1 2 3 4)&lt;br /&gt;
|p is a list with 5 elements&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM &amp;#039;p)&lt;br /&gt;
|true&lt;br /&gt;
|The argument to ATOM is the atom p&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM p)&lt;br /&gt;
|NIL&lt;br /&gt;
|Because p is not quoted, it is evaluated to the 5-element list.&lt;br /&gt;
|-&lt;br /&gt;
|(EVAL p)&lt;br /&gt;
|10&lt;br /&gt;
|The argument to EVAL is the value of p; the value of p is 10. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== List Functions (CAR, CDR, CONS, REVERSE)==&lt;br /&gt;
&lt;br /&gt;
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed.  The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list).  The function CONS takes two arguments, of which the second must be a list.  It returns a list which is composed by placing the first argument as the first element in the second argument’s list.  The function REVERSE returns a list which is its arguments in reverse order.  &lt;br /&gt;
&lt;br /&gt;
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there&amp;#039;s a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.&lt;br /&gt;
&lt;br /&gt;
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: &lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(CAR &amp;#039;(This is a list))&lt;br /&gt;
|This&lt;br /&gt;
|-&lt;br /&gt;
|(CDR &amp;#039;(This is a list))&lt;br /&gt;
|(is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(CONS &amp;#039;red &amp;#039;(white blue))&lt;br /&gt;
|(red white blue)&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ z (CONS &amp;#039;(red white blue) (CDR &amp;#039;(This is a list))))&lt;br /&gt;
|((red white blue) is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(REVERSE z)&lt;br /&gt;
|(list a is (red white blue))&lt;br /&gt;
|-&lt;br /&gt;
|(CDDAR z)&lt;br /&gt;
|(blue)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Arithmetic Functions (ADD, MULT, ...)==&lt;br /&gt;
&lt;br /&gt;
As you have probably already figured out, the function ADD simply summed its arguments.  We’ll also be using the following arithmetic functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Function&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Result&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD x1 x2 …)&lt;br /&gt;
|sum of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(SUB a b)&lt;br /&gt;
|a-b&lt;br /&gt;
|-&lt;br /&gt;
|(MULT x1 x2 …)&lt;br /&gt;
|product of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(DIV a b)&lt;br /&gt;
|a/b&lt;br /&gt;
|-&lt;br /&gt;
|(SQUARE a)&lt;br /&gt;
|a*a&lt;br /&gt;
|-&lt;br /&gt;
|(EXP a n)&lt;br /&gt;
|a&amp;lt;sup&amp;gt;n&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|(EQ a b)&lt;br /&gt;
|true if a and b are equal, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(POS a)&lt;br /&gt;
|true if a is positive, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(NEG a)&lt;br /&gt;
|true if a is negative, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /.  Here are some examples of these functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))&lt;br /&gt;
|24.5&lt;br /&gt;
|-&lt;br /&gt;
|(- (* 3 2) (- 12 (+ 1 2 1)))&lt;br /&gt;
| -2&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (SQUARE 3) (SQUARE 4))&lt;br /&gt;
|25&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== User-defined Functions ==&lt;br /&gt;
&lt;br /&gt;
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.)  For example,&lt;br /&gt;
 (DEF SECOND (args) (CAR (CDR args)))&lt;br /&gt;
defines a new function called SECOND which operates on a single parameter named “args”.  SECOND will take the CDR of the parameter and then the CAR of that result.  So, for example:&lt;br /&gt;
(SECOND ’(a b c d e))&lt;br /&gt;
would first CDR the list to give (b c d e), and then CAR that value returning the single character “b”.  Consider the following program fragment:&lt;br /&gt;
 (SETQ X ’(a c s l))&lt;br /&gt;
 (DEF WHAT(args) (CONS args (REVERSE (CDR args))))&lt;br /&gt;
 (DEF SECOND(args) (CONS (CAR (CDR args)) NIL))&lt;br /&gt;
&lt;br /&gt;
The following chart illustrates the use of the user-defined functions WHAT and SECOND:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT X) || ((a c s l) l s c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND X) || (c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND (WHAT X)) || (l)&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT (SECOND X)) || ((c))&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Online Interpreters ==&lt;br /&gt;
&lt;br /&gt;
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from   [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, &amp;lt;code&amp;gt;(CAR (CDR x))&amp;lt;/code&amp;gt; is legal as is &amp;lt;code&amp;gt;(car (cdr x))&amp;lt;/code&amp;gt; One drawback of this interpreter is the print function changes lowercase input into uppercase. &lt;br /&gt;
 &lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression. &amp;lt;code&amp;gt;(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 (MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))&lt;br /&gt;
 (MULT 11 20  (DIV 6 -3))&lt;br /&gt;
 (MULT 11 20 -2)&lt;br /&gt;
 -440&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:  &amp;lt;code&amp;gt;(CDR ’((2 (3))(4 (5 6) 7)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed.  The first element of the list is (2  (3)) and the list without this element is&lt;br /&gt;
((4 (5 6) 7)), a list with one element.&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Consider the following program fragment:&lt;br /&gt;
 (SETQ X ’(RI VA FL CA TX))&lt;br /&gt;
 (CAR (CDR (REVERSE X)))&lt;br /&gt;
What is the value of the CAR expression? &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The first statement binds variable X to the list ‘(RI VA FL CA TX).&lt;br /&gt;
The REVERSE of this list is ‘(TX CA FL VA RI)&lt;br /&gt;
whose CDR is ‘(CA FL VA RI).  The CAR of this list is just the atom CA (without the quote).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
&lt;br /&gt;
Too scary of a sample problem! --marc 9/3/2018&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Given the function definitions for HY and FY as follows:&lt;br /&gt;
   (DEFUN HY(PARM) (REVERSE (CDR PARM)))&lt;br /&gt;
   (DEFUN FY(PARM) (CAR (HY (CDR PARM))))&lt;br /&gt;
What is the value of the following?&lt;br /&gt;
     (FY ’(DO RE (MI FA) SO))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
To evaluate (FY ’(DO RE (MI FA) SO)), we must evaluate&lt;br /&gt;
     (CAR (HY (CDR ’(DO RE (MI FA) SO)))).&lt;br /&gt;
Thus, HY is invoked with &lt;br /&gt;
     PARM = ’(RE (MI FA) SO), and we evaluate&lt;br /&gt;
     (REVERSE (CDR ’(RE (MI FA) SO)))&lt;br /&gt;
This has a value of (SO (MI FA)) which is returned to FY.  FY now takes the CAR of this which is SO (without the quotes).&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jWFgmE279eQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jWFgmE279eQ &amp;#039;&amp;#039;LISP very gentle intro&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/mRpbbss48sw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/mRpbbss48sw &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/50wj_f51kBM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/50wj_f51kBM &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Completes the problem that was started in the above video. &lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=629</id>
		<title>LISP</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=629"/>
		<updated>2019-01-21T15:30:31Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Syntax */ Replaced curly quotes with vertical ones&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful.  It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language.”  It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment.  LISP presents a very different way to think about programming from the “algorithmic” languages, such as Python, C++, and Java.&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
As its name implies, the basis of LISP is a list.  One constructs a list by enumerating elements inside a pair of parentheses.  For example, here is a list with four elements (the second element is also a list):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;(23 (this is easy) hello 821)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The elements in the list, which are not lists, are called &amp;quot;atoms.&amp;quot;  For example, the atoms in the list above are: 23, &amp;#039;this, &amp;#039;hello, 821, &amp;#039;easy, and &amp;#039;is.  Literals are identified with a single leading quote.  Everything in LISP is either an atom or a list (but not both).  The only exception is &amp;quot;NIL,&amp;quot; which is both an atom and a list.  It can also be written as &amp;quot;()&amp;quot; – a pair of parentheses with nothing inside.&lt;br /&gt;
&lt;br /&gt;
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn).  To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments.  For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35.  Some functions have an arbitrary number of arguments; others require a fixed number.  All statements return a value, which is either an atom or a list.&lt;br /&gt;
&lt;br /&gt;
== Basic Functions (SET, SETQ, EVAL, ATOM) ==&lt;br /&gt;
&lt;br /&gt;
We may assign values to variables using the function SET.  For example, the statement (SET ’test 6) would have a value of a 6, and (more importantly, however) would also cause the atom ‘test to be bound to the atom 6.  The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted.  &lt;br /&gt;
Observe the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ( MULT 2 3))&lt;br /&gt;
|6&lt;br /&gt;
|a is an atom with a vaue of 6&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ’(MULT 2 3))&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|a is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’b ’a)&lt;br /&gt;
|a&lt;br /&gt;
|b is an atom with a value of the character a&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’c a)&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|c is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ EX (ADD 3 (MULT 2 5)))&lt;br /&gt;
|13&lt;br /&gt;
|The variable EX has a value of 13&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ VOWELS ’(A E I O U))&lt;br /&gt;
|(A E I O U)&lt;br /&gt;
|VOWELS is a list of 5 elements&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The function EVAL returns the value of its argument, after it has been evaluated.  For example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed).  In this last example, you can think of z being “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments.  The function ATOM can be used to determine whether an item is an atom or a list. It returns either &amp;quot;true&amp;quot;, or &amp;quot;NIL&amp;quot; for false. Consider the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SETQ p &amp;#039;(ADD 1 2 3 4))&lt;br /&gt;
|(ADD 1 2 3 4)&lt;br /&gt;
|p is a list with 5 elements&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM &amp;#039;p)&lt;br /&gt;
|true&lt;br /&gt;
|The argument to ATOM is the atom p&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM p)&lt;br /&gt;
|NIL&lt;br /&gt;
|Because p is not quoted, it is evaluated to the 5-element list.&lt;br /&gt;
|-&lt;br /&gt;
|(EVAL p)&lt;br /&gt;
|10&lt;br /&gt;
|The argument to EVAL is the value of p; the value of p is 10. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== List Functions (CAR, CDR, CONS, REVERSE)==&lt;br /&gt;
&lt;br /&gt;
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed.  The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list).  The function CONS takes two arguments, of which the second must be a list.  It returns a list which is composed by placing the first argument as the first element in the second argument’s list.  The function REVERSE returns a list which is its arguments in reverse order.  &lt;br /&gt;
&lt;br /&gt;
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there&amp;#039;s a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.&lt;br /&gt;
&lt;br /&gt;
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: &lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(CAR &amp;#039;(This is a list))&lt;br /&gt;
|This&lt;br /&gt;
|-&lt;br /&gt;
|(CDR &amp;#039;(This is a list))&lt;br /&gt;
|(is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(CONS &amp;#039;red &amp;#039;(white blue))&lt;br /&gt;
|(red white blue)&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ z (CONS &amp;#039;(red white blue) (CDR &amp;#039;(This is a list))))&lt;br /&gt;
|((red white blue) is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(REVERSE z)&lt;br /&gt;
|(list a is (red white blue))&lt;br /&gt;
|-&lt;br /&gt;
|(CDDAR z)&lt;br /&gt;
|(blue)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Arithmetic Functions (ADD, MULT, ...)==&lt;br /&gt;
&lt;br /&gt;
As you have probably already figured out, the function ADD simply summed its arguments.  We’ll also be using the following arithmetic functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Function&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Result&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD x1 x2 …)&lt;br /&gt;
|sum of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(SUB a b)&lt;br /&gt;
|a-b&lt;br /&gt;
|-&lt;br /&gt;
|(MULT x1 x2 …)&lt;br /&gt;
|product of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(DIV a b)&lt;br /&gt;
|a/b&lt;br /&gt;
|-&lt;br /&gt;
|(SQUARE a)&lt;br /&gt;
|a*a&lt;br /&gt;
|-&lt;br /&gt;
|(EXP a n)&lt;br /&gt;
|a&amp;lt;sup&amp;gt;n&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|(EQ a b)&lt;br /&gt;
|true if a and b are equal, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(POS a)&lt;br /&gt;
|true if a is positive, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(NEG a)&lt;br /&gt;
|true if a is negative, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /.  Here are some examples of these functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))&lt;br /&gt;
|24.5&lt;br /&gt;
|-&lt;br /&gt;
|(- (* 3 2) (- 12 (+ 1 2 1)))&lt;br /&gt;
| -2&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (SQUARE 3) (SQUARE 4))&lt;br /&gt;
|25&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== User-defined Functions ==&lt;br /&gt;
&lt;br /&gt;
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.)  For example,&lt;br /&gt;
 (DEF SECOND (args) (CAR (CDR args)))&lt;br /&gt;
defines a new function called SECOND which operates on a single parameter named “args”.  SECOND will take the CDR of the parameter and then the CAR of that result.  So, for example:&lt;br /&gt;
(SECOND ’(a b c d e))&lt;br /&gt;
would first CDR the list to give (b c d e), and then CAR that value returning the single character “b”.  Consider the following program fragment:&lt;br /&gt;
:(SETQ X ’(a c s l))&lt;br /&gt;
:(DEF WHAT(args) (CONS args (REVERSE (CDR args))))&lt;br /&gt;
:(DEF SECOND(args) (CONS (CAR (CDR args)) NIL))&lt;br /&gt;
&lt;br /&gt;
The following chart illustrates the use of the user-defined functions WHAT and SECOND:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT X) || ((a c s l) l s c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND X) || (c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND (WHAT X)) || (l)&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT (SECOND X)) || ((c))&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Online Interpreters ==&lt;br /&gt;
&lt;br /&gt;
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from   [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, &amp;lt;code&amp;gt;(CAR (CDR x))&amp;lt;/code&amp;gt; is legal as is &amp;lt;code&amp;gt;(car (cdr x))&amp;lt;/code&amp;gt; One drawback of this interpreter is the print function changes lowercase input into uppercase. &lt;br /&gt;
 &lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression. &amp;lt;code&amp;gt;(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 (MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))&lt;br /&gt;
 (MULT 11 20  (DIV 6 -3))&lt;br /&gt;
 (MULT 11 20 -2)&lt;br /&gt;
 -440&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:  &amp;lt;code&amp;gt;(CDR ’((2 (3))(4 (5 6) 7)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed.  The first element of the list is (2  (3)) and the list without this element is&lt;br /&gt;
((4 (5 6) 7)), a list with one element.&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Consider the following program fragment:&lt;br /&gt;
 (SETQ X ’(RI VA FL CA TX))&lt;br /&gt;
 (CAR (CDR (REVERSE X)))&lt;br /&gt;
What is the value of the CAR expression? &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The first statement binds variable X to the list ‘(RI VA FL CA TX).&lt;br /&gt;
The REVERSE of this list is ‘(TX CA FL VA RI)&lt;br /&gt;
whose CDR is ‘(CA FL VA RI).  The CAR of this list is just the atom CA (without the quote).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
&lt;br /&gt;
Too scary of a sample problem! --marc 9/3/2018&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Given the function definitions for HY and FY as follows:&lt;br /&gt;
   (DEFUN HY(PARM) (REVERSE (CDR PARM)))&lt;br /&gt;
   (DEFUN FY(PARM) (CAR (HY (CDR PARM))))&lt;br /&gt;
What is the value of the following?&lt;br /&gt;
     (FY ’(DO RE (MI FA) SO))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
To evaluate (FY ’(DO RE (MI FA) SO)), we must evaluate&lt;br /&gt;
     (CAR (HY (CDR ’(DO RE (MI FA) SO)))).&lt;br /&gt;
Thus, HY is invoked with &lt;br /&gt;
     PARM = ’(RE (MI FA) SO), and we evaluate&lt;br /&gt;
     (REVERSE (CDR ’(RE (MI FA) SO)))&lt;br /&gt;
This has a value of (SO (MI FA)) which is returned to FY.  FY now takes the CAR of this which is SO (without the quotes).&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jWFgmE279eQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jWFgmE279eQ &amp;#039;&amp;#039;LISP very gentle intro&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/mRpbbss48sw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/mRpbbss48sw &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/50wj_f51kBM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/50wj_f51kBM &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Completes the problem that was started in the above video. &lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=628</id>
		<title>LISP</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=628"/>
		<updated>2019-01-21T03:29:40Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* List Functions (CAR, CDR, CONS, REVERSE) */  Removed extra parentheses and different quotes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful.  It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language.”  It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment.  LISP presents a very different way to think about programming from the “algorithmic” languages, such as Python, C++, and Java.&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
As its name implies, the basis of LISP is a list.  One constructs a list by enumerating elements inside a pair of parentheses.  For example, here is a list with four elements (the second element is also a list):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;(23 (this is easy) hello 821)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The elements in the list, which are not lists, are called “atoms.”  For example, the atoms in the list above are: 23, ‘this, ‘hello, 821, ‘easy, and ‘is.  Literals are identified with a single leading quote.  Everything in LISP is either an atom or a list (but not both).  The only exception is “NIL,” which is both an atom and a list.  It can also be written as “()” – a pair of parentheses with nothing inside.&lt;br /&gt;
&lt;br /&gt;
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn).  To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments.  For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35.  Some functions have an arbitrary number of arguments; others require a fixed number.  All statements return a value, which is either an atom or a list. &lt;br /&gt;
&lt;br /&gt;
== Basic Functions (SET, SETQ, EVAL, ATOM) ==&lt;br /&gt;
&lt;br /&gt;
We may assign values to variables using the function SET.  For example, the statement (SET ’test 6) would have a value of a 6, and (more importantly, however) would also cause the atom ‘test to be bound to the atom 6.  The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted.  &lt;br /&gt;
Observe the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ( MULT 2 3))&lt;br /&gt;
|6&lt;br /&gt;
|a is an atom with a vaue of 6&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ’(MULT 2 3))&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|a is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’b ’a)&lt;br /&gt;
|a&lt;br /&gt;
|b is an atom with a value of the character a&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’c a)&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|c is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ EX (ADD 3 (MULT 2 5)))&lt;br /&gt;
|13&lt;br /&gt;
|The variable EX has a value of 13&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ VOWELS ’(A E I O U))&lt;br /&gt;
|(A E I O U)&lt;br /&gt;
|VOWELS is a list of 5 elements&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The function EVAL returns the value of its argument, after it has been evaluated.  For example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed).  In this last example, you can think of z being “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments.  The function ATOM can be used to determine whether an item is an atom or a list. It returns either &amp;quot;true&amp;quot;, or &amp;quot;NIL&amp;quot; for false. Consider the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SETQ p &amp;#039;(ADD 1 2 3 4))&lt;br /&gt;
|(ADD 1 2 3 4)&lt;br /&gt;
|p is a list with 5 elements&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM &amp;#039;p)&lt;br /&gt;
|true&lt;br /&gt;
|The argument to ATOM is the atom p&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM p)&lt;br /&gt;
|NIL&lt;br /&gt;
|Because p is not quoted, it is evaluated to the 5-element list.&lt;br /&gt;
|-&lt;br /&gt;
|(EVAL p)&lt;br /&gt;
|10&lt;br /&gt;
|The argument to EVAL is the value of p; the value of p is 10. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== List Functions (CAR, CDR, CONS, REVERSE)==&lt;br /&gt;
&lt;br /&gt;
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed.  The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list).  The function CONS takes two arguments, of which the second must be a list.  It returns a list which is composed by placing the first argument as the first element in the second argument’s list.  The function REVERSE returns a list which is its arguments in reverse order.  &lt;br /&gt;
&lt;br /&gt;
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there&amp;#039;s a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.&lt;br /&gt;
&lt;br /&gt;
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: &lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(CAR &amp;#039;(This is a list))&lt;br /&gt;
|This&lt;br /&gt;
|-&lt;br /&gt;
|(CDR &amp;#039;(This is a list))&lt;br /&gt;
|(is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(CONS &amp;#039;red &amp;#039;(white blue))&lt;br /&gt;
|(red white blue)&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ z (CONS &amp;#039;(red white blue) (CDR &amp;#039;(This is a list))))&lt;br /&gt;
|((red white blue) is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(REVERSE z)&lt;br /&gt;
|(list a is (red white blue))&lt;br /&gt;
|-&lt;br /&gt;
|(CDDAR z)&lt;br /&gt;
|(blue)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Arithmetic Functions (ADD, MULT, ...)==&lt;br /&gt;
&lt;br /&gt;
As you have probably already figured out, the function ADD simply summed its arguments.  We’ll also be using the following arithmetic functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Function&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Result&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD x1 x2 …)&lt;br /&gt;
|sum of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(SUB a b)&lt;br /&gt;
|a-b&lt;br /&gt;
|-&lt;br /&gt;
|(MULT x1 x2 …)&lt;br /&gt;
|product of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(DIV a b)&lt;br /&gt;
|a/b&lt;br /&gt;
|-&lt;br /&gt;
|(SQUARE a)&lt;br /&gt;
|a*a&lt;br /&gt;
|-&lt;br /&gt;
|(EXP a n)&lt;br /&gt;
|a&amp;lt;sup&amp;gt;n&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|(EQ a b)&lt;br /&gt;
|true if a and b are equal, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(POS a)&lt;br /&gt;
|true if a is positive, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(NEG a)&lt;br /&gt;
|true if a is negative, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /.  Here are some examples of these functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))&lt;br /&gt;
|24.5&lt;br /&gt;
|-&lt;br /&gt;
|(- (* 3 2) (- 12 (+ 1 2 1)))&lt;br /&gt;
| -2&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (SQUARE 3) (SQUARE 4))&lt;br /&gt;
|25&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== User-defined Functions ==&lt;br /&gt;
&lt;br /&gt;
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.)  For example,&lt;br /&gt;
 (DEF SECOND (args) (CAR (CDR args)))&lt;br /&gt;
defines a new function called SECOND which operates on a single parameter named “args”.  SECOND will take the CDR of the parameter and then the CAR of that result.  So, for example:&lt;br /&gt;
(SECOND ’(a b c d e))&lt;br /&gt;
would first CDR the list to give (b c d e), and then CAR that value returning the single character “b”.  Consider the following program fragment:&lt;br /&gt;
:(SETQ X ’(a c s l))&lt;br /&gt;
:(DEF WHAT(args) (CONS args (REVERSE (CDR args))))&lt;br /&gt;
:(DEF SECOND(args) (CONS (CAR (CDR args)) NIL))&lt;br /&gt;
&lt;br /&gt;
The following chart illustrates the use of the user-defined functions WHAT and SECOND:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT X) || ((a c s l) l s c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND X) || (c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND (WHAT X)) || (l)&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT (SECOND X)) || ((c))&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Online Interpreters ==&lt;br /&gt;
&lt;br /&gt;
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from   [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, &amp;lt;code&amp;gt;(CAR (CDR x))&amp;lt;/code&amp;gt; is legal as is &amp;lt;code&amp;gt;(car (cdr x))&amp;lt;/code&amp;gt; One drawback of this interpreter is the print function changes lowercase input into uppercase. &lt;br /&gt;
 &lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression. &amp;lt;code&amp;gt;(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 (MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))&lt;br /&gt;
 (MULT 11 20  (DIV 6 -3))&lt;br /&gt;
 (MULT 11 20 -2)&lt;br /&gt;
 -440&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:  &amp;lt;code&amp;gt;(CDR ’((2 (3))(4 (5 6) 7)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed.  The first element of the list is (2  (3)) and the list without this element is&lt;br /&gt;
((4 (5 6) 7)), a list with one element.&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Consider the following program fragment:&lt;br /&gt;
 (SETQ X ’(RI VA FL CA TX))&lt;br /&gt;
 (CAR (CDR (REVERSE X)))&lt;br /&gt;
What is the value of the CAR expression? &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The first statement binds variable X to the list ‘(RI VA FL CA TX).&lt;br /&gt;
The REVERSE of this list is ‘(TX CA FL VA RI)&lt;br /&gt;
whose CDR is ‘(CA FL VA RI).  The CAR of this list is just the atom CA (without the quote).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
&lt;br /&gt;
Too scary of a sample problem! --marc 9/3/2018&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Given the function definitions for HY and FY as follows:&lt;br /&gt;
   (DEFUN HY(PARM) (REVERSE (CDR PARM)))&lt;br /&gt;
   (DEFUN FY(PARM) (CAR (HY (CDR PARM))))&lt;br /&gt;
What is the value of the following?&lt;br /&gt;
     (FY ’(DO RE (MI FA) SO))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
To evaluate (FY ’(DO RE (MI FA) SO)), we must evaluate&lt;br /&gt;
     (CAR (HY (CDR ’(DO RE (MI FA) SO)))).&lt;br /&gt;
Thus, HY is invoked with &lt;br /&gt;
     PARM = ’(RE (MI FA) SO), and we evaluate&lt;br /&gt;
     (REVERSE (CDR ’(RE (MI FA) SO)))&lt;br /&gt;
This has a value of (SO (MI FA)) which is returned to FY.  FY now takes the CAR of this which is SO (without the quotes).&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jWFgmE279eQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jWFgmE279eQ &amp;#039;&amp;#039;LISP very gentle intro&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/mRpbbss48sw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/mRpbbss48sw &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/50wj_f51kBM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/50wj_f51kBM &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Completes the problem that was started in the above video. &lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=627</id>
		<title>Data Structures</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=627"/>
		<updated>2019-01-10T01:46:08Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Sample Problems */ More syntaxhighlight error fixing&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;At the heart of virtually every computer program are its algorithms and its data structures.  It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate.  The bigger the data sets, the more important data structures are in various algorithms.&lt;br /&gt;
&lt;br /&gt;
This category concentrates on four of the most basic structures:  &amp;#039;&amp;#039;&amp;#039;stacks&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;queues&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;binary search trees&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;priority queues&amp;#039;&amp;#039;&amp;#039;.  Questions will cover these data structures and implicit algorithms, not specific to implementation language details.&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; is usually used to save information that will need to be processed later.  Items are processed in a “last-in, first-out” (LIFO) order.  A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed.  This is also known as “first-in, first-out” (FIFO) order.  A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question).  A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query.  One can only find out or delete the smallest element of the list.&lt;br /&gt;
&lt;br /&gt;
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates.  The rest of this page is an overview of these structures.&lt;br /&gt;
&lt;br /&gt;
== Stacks and Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; supports two operations:  PUSH and POP.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X.  If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  An analogy to this is a stack of books on a desk:  a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped).  Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Queues&amp;#039;&amp;#039; operate just like stacks, except items are removed from the bottom instead of the top.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X.  If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change:  new coins are added to the tops of the piles, and change is given from the bottom of each.  Sometimes the top and bottom of a queue are referred to as the rear and the front respectively.  Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue.  There is a similarity to the Britsh &amp;quot;queueing up&amp;quot;.  Some textbooks refer to this data structure as a “FIFO stack”.&lt;br /&gt;
&lt;br /&gt;
Consider the following sequence of 14 operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;M&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;E&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;R&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;I&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;C&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;N&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL.  After all of the operations, there are three items still on the stack:  the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom.  If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL.  There would be three items still on the queue:  N at the top and C on the bottom.  Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.&lt;br /&gt;
&lt;br /&gt;
=== Pre-2018 Syntax ===&lt;br /&gt;
&lt;br /&gt;
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(A)&lt;br /&gt;
PUSH(B)&lt;br /&gt;
PUSH(C)&lt;br /&gt;
POP(X)&lt;br /&gt;
POP(Z)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. &lt;br /&gt;
&lt;br /&gt;
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.&lt;br /&gt;
&lt;br /&gt;
== Trees ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Trees&amp;#039;&amp;#039;, in general, use the following terminology:  the &amp;#039;&amp;#039;root&amp;#039;&amp;#039; is the top node in the tree; &amp;#039;&amp;#039;children&amp;#039;&amp;#039; are the nodes that are immediately below a &amp;#039;&amp;#039;parent&amp;#039;&amp;#039; node; &amp;#039;&amp;#039;leaves&amp;#039;&amp;#039; are the bottom-most nodes on every branch of the tree; and &amp;#039;&amp;#039;siblings&amp;#039;&amp;#039; are nodes that have the same immediate parent.  &lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is composed of nodes having three parts:  information (or a key), a pointer to a left child, and a pointer to a right child.  It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.&lt;br /&gt;
&lt;br /&gt;
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-american.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;root&amp;#039;&amp;#039; of the resulting tree is the node containing the key A.  Our ACSL convention places duplicate keys into the tree as if they were less than their equal key.  (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.)  The tree has a &amp;#039;&amp;#039;depth&amp;#039;&amp;#039; (sometimes called height) of 3 because the deepest node is 3 nodes below the root.  The root node has a depth of 0.  Nodes with no children are called leaf nodes; there are four of them in the tree:  A, C, I and N.  Our ACSL convention is that an &amp;#039;&amp;#039;external node&amp;#039;&amp;#039; is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.)  In the final tree above, there are 9 external nodes; these are not drawn.  The tree has an &amp;#039;&amp;#039;internal path length&amp;#039;&amp;#039; of 15 which is the sum of the depths of all nodes.  It has an &amp;#039;&amp;#039;external path length&amp;#039;&amp;#039; of 31 which is the sum of the depths of all external nodes.  To insert the N (the last key inserted), 3 &amp;#039;&amp;#039;comparisons&amp;#039;&amp;#039; were needed against the root A (&amp;gt;), the M (&amp;gt;), and the R (≤).&lt;br /&gt;
&lt;br /&gt;
To perform an &amp;#039;&amp;#039;inorder&amp;#039;&amp;#039; traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child.  In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R.  A &amp;#039;&amp;#039;preorder&amp;#039;&amp;#039; travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N.  A &amp;#039;&amp;#039;postorder&amp;#039;&amp;#039; traversal (left, right, root) is: A, C, I, E, N, R, M, A.  Inorder traversals are typically used to list the contents of the tree in sorted order. &lt;br /&gt;
&lt;br /&gt;
A binary search tree can support the operations insert, delete, and search.  Moreover, it handles the operations efficiently for &amp;#039;&amp;#039;balanced&amp;#039;&amp;#039; trees. In a tree with 1 million items, one can search for a particular value in about log&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; 1,000,000 ≈ 20 steps.  Items can be inserted or deleted in about as many steps, too.  However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root &amp;quot;A&amp;quot;.  This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees.  Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. &lt;br /&gt;
&lt;br /&gt;
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = root&lt;br /&gt;
found = FALSE&lt;br /&gt;
while (p ≠ NIL) and (not found)&lt;br /&gt;
  if (x &amp;lt; p’s key)&lt;br /&gt;
    p = p’s left child&lt;br /&gt;
  else if (x &amp;gt; p’s key)&lt;br /&gt;
    p = p’s right child&lt;br /&gt;
  else&lt;br /&gt;
    found = TRUE&lt;br /&gt;
  end if&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deleting from a binary search tree is a bit more complicated.  The algorithm we’ll use is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = node to delete&lt;br /&gt;
f = father of p&lt;br /&gt;
if (p has no children)&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has one child)&lt;br /&gt;
  make p’s child become f’s child&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has two children)&lt;br /&gt;
  l = p’s left child (it might also have children)&lt;br /&gt;
  r = p’s right child (it might also have children)&lt;br /&gt;
  make l become f’s child instead of p&lt;br /&gt;
  stick r onto the l tree&lt;br /&gt;
  delete p&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These diagrams illustrate the algorithm using the tree above.  At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class =&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:bst-american-del-i.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-r.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-m.svg|200px]] &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also general trees that use the same terminology, but they have 0 or more &amp;#039;&amp;#039;subnodes&amp;#039;&amp;#039; which can be accessed with an array or linked list of pointers.  &amp;#039;&amp;#039;Pre-order&amp;#039;&amp;#039; and &amp;#039;&amp;#039;post-order&amp;#039;&amp;#039; traversals are possible with these trees, but the other algorithms do not work in the same way.  Applications of general trees include game theory, organizational charts, family trees, etc.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Balanced&amp;#039;&amp;#039; trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node.  &amp;#039;&amp;#039;Complete&amp;#039;&amp;#039; trees are filled in at every level and are always balanced.  &amp;#039;&amp;#039;Strictly binary&amp;#039;&amp;#039; trees ensure that every node has either 0 or 2 subnodes.  You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.&lt;br /&gt;
&lt;br /&gt;
== Priority Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only.  These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time.  &lt;br /&gt;
&lt;br /&gt;
The standard way to implement a priority queue is using a &amp;#039;&amp;#039;heap&amp;#039;&amp;#039; data structure.  A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties:  every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”.  That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.&lt;br /&gt;
&lt;br /&gt;
The algorithm for insertion is not too difficult:  put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid.  &lt;br /&gt;
&amp;lt;!--here would be the place to show building the heap with AMERICAN--&amp;gt;&lt;br /&gt;
The heap at the left&lt;br /&gt;
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:heap-american.svg|220px]]||&lt;br /&gt;
[[File:heap-american-insert-c.svg|220px]]&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The smallest value is always the root.  To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid.  The following pseudo-code formalizes this notion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
b = bottom-most and right-most element&lt;br /&gt;
p = root of tree&lt;br /&gt;
p’s key = b’s key&lt;br /&gt;
delete b&lt;br /&gt;
while (p is larger than either child)&lt;br /&gt;
  exchange p with smaller child&lt;br /&gt;
  p = smaller child&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the smallest item is at the root of the heap, the heap is called a &amp;#039;&amp;#039;min-heap&amp;#039;&amp;#039;. Of course,  A &amp;#039;&amp;#039;max-heap&amp;#039;&amp;#039; is also possible and is common in practice.  An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.&lt;br /&gt;
&lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Consider an initially empty stack.  After the following operations are performed, what is the value of Z?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
PUSH(3)&lt;br /&gt;
PUSH(6)&lt;br /&gt;
PUSH(8)&lt;br /&gt;
Y = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(X-Y)&lt;br /&gt;
Z = POP()	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The first POP stores 8 in Y. The POP stores 6 in X. Then, 8-6=2 is pushed onto the stack. Finally, the last POP removes the 2 and stores it in Z.&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Create a min-heap with the letters in the word PROGRAMMING.  What are the letters in the bottom-most row, from left to right?&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:heap-programming.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Create a binary search tree from the letters in the word PROGRAM.  What is the internal path length?	&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-program.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Video Resources =&lt;br /&gt;
&lt;br /&gt;
== ACSL Videos ==&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/gXj7K_petqo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/gXj7K_petqo &amp;#039;&amp;#039;Data Structures (Stacks and Queues)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on stacks and queues.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/_BnbbOhyroQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/_BnbbOhyroQ &amp;#039;&amp;#039;Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Shows how to build a binary search tree from the letters &amp;#039;&amp;#039;&amp;#039;S U S H I&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/l9aMO7lgHj0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/l9aMO7lgHj0 &amp;#039;&amp;#039;Binary Search Tree ACSL Problem (Internal Path Length)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on internal path length of a binary search tree&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Other Videos ==&lt;br /&gt;
&lt;br /&gt;
There is no shortage of instructional video material covering basic data structures.  Here is a series that combines teaching concepts with showing Java code that implements the concepts.  Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them.  However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/wjI1WNcIntg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/wjI1WNcIntg &amp;#039;&amp;#039;Data Structures: Stacks and Queues&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/njTh_OwMljA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/njTh_OwMljA &amp;#039;&amp;#039;Data Structures: Linked Lists&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/oSWTXtMglKE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/oSWTXtMglKE &amp;#039;&amp;#039;Data Structures: Trees&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about binary search trees. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/t0Cq6tVNRBA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/t0Cq6tVNRBA &amp;#039;&amp;#039;Data Structures: Heaps&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about heaps. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here are a few more videos covering the basics of binary search trees.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/FvdPo8PBQtc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/FvdPo8PBQtc &amp;#039;&amp;#039;How to Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;edutechional&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.&amp;#039;&amp;#039;&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=626</id>
		<title>Data Structures</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=626"/>
		<updated>2019-01-10T01:45:47Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Priority Queues */ Same as before&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;At the heart of virtually every computer program are its algorithms and its data structures.  It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate.  The bigger the data sets, the more important data structures are in various algorithms.&lt;br /&gt;
&lt;br /&gt;
This category concentrates on four of the most basic structures:  &amp;#039;&amp;#039;&amp;#039;stacks&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;queues&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;binary search trees&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;priority queues&amp;#039;&amp;#039;&amp;#039;.  Questions will cover these data structures and implicit algorithms, not specific to implementation language details.&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; is usually used to save information that will need to be processed later.  Items are processed in a “last-in, first-out” (LIFO) order.  A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed.  This is also known as “first-in, first-out” (FIFO) order.  A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question).  A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query.  One can only find out or delete the smallest element of the list.&lt;br /&gt;
&lt;br /&gt;
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates.  The rest of this page is an overview of these structures.&lt;br /&gt;
&lt;br /&gt;
== Stacks and Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; supports two operations:  PUSH and POP.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X.  If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  An analogy to this is a stack of books on a desk:  a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped).  Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Queues&amp;#039;&amp;#039; operate just like stacks, except items are removed from the bottom instead of the top.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X.  If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change:  new coins are added to the tops of the piles, and change is given from the bottom of each.  Sometimes the top and bottom of a queue are referred to as the rear and the front respectively.  Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue.  There is a similarity to the Britsh &amp;quot;queueing up&amp;quot;.  Some textbooks refer to this data structure as a “FIFO stack”.&lt;br /&gt;
&lt;br /&gt;
Consider the following sequence of 14 operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;M&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;E&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;R&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;I&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;C&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;N&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL.  After all of the operations, there are three items still on the stack:  the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom.  If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL.  There would be three items still on the queue:  N at the top and C on the bottom.  Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.&lt;br /&gt;
&lt;br /&gt;
=== Pre-2018 Syntax ===&lt;br /&gt;
&lt;br /&gt;
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(A)&lt;br /&gt;
PUSH(B)&lt;br /&gt;
PUSH(C)&lt;br /&gt;
POP(X)&lt;br /&gt;
POP(Z)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. &lt;br /&gt;
&lt;br /&gt;
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.&lt;br /&gt;
&lt;br /&gt;
== Trees ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Trees&amp;#039;&amp;#039;, in general, use the following terminology:  the &amp;#039;&amp;#039;root&amp;#039;&amp;#039; is the top node in the tree; &amp;#039;&amp;#039;children&amp;#039;&amp;#039; are the nodes that are immediately below a &amp;#039;&amp;#039;parent&amp;#039;&amp;#039; node; &amp;#039;&amp;#039;leaves&amp;#039;&amp;#039; are the bottom-most nodes on every branch of the tree; and &amp;#039;&amp;#039;siblings&amp;#039;&amp;#039; are nodes that have the same immediate parent.  &lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is composed of nodes having three parts:  information (or a key), a pointer to a left child, and a pointer to a right child.  It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.&lt;br /&gt;
&lt;br /&gt;
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-american.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;root&amp;#039;&amp;#039; of the resulting tree is the node containing the key A.  Our ACSL convention places duplicate keys into the tree as if they were less than their equal key.  (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.)  The tree has a &amp;#039;&amp;#039;depth&amp;#039;&amp;#039; (sometimes called height) of 3 because the deepest node is 3 nodes below the root.  The root node has a depth of 0.  Nodes with no children are called leaf nodes; there are four of them in the tree:  A, C, I and N.  Our ACSL convention is that an &amp;#039;&amp;#039;external node&amp;#039;&amp;#039; is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.)  In the final tree above, there are 9 external nodes; these are not drawn.  The tree has an &amp;#039;&amp;#039;internal path length&amp;#039;&amp;#039; of 15 which is the sum of the depths of all nodes.  It has an &amp;#039;&amp;#039;external path length&amp;#039;&amp;#039; of 31 which is the sum of the depths of all external nodes.  To insert the N (the last key inserted), 3 &amp;#039;&amp;#039;comparisons&amp;#039;&amp;#039; were needed against the root A (&amp;gt;), the M (&amp;gt;), and the R (≤).&lt;br /&gt;
&lt;br /&gt;
To perform an &amp;#039;&amp;#039;inorder&amp;#039;&amp;#039; traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child.  In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R.  A &amp;#039;&amp;#039;preorder&amp;#039;&amp;#039; travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N.  A &amp;#039;&amp;#039;postorder&amp;#039;&amp;#039; traversal (left, right, root) is: A, C, I, E, N, R, M, A.  Inorder traversals are typically used to list the contents of the tree in sorted order. &lt;br /&gt;
&lt;br /&gt;
A binary search tree can support the operations insert, delete, and search.  Moreover, it handles the operations efficiently for &amp;#039;&amp;#039;balanced&amp;#039;&amp;#039; trees. In a tree with 1 million items, one can search for a particular value in about log&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; 1,000,000 ≈ 20 steps.  Items can be inserted or deleted in about as many steps, too.  However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root &amp;quot;A&amp;quot;.  This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees.  Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. &lt;br /&gt;
&lt;br /&gt;
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = root&lt;br /&gt;
found = FALSE&lt;br /&gt;
while (p ≠ NIL) and (not found)&lt;br /&gt;
  if (x &amp;lt; p’s key)&lt;br /&gt;
    p = p’s left child&lt;br /&gt;
  else if (x &amp;gt; p’s key)&lt;br /&gt;
    p = p’s right child&lt;br /&gt;
  else&lt;br /&gt;
    found = TRUE&lt;br /&gt;
  end if&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deleting from a binary search tree is a bit more complicated.  The algorithm we’ll use is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = node to delete&lt;br /&gt;
f = father of p&lt;br /&gt;
if (p has no children)&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has one child)&lt;br /&gt;
  make p’s child become f’s child&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has two children)&lt;br /&gt;
  l = p’s left child (it might also have children)&lt;br /&gt;
  r = p’s right child (it might also have children)&lt;br /&gt;
  make l become f’s child instead of p&lt;br /&gt;
  stick r onto the l tree&lt;br /&gt;
  delete p&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These diagrams illustrate the algorithm using the tree above.  At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class =&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:bst-american-del-i.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-r.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-m.svg|200px]] &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also general trees that use the same terminology, but they have 0 or more &amp;#039;&amp;#039;subnodes&amp;#039;&amp;#039; which can be accessed with an array or linked list of pointers.  &amp;#039;&amp;#039;Pre-order&amp;#039;&amp;#039; and &amp;#039;&amp;#039;post-order&amp;#039;&amp;#039; traversals are possible with these trees, but the other algorithms do not work in the same way.  Applications of general trees include game theory, organizational charts, family trees, etc.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Balanced&amp;#039;&amp;#039; trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node.  &amp;#039;&amp;#039;Complete&amp;#039;&amp;#039; trees are filled in at every level and are always balanced.  &amp;#039;&amp;#039;Strictly binary&amp;#039;&amp;#039; trees ensure that every node has either 0 or 2 subnodes.  You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.&lt;br /&gt;
&lt;br /&gt;
== Priority Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only.  These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time.  &lt;br /&gt;
&lt;br /&gt;
The standard way to implement a priority queue is using a &amp;#039;&amp;#039;heap&amp;#039;&amp;#039; data structure.  A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties:  every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”.  That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.&lt;br /&gt;
&lt;br /&gt;
The algorithm for insertion is not too difficult:  put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid.  &lt;br /&gt;
&amp;lt;!--here would be the place to show building the heap with AMERICAN--&amp;gt;&lt;br /&gt;
The heap at the left&lt;br /&gt;
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:heap-american.svg|220px]]||&lt;br /&gt;
[[File:heap-american-insert-c.svg|220px]]&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The smallest value is always the root.  To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid.  The following pseudo-code formalizes this notion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
b = bottom-most and right-most element&lt;br /&gt;
p = root of tree&lt;br /&gt;
p’s key = b’s key&lt;br /&gt;
delete b&lt;br /&gt;
while (p is larger than either child)&lt;br /&gt;
  exchange p with smaller child&lt;br /&gt;
  p = smaller child&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the smallest item is at the root of the heap, the heap is called a &amp;#039;&amp;#039;min-heap&amp;#039;&amp;#039;. Of course,  A &amp;#039;&amp;#039;max-heap&amp;#039;&amp;#039; is also possible and is common in practice.  An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.&lt;br /&gt;
&lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Consider an initially empty stack.  After the following operations are performed, what is the value of Z?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
PUSH(3)&lt;br /&gt;
PUSH(6)&lt;br /&gt;
PUSH(8)&lt;br /&gt;
Y = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(X-Y)&lt;br /&gt;
Z = POP()	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The first POP stores 8 in Y. The POP stores 6 in X. Then, 8-6=2 is pushed onto the stack. Finally, the last POP removes the 2 and stores it in Z.&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Create a min-heap with the letters in the word PROGRAMMING.  What are the letters in the bottom-most row, from left to right?&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:heap-programming.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Create a binary search tree from the letters in the word PROGRAM.  What is the internal path length?	&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-program.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Video Resources =&lt;br /&gt;
&lt;br /&gt;
== ACSL Videos ==&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/gXj7K_petqo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/gXj7K_petqo &amp;#039;&amp;#039;Data Structures (Stacks and Queues)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on stacks and queues.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/_BnbbOhyroQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/_BnbbOhyroQ &amp;#039;&amp;#039;Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Shows how to build a binary search tree from the letters &amp;#039;&amp;#039;&amp;#039;S U S H I&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/l9aMO7lgHj0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/l9aMO7lgHj0 &amp;#039;&amp;#039;Binary Search Tree ACSL Problem (Internal Path Length)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on internal path length of a binary search tree&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Other Videos ==&lt;br /&gt;
&lt;br /&gt;
There is no shortage of instructional video material covering basic data structures.  Here is a series that combines teaching concepts with showing Java code that implements the concepts.  Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them.  However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/wjI1WNcIntg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/wjI1WNcIntg &amp;#039;&amp;#039;Data Structures: Stacks and Queues&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/njTh_OwMljA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/njTh_OwMljA &amp;#039;&amp;#039;Data Structures: Linked Lists&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/oSWTXtMglKE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/oSWTXtMglKE &amp;#039;&amp;#039;Data Structures: Trees&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about binary search trees. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/t0Cq6tVNRBA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/t0Cq6tVNRBA &amp;#039;&amp;#039;Data Structures: Heaps&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about heaps. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here are a few more videos covering the basics of binary search trees.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/FvdPo8PBQtc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/FvdPo8PBQtc &amp;#039;&amp;#039;How to Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;edutechional&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.&amp;#039;&amp;#039;&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=625</id>
		<title>Data Structures</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=625"/>
		<updated>2019-01-10T01:45:18Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Trees */ Fixed syntax highlighting&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;At the heart of virtually every computer program are its algorithms and its data structures.  It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate.  The bigger the data sets, the more important data structures are in various algorithms.&lt;br /&gt;
&lt;br /&gt;
This category concentrates on four of the most basic structures:  &amp;#039;&amp;#039;&amp;#039;stacks&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;queues&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;binary search trees&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;priority queues&amp;#039;&amp;#039;&amp;#039;.  Questions will cover these data structures and implicit algorithms, not specific to implementation language details.&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; is usually used to save information that will need to be processed later.  Items are processed in a “last-in, first-out” (LIFO) order.  A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed.  This is also known as “first-in, first-out” (FIFO) order.  A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question).  A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query.  One can only find out or delete the smallest element of the list.&lt;br /&gt;
&lt;br /&gt;
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates.  The rest of this page is an overview of these structures.&lt;br /&gt;
&lt;br /&gt;
== Stacks and Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; supports two operations:  PUSH and POP.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X.  If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  An analogy to this is a stack of books on a desk:  a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped).  Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Queues&amp;#039;&amp;#039; operate just like stacks, except items are removed from the bottom instead of the top.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X.  If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change:  new coins are added to the tops of the piles, and change is given from the bottom of each.  Sometimes the top and bottom of a queue are referred to as the rear and the front respectively.  Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue.  There is a similarity to the Britsh &amp;quot;queueing up&amp;quot;.  Some textbooks refer to this data structure as a “FIFO stack”.&lt;br /&gt;
&lt;br /&gt;
Consider the following sequence of 14 operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;M&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;E&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;R&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;I&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;C&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;N&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL.  After all of the operations, there are three items still on the stack:  the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom.  If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL.  There would be three items still on the queue:  N at the top and C on the bottom.  Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.&lt;br /&gt;
&lt;br /&gt;
=== Pre-2018 Syntax ===&lt;br /&gt;
&lt;br /&gt;
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(A)&lt;br /&gt;
PUSH(B)&lt;br /&gt;
PUSH(C)&lt;br /&gt;
POP(X)&lt;br /&gt;
POP(Z)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. &lt;br /&gt;
&lt;br /&gt;
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.&lt;br /&gt;
&lt;br /&gt;
== Trees ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Trees&amp;#039;&amp;#039;, in general, use the following terminology:  the &amp;#039;&amp;#039;root&amp;#039;&amp;#039; is the top node in the tree; &amp;#039;&amp;#039;children&amp;#039;&amp;#039; are the nodes that are immediately below a &amp;#039;&amp;#039;parent&amp;#039;&amp;#039; node; &amp;#039;&amp;#039;leaves&amp;#039;&amp;#039; are the bottom-most nodes on every branch of the tree; and &amp;#039;&amp;#039;siblings&amp;#039;&amp;#039; are nodes that have the same immediate parent.  &lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is composed of nodes having three parts:  information (or a key), a pointer to a left child, and a pointer to a right child.  It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.&lt;br /&gt;
&lt;br /&gt;
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-american.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;root&amp;#039;&amp;#039; of the resulting tree is the node containing the key A.  Our ACSL convention places duplicate keys into the tree as if they were less than their equal key.  (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.)  The tree has a &amp;#039;&amp;#039;depth&amp;#039;&amp;#039; (sometimes called height) of 3 because the deepest node is 3 nodes below the root.  The root node has a depth of 0.  Nodes with no children are called leaf nodes; there are four of them in the tree:  A, C, I and N.  Our ACSL convention is that an &amp;#039;&amp;#039;external node&amp;#039;&amp;#039; is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.)  In the final tree above, there are 9 external nodes; these are not drawn.  The tree has an &amp;#039;&amp;#039;internal path length&amp;#039;&amp;#039; of 15 which is the sum of the depths of all nodes.  It has an &amp;#039;&amp;#039;external path length&amp;#039;&amp;#039; of 31 which is the sum of the depths of all external nodes.  To insert the N (the last key inserted), 3 &amp;#039;&amp;#039;comparisons&amp;#039;&amp;#039; were needed against the root A (&amp;gt;), the M (&amp;gt;), and the R (≤).&lt;br /&gt;
&lt;br /&gt;
To perform an &amp;#039;&amp;#039;inorder&amp;#039;&amp;#039; traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child.  In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R.  A &amp;#039;&amp;#039;preorder&amp;#039;&amp;#039; travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N.  A &amp;#039;&amp;#039;postorder&amp;#039;&amp;#039; traversal (left, right, root) is: A, C, I, E, N, R, M, A.  Inorder traversals are typically used to list the contents of the tree in sorted order. &lt;br /&gt;
&lt;br /&gt;
A binary search tree can support the operations insert, delete, and search.  Moreover, it handles the operations efficiently for &amp;#039;&amp;#039;balanced&amp;#039;&amp;#039; trees. In a tree with 1 million items, one can search for a particular value in about log&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; 1,000,000 ≈ 20 steps.  Items can be inserted or deleted in about as many steps, too.  However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root &amp;quot;A&amp;quot;.  This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees.  Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. &lt;br /&gt;
&lt;br /&gt;
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = root&lt;br /&gt;
found = FALSE&lt;br /&gt;
while (p ≠ NIL) and (not found)&lt;br /&gt;
  if (x &amp;lt; p’s key)&lt;br /&gt;
    p = p’s left child&lt;br /&gt;
  else if (x &amp;gt; p’s key)&lt;br /&gt;
    p = p’s right child&lt;br /&gt;
  else&lt;br /&gt;
    found = TRUE&lt;br /&gt;
  end if&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deleting from a binary search tree is a bit more complicated.  The algorithm we’ll use is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
p = node to delete&lt;br /&gt;
f = father of p&lt;br /&gt;
if (p has no children)&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has one child)&lt;br /&gt;
  make p’s child become f’s child&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has two children)&lt;br /&gt;
  l = p’s left child (it might also have children)&lt;br /&gt;
  r = p’s right child (it might also have children)&lt;br /&gt;
  make l become f’s child instead of p&lt;br /&gt;
  stick r onto the l tree&lt;br /&gt;
  delete p&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These diagrams illustrate the algorithm using the tree above.  At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class =&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:bst-american-del-i.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-r.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-m.svg|200px]] &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also general trees that use the same terminology, but they have 0 or more &amp;#039;&amp;#039;subnodes&amp;#039;&amp;#039; which can be accessed with an array or linked list of pointers.  &amp;#039;&amp;#039;Pre-order&amp;#039;&amp;#039; and &amp;#039;&amp;#039;post-order&amp;#039;&amp;#039; traversals are possible with these trees, but the other algorithms do not work in the same way.  Applications of general trees include game theory, organizational charts, family trees, etc.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Balanced&amp;#039;&amp;#039; trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node.  &amp;#039;&amp;#039;Complete&amp;#039;&amp;#039; trees are filled in at every level and are always balanced.  &amp;#039;&amp;#039;Strictly binary&amp;#039;&amp;#039; trees ensure that every node has either 0 or 2 subnodes.  You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.&lt;br /&gt;
&lt;br /&gt;
== Priority Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only.  These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time.  &lt;br /&gt;
&lt;br /&gt;
The standard way to implement a priority queue is using a &amp;#039;&amp;#039;heap&amp;#039;&amp;#039; data structure.  A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties:  every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”.  That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.&lt;br /&gt;
&lt;br /&gt;
The algorithm for insertion is not too difficult:  put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid.  &lt;br /&gt;
&amp;lt;!--here would be the place to show building the heap with AMERICAN--&amp;gt;&lt;br /&gt;
The heap at the left&lt;br /&gt;
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:heap-american.svg|220px]]||&lt;br /&gt;
[[File:heap-american-insert-c.svg|220px]]&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The smallest value is always the root.  To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid.  The following pseudo-code formalizes this notion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
b = bottom-most and right-most element&lt;br /&gt;
p = root of tree&lt;br /&gt;
p’s key = b’s key&lt;br /&gt;
delete b&lt;br /&gt;
while (p is larger than either child)&lt;br /&gt;
  exchange p with smaller child&lt;br /&gt;
  p = smaller child&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the smallest item is at the root of the heap, the heap is called a &amp;#039;&amp;#039;min-heap&amp;#039;&amp;#039;. Of course,  A &amp;#039;&amp;#039;max-heap&amp;#039;&amp;#039; is also possible and is common in practice.  An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.&lt;br /&gt;
&lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Consider an initially empty stack.  After the following operations are performed, what is the value of Z?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
PUSH(3)&lt;br /&gt;
PUSH(6)&lt;br /&gt;
PUSH(8)&lt;br /&gt;
Y = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(X-Y)&lt;br /&gt;
Z = POP()	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The first POP stores 8 in Y. The POP stores 6 in X. Then, 8-6=2 is pushed onto the stack. Finally, the last POP removes the 2 and stores it in Z.&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Create a min-heap with the letters in the word PROGRAMMING.  What are the letters in the bottom-most row, from left to right?&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:heap-programming.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Create a binary search tree from the letters in the word PROGRAM.  What is the internal path length?	&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-program.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Video Resources =&lt;br /&gt;
&lt;br /&gt;
== ACSL Videos ==&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/gXj7K_petqo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/gXj7K_petqo &amp;#039;&amp;#039;Data Structures (Stacks and Queues)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on stacks and queues.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/_BnbbOhyroQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/_BnbbOhyroQ &amp;#039;&amp;#039;Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Shows how to build a binary search tree from the letters &amp;#039;&amp;#039;&amp;#039;S U S H I&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/l9aMO7lgHj0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/l9aMO7lgHj0 &amp;#039;&amp;#039;Binary Search Tree ACSL Problem (Internal Path Length)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on internal path length of a binary search tree&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Other Videos ==&lt;br /&gt;
&lt;br /&gt;
There is no shortage of instructional video material covering basic data structures.  Here is a series that combines teaching concepts with showing Java code that implements the concepts.  Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them.  However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/wjI1WNcIntg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/wjI1WNcIntg &amp;#039;&amp;#039;Data Structures: Stacks and Queues&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/njTh_OwMljA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/njTh_OwMljA &amp;#039;&amp;#039;Data Structures: Linked Lists&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/oSWTXtMglKE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/oSWTXtMglKE &amp;#039;&amp;#039;Data Structures: Trees&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about binary search trees. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/t0Cq6tVNRBA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/t0Cq6tVNRBA &amp;#039;&amp;#039;Data Structures: Heaps&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about heaps. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here are a few more videos covering the basics of binary search trees.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/FvdPo8PBQtc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/FvdPo8PBQtc &amp;#039;&amp;#039;How to Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;edutechional&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.&amp;#039;&amp;#039;&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=What_Does_This_Program_Do%3F&amp;diff=623</id>
		<title>What Does This Program Do?</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=What_Does_This_Program_Do%3F&amp;diff=623"/>
		<updated>2019-01-09T23:25:37Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Sample Problems */ More syntaxhighlight error fixing&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Frequently, one must use or modify sections of another programmer’s code. Since the original author is often unavailable to explain his/her code, and documentation is, unfortunately,&lt;br /&gt;
not always available or sufficient, it is essential to be able to read and understand an arbitrary program.  &lt;br /&gt;
&lt;br /&gt;
This category presents a program and asks the student to determine that the program does. The programs are written using a pseudocode that&lt;br /&gt;
should be readily understandable by all programmers familiar with a high-level programming language, such as Python, Java, or C.&lt;br /&gt;
&lt;br /&gt;
= Description of the ACSL Pseudo-code =&lt;br /&gt;
&lt;br /&gt;
We will use the following constructs in writing this code for this topic in ACSL:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Construct&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Code Segment&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|Operators&lt;br /&gt;
|! (not) , ^ or ↑(exponent), *, / (real division), % (modulus), +, -, &amp;gt;, &amp;lt;, &amp;gt;=, &amp;lt;=, !=, ==, &amp;amp;&amp;amp; (and), &amp;lt;math&amp;gt;||&amp;lt;/math&amp;gt; (or) in that order of precedence&lt;br /&gt;
|-&lt;br /&gt;
|Functions&lt;br /&gt;
|abs(x) - absolute value, sqrt(x) - square root, int(x) - greatest integer &amp;lt;= x&lt;br /&gt;
|-&lt;br /&gt;
|Variables&lt;br /&gt;
|Start with a letter, only letters and digits&lt;br /&gt;
|-&lt;br /&gt;
|Sequential statements&lt;br /&gt;
|&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|INPUT variable&lt;br /&gt;
|-&lt;br /&gt;
|variable = expression (assignment)&lt;br /&gt;
|-&lt;br /&gt;
|OUTPUT variable&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|Decision statements&lt;br /&gt;
|&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|IF boolean expression THEN&lt;br /&gt;
|-&lt;br /&gt;
|Statement(s)&lt;br /&gt;
|-&lt;br /&gt;
|ELSE   (optional)&lt;br /&gt;
|-&lt;br /&gt;
|Statement(s)&lt;br /&gt;
|-&lt;br /&gt;
|END IF&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|Indefinite Loop statements&lt;br /&gt;
|&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|WHILE Boolean expression&lt;br /&gt;
|-&lt;br /&gt;
|     Statement(s)&lt;br /&gt;
|-&lt;br /&gt;
|END WHILE&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|Definite Loop statements&lt;br /&gt;
|&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|FOR variable = start TO end STEP increment&lt;br /&gt;
|-&lt;br /&gt;
|    Statement(s)&lt;br /&gt;
|-&lt;br /&gt;
|NEXT&lt;br /&gt;
|}&lt;br /&gt;
|-&lt;br /&gt;
|Arrays:&lt;br /&gt;
|1 dimensional  arrays use a single subscript such as A(5).  2 dimensional arrays use (row, col) order such as A(2,3).  Arrays can start at location 0 for 1 dimensional arrays and location (0,0) for 2 dimensional arrays.  Most ACSL past problems start with either A(1) or A(1,1).  The size of the array will usually be specified in the problem statement.&lt;br /&gt;
|-&lt;br /&gt;
|Strings:&lt;br /&gt;
|Strings can contain 0 or more characters and the indexed position starts with 0 at the first character.  An empty string has a length of 0.  Errors occur if accessing a character that is in a negative position or equal to the length of the string or larger.  The len(A) function will find the length of the string which is the total number of characters.  Strings are identified with surrounding double quotes. Use [ ] for identifying the characters in a substring of a given string as follows:  &lt;br /&gt;
S = “ACSL WDTPD” (S has a length of 10 and D is at location 9)&lt;br /&gt;
&lt;br /&gt;
S[:3] = “ACS” (take the first 3 characters starting on the left) &lt;br /&gt;
&lt;br /&gt;
S[5:] = “WDTPD” (take the last 5 characters starting on the right) &lt;br /&gt;
&lt;br /&gt;
S[2:6] = “SL WD” (take the characters starting at location 2 and ending at location 6)&lt;br /&gt;
&lt;br /&gt;
S[0] = “A” (position 0 only). &lt;br /&gt;
&lt;br /&gt;
String concatenation is accomplished using the + symbol&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The questions in this topic will cover any of the above constructs in the Intermediate and Senior Division.  In the Junior Division, loops will not be included in contest 1; loops will be used in contest 2; strings will be used in contest 3; and arrays will be included in contest 4.&lt;br /&gt;
&lt;br /&gt;
= Sample Problems =&lt;br /&gt;
&lt;br /&gt;
== Problem 1 ==&lt;br /&gt;
&lt;br /&gt;
After this program is executed, what is the value of B that is printed if the input values are 50 and 10?&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
input H, R&lt;br /&gt;
B = 0&lt;br /&gt;
if H&amp;gt;48 then&lt;br /&gt;
    B = B + (H - 48) * 2 * R&lt;br /&gt;
    H = 48&lt;br /&gt;
end if&lt;br /&gt;
if H&amp;gt;40 then&lt;br /&gt;
   B = B + (H - 40) * (3/2) * R&lt;br /&gt;
   H = 40&lt;br /&gt;
end if&lt;br /&gt;
B = B + H * R&lt;br /&gt;
output B&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
This program computes an employee’s weekly salary, given the hourly rate (R) and the number of hours worked in the week (H).  The employee is paid an hourly rate for the number of hours worked, up to 40, time and a half for the overtime hours, up to 48 hours, and double for all hours over 48.  The table monitors variables B and H:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! | B || H&lt;br /&gt;
|-&lt;br /&gt;
| | 0 || 50&lt;br /&gt;
|-&lt;br /&gt;
| | 40 || 48&lt;br /&gt;
|-&lt;br /&gt;
| | 160 || 40&lt;br /&gt;
|-&lt;br /&gt;
| | 560 || 40&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Therefore, the final value of B is 2*2*10 + 8*3/2*10 + 40*10 = 40 + 120 + 400 = 560.&lt;br /&gt;
&lt;br /&gt;
== Problem 2 ==&lt;br /&gt;
&lt;br /&gt;
After the following program is executed, what is the final value of NUM?&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
A = “BANANAS”&lt;br /&gt;
NUM = 0: T = “”&lt;br /&gt;
for J = len(A) - 1 to 0 step –1&lt;br /&gt;
     T = T + A[j]&lt;br /&gt;
next &lt;br /&gt;
for J = 0 to len(A) - 1&lt;br /&gt;
    if A[J] == T[J] then NUM = NUM + 1&lt;br /&gt;
next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The program first stores the reverse of variable A into variable T and then counts the number of letters that are in the same position in both strings.&lt;br /&gt;
Variable NUM is incremented each time a character at position x of A is the same as the character in position x of string T. There are 5 such positions: 1, 2, 3, 4, and 5.&lt;br /&gt;
&lt;br /&gt;
== Problem 3 ==&lt;br /&gt;
&lt;br /&gt;
After the following program is executed, what is the final value of C[4]?&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
A(0) = 12: A(1) = 41: A(2) = 52&lt;br /&gt;
A(3) = 57: A(4) = 77: A(5) = -100&lt;br /&gt;
B(0) = 17: B(1) = 34: B(20 = 81&lt;br /&gt;
J = 0: K = 0: N = 0&lt;br /&gt;
while A(J) &amp;gt; 0&lt;br /&gt;
  while B(K) &amp;lt;= A(J)&lt;br /&gt;
    C(N) = B(K)&lt;br /&gt;
    N = N + 1&lt;br /&gt;
    k = k + 1&lt;br /&gt;
  end while&lt;br /&gt;
  C(N) = A(J): N = N + 1: J = J + 1&lt;br /&gt;
end while&lt;br /&gt;
C(N) = B(K)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The following table traces the variables through the execution of the program. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
!J&lt;br /&gt;
!K&lt;br /&gt;
!N&lt;br /&gt;
!A(J)&lt;br /&gt;
!B(K)&lt;br /&gt;
!C(N)&lt;br /&gt;
|-&lt;br /&gt;
|0 || 0  || 0  || 12 || 17 || 12&lt;br /&gt;
|-&lt;br /&gt;
|1 || 0 || 1 || 41 || 17 || 17&lt;br /&gt;
|-&lt;br /&gt;
|1 || 1 || 2 || 41 || 34 || 34&lt;br /&gt;
|-&lt;br /&gt;
|1 || 2 || 3 || 41 || 81 || 41&lt;br /&gt;
|-&lt;br /&gt;
|2 || 2 || 4 || 52 || 81 || 52&lt;br /&gt;
|-&lt;br /&gt;
|3 || 2 || 5 || 57 || 81 || 57&lt;br /&gt;
|-&lt;br /&gt;
|4 || 2 || 6 || 77 || 81 || 77&lt;br /&gt;
|-&lt;br /&gt;
|5 || 2 || 7 || -100 || 81 || 81&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Thus, the value of C(4) is 52.  Note that this program merges two arrays in increasing order into one array until a negative number is input.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
= Video Resources =&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=622</id>
		<title>Data Structures</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Data_Structures&amp;diff=622"/>
		<updated>2019-01-09T23:12:13Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Stacks and Queues */ Another method for fixing syntaxhighlight&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;At the heart of virtually every computer program are its algorithms and its data structures.  It is hard to separate these two items, for data structures are meaningless without algorithms to create and manipulate them, and algorithms are usually trivial unless there are data structures on which to operate.  The bigger the data sets, the more important data structures are in various algorithms.&lt;br /&gt;
&lt;br /&gt;
This category concentrates on four of the most basic structures:  &amp;#039;&amp;#039;&amp;#039;stacks&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;queues&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;binary search trees&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;priority queues&amp;#039;&amp;#039;&amp;#039;.  Questions will cover these data structures and implicit algorithms, not specific to implementation language details.&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; is usually used to save information that will need to be processed later.  Items are processed in a “last-in, first-out” (LIFO) order.  A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is usually used to process items in the order in which requests are generated; a new item is not processed until all items currently on the queue are processed.  This is also known as “first-in, first-out” (FIFO) order.  A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is used when one is storing items and needs to be able to efficiently process the operations of insertion, deletion, and query (i.e. find out if a particular item is found in the list of items and if not, which item is close to the item in question).  A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is used like a binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query.  One can only find out or delete the smallest element of the list.&lt;br /&gt;
&lt;br /&gt;
There are many online resources covering these basic data structures; indeed there are many books and entire courses devoted to fundamental data structures. Implementation varies by computer language, but for our purposes, they can all be represented as a list of items that might contain duplicates.  The rest of this page is an overview of these structures.&lt;br /&gt;
&lt;br /&gt;
== Stacks and Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;stack&amp;#039;&amp;#039; supports two operations:  PUSH and POP.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the stack; the command “X = POP()” removes the top item from the stack and stores its value into variable X.  If the stack was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  An analogy to this is a stack of books on a desk:  a new book is placed on the top of the stack (pushed) and a book is removed from the top also (popped).  Some textbooks call this data structure a “push-down stack” or a “LIFO stack”.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Queues&amp;#039;&amp;#039; operate just like stacks, except items are removed from the bottom instead of the top.  A command of the form PUSH(&amp;quot;A&amp;quot;) puts the key &amp;quot;A&amp;quot; at the top of the queue; the command “X = POP()” removes the item from the bottom of the queue and stores its value into variable X.  If the queue was empty (because nothing had ever been pushed on it, or if all elements have been popped off of it), then X is given the special value of NIL.  A good physical analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change:  new coins are added to the tops of the piles, and change is given from the bottom of each.  Sometimes the top and bottom of a queue are referred to as the rear and the front respectively.  Therefore, items are pushed/enqueued at the rear of the queue and popped/dequeued at the front of the queue.  There is a similarity to the Britsh &amp;quot;queueing up&amp;quot;.  Some textbooks refer to this data structure as a “FIFO stack”.&lt;br /&gt;
&lt;br /&gt;
Consider the following sequence of 14 operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;M&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;E&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;R&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;I&amp;quot;)&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(&amp;quot;C&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;A&amp;quot;)&lt;br /&gt;
PUSH(&amp;quot;N&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL.  After all of the operations, there are three items still on the stack:  the N is at the top (it will be the next to be popped, if nothing else is pushed before the pop command), and C is at the bottom.  If, instead of using a stack we used a queue, then the values popped would be: A, M, E, R, I and NIL.  There would be three items still on the queue:  N at the top and C on the bottom.  Since items are removed from the bottom of a queue, C would be the next item to be popped regardless of any additional pushes.&lt;br /&gt;
&lt;br /&gt;
=== Pre-2018 Syntax ===&lt;br /&gt;
&lt;br /&gt;
ACSL Contests pre-2018 used a slightly different, and more liberal, syntax. Consider the following sequence on a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
PUSH(A)&lt;br /&gt;
PUSH(B)&lt;br /&gt;
PUSH(C)&lt;br /&gt;
POP(X)&lt;br /&gt;
POP(Z)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After the 5 operations, the stack would be left with A as the only element on the stack, the value of C in variable X, and the value of B in variable Z. &lt;br /&gt;
&lt;br /&gt;
Were the above operations applied to a queue, the queue would be left with C; variable X would contain A; and variable Z would contain B.&lt;br /&gt;
&lt;br /&gt;
== Trees ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Trees&amp;#039;&amp;#039;, in general, use the following terminology:  the &amp;#039;&amp;#039;root&amp;#039;&amp;#039; is the top node in the tree; &amp;#039;&amp;#039;children&amp;#039;&amp;#039; are the nodes that are immediately below a &amp;#039;&amp;#039;parent&amp;#039;&amp;#039; node; &amp;#039;&amp;#039;leaves&amp;#039;&amp;#039; are the bottom-most nodes on every branch of the tree; and &amp;#039;&amp;#039;siblings&amp;#039;&amp;#039; are nodes that have the same immediate parent.  &lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;binary search tree&amp;#039;&amp;#039; is composed of nodes having three parts:  information (or a key), a pointer to a left child, and a pointer to a right child.  It has the property that the key at every node is always greater than or equal to the key of its left child, and less than the key of its right child.&lt;br /&gt;
&lt;br /&gt;
The following tree is built from the keys A, M, E, R, I, C, A, N in that order:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-american.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;root&amp;#039;&amp;#039; of the resulting tree is the node containing the key A.  Our ACSL convention places duplicate keys into the tree as if they were less than their equal key.  (In some textbooks and software libraries, duplicate keys may be considered larger than their equal key.)  The tree has a &amp;#039;&amp;#039;depth&amp;#039;&amp;#039; (sometimes called height) of 3 because the deepest node is 3 nodes below the root.  The root node has a depth of 0.  Nodes with no children are called leaf nodes; there are four of them in the tree:  A, C, I and N.  Our ACSL convention is that an &amp;#039;&amp;#039;external node&amp;#039;&amp;#039; is the name given to a place where a new node could be attached to the tree. (In some textbooks, an external node is synonymous with a leaf node.)  In the final tree above, there are 9 external nodes; these are not drawn.  The tree has an &amp;#039;&amp;#039;internal path length&amp;#039;&amp;#039; of 15 which is the sum of the depths of all nodes.  It has an &amp;#039;&amp;#039;external path length&amp;#039;&amp;#039; of 31 which is the sum of the depths of all external nodes.  To insert the N (the last key inserted), 3 &amp;#039;&amp;#039;comparisons&amp;#039;&amp;#039; were needed against the root A (&amp;gt;), the M (&amp;gt;), and the R (≤).&lt;br /&gt;
&lt;br /&gt;
To perform an &amp;#039;&amp;#039;inorder&amp;#039;&amp;#039; traversal of the tree, recursively traverse the tree by first visiting the left child, then the root, then the right child.  In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N, and R.  A &amp;#039;&amp;#039;preorder&amp;#039;&amp;#039; travel (root, left, right) visits in the following order: A, A, M, E, C, I, R, and N.  A &amp;#039;&amp;#039;postorder&amp;#039;&amp;#039; traversal (left, right, root) is: A, C, I, E, N, R, M, A.  Inorder traversals are typically used to list the contents of the tree in sorted order. &lt;br /&gt;
&lt;br /&gt;
A binary search tree can support the operations insert, delete, and search.  Moreover, it handles the operations efficiently for &amp;#039;&amp;#039;balanced&amp;#039;&amp;#039; trees. In a tree with 1 million items, one can search for a particular value in about log&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt; 1,000,000 ≈ 20 steps.  Items can be inserted or deleted in about as many steps, too.  However, consider the binary search tree resulting from inserting the keys A, E, I, O, U, Y which places all of the other letters on the right side of the root &amp;quot;A&amp;quot;.  This is very unbalanced; therefore, sophisticated algorithms can be used to maintain balanced trees.  Binary search trees are “dynamic” data structures that can support many operations in any order and introduces or removes nodes as needed. &lt;br /&gt;
&lt;br /&gt;
To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
p = root&lt;br /&gt;
found = FALSE&lt;br /&gt;
while (p ≠ NIL) and (not found)&lt;br /&gt;
  if (x &amp;lt; p’s key)&lt;br /&gt;
    p = p’s left child&lt;br /&gt;
  else if (x &amp;gt; p’s key)&lt;br /&gt;
    p = p’s right child&lt;br /&gt;
  else&lt;br /&gt;
    found = TRUE&lt;br /&gt;
  end if&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deleting from a binary search tree is a bit more complicated.  The algorithm we’ll use is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
p = node to delete&lt;br /&gt;
f = father of p&lt;br /&gt;
if (p has no children)&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has one child)&lt;br /&gt;
  make p’s child become f’s child&lt;br /&gt;
  delete p&lt;br /&gt;
else if (p has two children)&lt;br /&gt;
  l = p’s left child (it might also have children)&lt;br /&gt;
  r = p’s right child (it might also have children)&lt;br /&gt;
  make l become f’s child instead of p&lt;br /&gt;
  stick r onto the l tree&lt;br /&gt;
  delete p&lt;br /&gt;
end if&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These diagrams illustrate the algorithm using the tree above.  At the left, we delete I (0 children); in the middle, we delete the R (1 child); and at the right, we delete the M (2 children).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class =&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:bst-american-del-i.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-r.svg|200px]]&lt;br /&gt;
|[[File:bst-american-del-m.svg|200px]] &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are also general trees that use the same terminology, but they have 0 or more &amp;#039;&amp;#039;subnodes&amp;#039;&amp;#039; which can be accessed with an array or linked list of pointers.  &amp;#039;&amp;#039;Pre-order&amp;#039;&amp;#039; and &amp;#039;&amp;#039;post-order&amp;#039;&amp;#039; traversals are possible with these trees, but the other algorithms do not work in the same way.  Applications of general trees include game theory, organizational charts, family trees, etc.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Balanced&amp;#039;&amp;#039; trees minimize searching time when every leaf node has a depth of within 1 of every other leaf node.  &amp;#039;&amp;#039;Complete&amp;#039;&amp;#039; trees are filled in at every level and are always balanced.  &amp;#039;&amp;#039;Strictly binary&amp;#039;&amp;#039; trees ensure that every node has either 0 or 2 subnodes.  You may want to consider how there are exactly 5 strictly binary trees with 7 nodes.&lt;br /&gt;
&lt;br /&gt;
== Priority Queues ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is quite similar to a binary search tree, but one can only delete the smallest item and retrieve the smallest item only.  These insert and delete operations can be done in a guaranteed time proportional to the log (base 2) of the number of items; the retrieve-the-smallest can be done in constant time.  &lt;br /&gt;
&lt;br /&gt;
The standard way to implement a priority queue is using a &amp;#039;&amp;#039;heap&amp;#039;&amp;#039; data structure.  A heap uses a binary tree (that is, a tree with two children) and maintains the following two properties:  every node is less than or equal to both of its two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains no “holes”.  That is, all levels of the tree are completely filled, except the bottom level, which is filled in from the left to the right.&lt;br /&gt;
&lt;br /&gt;
The algorithm for insertion is not too difficult:  put the new node at the bottom of the tree and then go up the tree, making exchanges with its parent, until the tree is valid.  &lt;br /&gt;
&amp;lt;!--here would be the place to show building the heap with AMERICAN--&amp;gt;&lt;br /&gt;
The heap at the left&lt;br /&gt;
was building from the letters A, M, E, R, I, C, A, N (in that order); the heap at the right is after a C has been added.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[[File:heap-american.svg|220px]]||&lt;br /&gt;
[[File:heap-american-insert-c.svg|220px]]&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The smallest value is always the root.  To delete it (and one can only delete the smallest value), one replaces it with the bottom-most and right-most element, and then walks down the tree making exchanges with the smaller child in order to ensure that the tree is valid.  The following pseudo-code formalizes this notion:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
b = bottom-most and right-most element&lt;br /&gt;
p = root of tree&lt;br /&gt;
p’s key = b’s key&lt;br /&gt;
delete b&lt;br /&gt;
while (p is larger than either child)&lt;br /&gt;
  exchange p with smaller child&lt;br /&gt;
  p = smaller child&lt;br /&gt;
end while&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the smallest item is at the root of the heap, the heap is called a &amp;#039;&amp;#039;min-heap&amp;#039;&amp;#039;. Of course,  A &amp;#039;&amp;#039;max-heap&amp;#039;&amp;#039; is also possible and is common in practice.  An efficient implementation of a heap uses an array that can be understood conceptually by using a tree data structure.&lt;br /&gt;
&lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Consider an initially empty stack.  After the following operations are performed, what is the value of Z?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
PUSH(3)&lt;br /&gt;
PUSH(6)&lt;br /&gt;
PUSH(8)&lt;br /&gt;
Y = POP()&lt;br /&gt;
X = POP()&lt;br /&gt;
PUSH(X-Y)&lt;br /&gt;
Z = POP()	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The first POP stores 8 in Y. The POP stores 6 in X. Then, 8-6=2 is pushed onto the stack. Finally, the last POP removes the 2 and stores it in Z.&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Create a min-heap with the letters in the word PROGRAMMING.  What are the letters in the bottom-most row, from left to right?&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; The bottom row contains the letters RORN, from left-to-right. Here is the entire heap:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:heap-programming.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Create a binary search tree from the letters in the word PROGRAM.  What is the internal path length?	&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039; When drawing the tree, P has a depth of 0, O and R have a depth of 1, G and R have a depth of 2, and A and M have a depth of 3. Therefore, the internal path length is 2*1 + 2*2 + 2*3 = 12. Here is the tree:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;&lt;br /&gt;
[[File:bst-program.svg|200px]]&lt;br /&gt;
&amp;lt;/center&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Video Resources =&lt;br /&gt;
&lt;br /&gt;
== ACSL Videos ==&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/gXj7K_petqo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/gXj7K_petqo &amp;#039;&amp;#039;Data Structures (Stacks and Queues)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on stacks and queues.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/_BnbbOhyroQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/_BnbbOhyroQ &amp;#039;&amp;#039;Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Shows how to build a binary search tree from the letters &amp;#039;&amp;#039;&amp;#039;S U S H I&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/l9aMO7lgHj0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/l9aMO7lgHj0 &amp;#039;&amp;#039;Binary Search Tree ACSL Problem (Internal Path Length)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A general tutorial on internal path length of a binary search tree&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Other Videos ==&lt;br /&gt;
&lt;br /&gt;
There is no shortage of instructional video material covering basic data structures.  Here is a series that combines teaching concepts with showing Java code that implements the concepts.  Most of the ACSL questions will not involve coding the basic operations on the data structures; rather, the problems will involve high-level understanding of them.  However, seeing the code is an excellent way to thoroughly understand these data structures, and what it takes to implement them.&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/wjI1WNcIntg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/wjI1WNcIntg &amp;#039;&amp;#039;Data Structures: Stacks and Queues&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
The first half of the video is a nice description of stacks and queues; the second half walks through very clean Java code that implements the fundamental methods on these data structures.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/njTh_OwMljA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/njTh_OwMljA &amp;#039;&amp;#039;Data Structures: Linked Lists&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Although ACSL does not cover linked lists per se, they are a great preliminary study for binary search trees. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/oSWTXtMglKE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/oSWTXtMglKE &amp;#039;&amp;#039;Data Structures: Trees&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about binary search trees. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of binary search trees; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/t0Cq6tVNRBA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/t0Cq6tVNRBA &amp;#039;&amp;#039;Data Structures: Heaps&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HackerRank&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Learn about heaps. This video is a part of HackerRank&amp;#039;s &amp;#039;&amp;#039;Cracking The Coding Interview Tutorial&amp;#039;&amp;#039; with Gayle Laakmann McDowell. The first half of the video is a clear and eloquent description of heaps; the second half walks through very clean Java code that implements the fundamental methods. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here are a few more videos covering the basics of binary search trees.&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/FvdPo8PBQtc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/FvdPo8PBQtc &amp;#039;&amp;#039;How to Construct a Binary Search Tree&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;edutechional&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;In this algorithm tutorial, I walk through how to construct a binary search tree given an unordered array, and then how to find elements inside of the tree.&amp;#039;&amp;#039;&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=621</id>
		<title>LISP</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=LISP&amp;diff=621"/>
		<updated>2019-01-09T22:41:05Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Problem 3 */ Tried to fix syntaxhighlight&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful.  It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language.”  It has been historically used for virtually all Artificial Intelligence programs and is often the environment of choice for applications which require a powerful interactive working environment.  LISP presents a very different way to think about programming from the “algorithmic” languages, such as Python, C++, and Java.&lt;br /&gt;
&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
As its name implies, the basis of LISP is a list.  One constructs a list by enumerating elements inside a pair of parentheses.  For example, here is a list with four elements (the second element is also a list):&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;code&amp;gt;(23 (this is easy) hello 821)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The elements in the list, which are not lists, are called “atoms.”  For example, the atoms in the list above are: 23, ‘this, ‘hello, 821, ‘easy, and ‘is.  Literals are identified with a single leading quote.  Everything in LISP is either an atom or a list (but not both).  The only exception is “NIL,” which is both an atom and a list.  It can also be written as “()” – a pair of parentheses with nothing inside.&lt;br /&gt;
&lt;br /&gt;
All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn).  To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments.  For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35.  Some functions have an arbitrary number of arguments; others require a fixed number.  All statements return a value, which is either an atom or a list. &lt;br /&gt;
&lt;br /&gt;
== Basic Functions (SET, SETQ, EVAL, ATOM) ==&lt;br /&gt;
&lt;br /&gt;
We may assign values to variables using the function SET.  For example, the statement (SET ’test 6) would have a value of a 6, and (more importantly, however) would also cause the atom ‘test to be bound to the atom 6.  The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted.  &lt;br /&gt;
Observe the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ( MULT 2 3))&lt;br /&gt;
|6&lt;br /&gt;
|a is an atom with a vaue of 6&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’a ’(MULT 2 3))&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|a is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’b ’a)&lt;br /&gt;
|a&lt;br /&gt;
|b is an atom with a value of the character a&lt;br /&gt;
|-&lt;br /&gt;
|(SET ’c a)&lt;br /&gt;
|(MULT 2 3)&lt;br /&gt;
|c is a list with 3 elements&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ EX (ADD 3 (MULT 2 5)))&lt;br /&gt;
|13&lt;br /&gt;
|The variable EX has a value of 13&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ VOWELS ’(A E I O U))&lt;br /&gt;
|(A E I O U)&lt;br /&gt;
|VOWELS is a list of 5 elements&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The function EVAL returns the value of its argument, after it has been evaluated.  For example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a value of (ADD 2 3); the function (EVAL z) has a value of 5 (but the binding of the atom z has not changed).  In this last example, you can think of z being “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, and once when the function EVAL is invoked to resolve arguments.  The function ATOM can be used to determine whether an item is an atom or a list. It returns either &amp;quot;true&amp;quot;, or &amp;quot;NIL&amp;quot; for false. Consider the following examples:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value || Comment &lt;br /&gt;
|-&lt;br /&gt;
|(SETQ p &amp;#039;(ADD 1 2 3 4))&lt;br /&gt;
|(ADD 1 2 3 4)&lt;br /&gt;
|p is a list with 5 elements&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM &amp;#039;p)&lt;br /&gt;
|true&lt;br /&gt;
|The argument to ATOM is the atom p&lt;br /&gt;
|-&lt;br /&gt;
|(ATOM p)&lt;br /&gt;
|NIL&lt;br /&gt;
|Because p is not quoted, it is evaluated to the 5-element list.&lt;br /&gt;
|-&lt;br /&gt;
|(EVAL p)&lt;br /&gt;
|10&lt;br /&gt;
|The argument to EVAL is the value of p; the value of p is 10. &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== List Functions (CAR, CDR, CONS, REVERSE)==&lt;br /&gt;
&lt;br /&gt;
The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after registers of a now long-forgotten IBM machine on which LISP was first developed.  The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list).  The function CONS takes two arguments, of which the second must be a list.  It returns a list which is composed by placing the first argument as the first element in the second argument’s list.  The function REVERSE returns a list which is its arguments in reverse order.  &lt;br /&gt;
&lt;br /&gt;
The CAR and CDR functions are used extensively to grab specific elements of a list or sublist, that there&amp;#039;s a shorthand for this: (CADR x) is the same as (CAR (CDR x)), which retrieves the second element of the list x; (CAADDAR x) is a shorthand for (CAR (CAR (CDR (CDR (CAR x))))), and so on.&lt;br /&gt;
&lt;br /&gt;
The following examples illustrate the use of CAR, CDR, CONS, and REVERSE: &lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(CAR ’(This is a list))&lt;br /&gt;
|This&lt;br /&gt;
|-&lt;br /&gt;
|(CDR ’(This is a list)))&lt;br /&gt;
|(is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(CONS &amp;#039;red &amp;#039;(white blue))&lt;br /&gt;
|(red white blue)&lt;br /&gt;
|-&lt;br /&gt;
|(SETQ z (CONS &amp;#039;(red white blue) (CDR ’(This is a list)))))&lt;br /&gt;
|((red white blue) is a list)&lt;br /&gt;
|-&lt;br /&gt;
|(REVERSE z)&lt;br /&gt;
|(list a is (red white blue))&lt;br /&gt;
|-&lt;br /&gt;
|(CDDAR z)&lt;br /&gt;
|(blue)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Arithmetic Functions (ADD, MULT, ...)==&lt;br /&gt;
&lt;br /&gt;
As you have probably already figured out, the function ADD simply summed its arguments.  We’ll also be using the following arithmetic functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: left&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Function&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Result&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD x1 x2 …)&lt;br /&gt;
|sum of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(SUB a b)&lt;br /&gt;
|a-b&lt;br /&gt;
|-&lt;br /&gt;
|(MULT x1 x2 …)&lt;br /&gt;
|product of all arguments&lt;br /&gt;
|-&lt;br /&gt;
|(DIV a b)&lt;br /&gt;
|a/b&lt;br /&gt;
|-&lt;br /&gt;
|(SQUARE a)&lt;br /&gt;
|a*a&lt;br /&gt;
|-&lt;br /&gt;
|(EXP a n)&lt;br /&gt;
|a&amp;lt;sup&amp;gt;n&amp;lt;/sup&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|(EQ a b)&lt;br /&gt;
|true if a and b are equal, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(POS a)&lt;br /&gt;
|true if a is positive, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|(NEG a)&lt;br /&gt;
|true if a is negative, NIL otherwise&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Functions ADD, SUB, MULT, and DIV can be written as their common mathematical symbols, +, -, *, and /.  Here are some examples of these functions:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; || &amp;#039;&amp;#039;&amp;#039;Value&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4))&lt;br /&gt;
|24.5&lt;br /&gt;
|-&lt;br /&gt;
|(- (* 3 2) (- 12 (+ 1 2 1)))&lt;br /&gt;
| -2&lt;br /&gt;
|-&lt;br /&gt;
|(ADD (SQUARE 3) (SQUARE 4))&lt;br /&gt;
|25&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== User-defined Functions ==&lt;br /&gt;
&lt;br /&gt;
LISP also allows us to create our own functions using the DEF function. (We will sometimes use DEFUN rather than DEF, as it is a bit more standard terminology.)  For example,&lt;br /&gt;
 (DEF SECOND (args) (CAR (CDR args)))&lt;br /&gt;
defines a new function called SECOND which operates on a single parameter named “args”.  SECOND will take the CDR of the parameter and then the CAR of that result.  So, for example:&lt;br /&gt;
(SECOND ’(a b c d e))&lt;br /&gt;
would first CDR the list to give (b c d e), and then CAR that value returning the single character “b”.  Consider the following program fragment:&lt;br /&gt;
:(SETQ X ’(a c s l))&lt;br /&gt;
:(DEF WHAT(args) (CONS args (REVERSE (CDR args))))&lt;br /&gt;
:(DEF SECOND(args) (CONS (CAR (CDR args)) NIL))&lt;br /&gt;
&lt;br /&gt;
The following chart illustrates the use of the user-defined functions WHAT and SECOND:&lt;br /&gt;
&lt;br /&gt;
:{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Statement || Value&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT X) || ((a c s l) l s c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND X) || (c)&lt;br /&gt;
|-&lt;br /&gt;
|(SECOND (WHAT X)) || (l)&lt;br /&gt;
|-&lt;br /&gt;
|(WHAT (SECOND X)) || ((c))&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Online Interpreters ==&lt;br /&gt;
&lt;br /&gt;
There are many online LISP interpreters available on the Internet. The one that ACSL uses for testing its programs is CLISP that is accessible from   [https://www.jdoodle.com/execute-clisp-online JDoodle]. This interpreter is nice because it is quite peppy in running programs, and functions are not case sensitive. So, &amp;lt;code&amp;gt;(CAR (CDR x))&amp;lt;/code&amp;gt; is legal as is &amp;lt;code&amp;gt;(car (cdr x))&amp;lt;/code&amp;gt; One drawback of this interpreter is the print function changes lowercase input into uppercase. &lt;br /&gt;
 &lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
Questions from this topic will typically present a line of LISP code or a short sequence of statements and ask what is the value of the (final) statement.&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression. &amp;lt;code&amp;gt;(MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 9 (SUB 2 5)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 (MULT (ADD 6 5 0) (MULT 5 1 2 2) (DIV 6 (SUB 2 5)))&lt;br /&gt;
 (MULT 11 20  (DIV 6 -3))&lt;br /&gt;
 (MULT 11 20 -2)&lt;br /&gt;
 -440&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:  &amp;lt;code&amp;gt;(CDR ’((2 (3))(4 (5 6) 7)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the list with the first element removed.  The first element of the list is (2  (3)) and the list without this element is&lt;br /&gt;
((4 (5 6) 7)), a list with one element.&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
Consider the following program fragment:&lt;br /&gt;
 (SETQ X ’(RI VA FL CA TX))&lt;br /&gt;
 (CAR (CDR (REVERSE X)))&lt;br /&gt;
What is the value of the CAR expression? &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The first statement binds variable X to the list ‘(RI VA FL CA TX).&lt;br /&gt;
The REVERSE of this list is ‘(TX CA FL VA RI)&lt;br /&gt;
whose CDR is ‘(CA FL VA RI).  The CAR of this list is just the atom CA (without the quote).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- &lt;br /&gt;
&lt;br /&gt;
Too scary of a sample problem! --marc 9/3/2018&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Given the function definitions for HY and FY as follows:&lt;br /&gt;
   (DEFUN HY(PARM) (REVERSE (CDR PARM)))&lt;br /&gt;
   (DEFUN FY(PARM) (CAR (HY (CDR PARM))))&lt;br /&gt;
What is the value of the following?&lt;br /&gt;
     (FY ’(DO RE (MI FA) SO))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
To evaluate (FY ’(DO RE (MI FA) SO)), we must evaluate&lt;br /&gt;
     (CAR (HY (CDR ’(DO RE (MI FA) SO)))).&lt;br /&gt;
Thus, HY is invoked with &lt;br /&gt;
     PARM = ’(RE (MI FA) SO), and we evaluate&lt;br /&gt;
     (REVERSE (CDR ’(RE (MI FA) SO)))&lt;br /&gt;
This has a value of (SO (MI FA)) which is returned to FY.  FY now takes the CAR of this which is SO (without the quotes).&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jWFgmE279eQ&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jWFgmE279eQ &amp;#039;&amp;#039;LISP very gentle intro&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A very gentle introduction to the LISP category, covering the LISP syntax and the basic operators, SETQ, ADD, SUB, MULT, and DIV.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/mRpbbss48sw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/mRpbbss48sw &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Explains the LISP operators CAR, CDR, and REVERSE, in the context of solving an ACSL All-Star Contest problem.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/50wj_f51kBM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/50wj_f51kBM &amp;#039;&amp;#039;LISP Basics (for ACSL)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Completes the problem that was started in the above video. &lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
	<entry>
		<id>http://www.categories.acsl.org/wiki/index.php?title=Bit-String_Flicking&amp;diff=620</id>
		<title>Bit-String Flicking</title>
		<link rel="alternate" type="text/html" href="http://www.categories.acsl.org/wiki/index.php?title=Bit-String_Flicking&amp;diff=620"/>
		<updated>2019-01-09T20:27:54Z</updated>

		<summary type="html">&lt;p&gt;Williamhu: /* Bitwise Operators */ Replaced &amp;#039;o&amp;#039; with &amp;#039;0&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Bit strings (strings of binary digits) are frequently manipulated bit-by-bit using the logical operators  &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039;. Bits strings are manipulated as a unit using &amp;#039;&amp;#039;&amp;#039;shift&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;circulate&amp;#039;&amp;#039;&amp;#039; operators. The bits on the left are called the &amp;#039;&amp;#039;most significant bits&amp;#039;&amp;#039; and those on the right are the &amp;#039;&amp;#039;least significant bits&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Most high-level languages (e.g., Python, Java, C++), support bit-string operations. Programmers typically use bit strings to maintain a set of flags.  Suppose that a program supports 8 options, each of which can be either “on” or “off”.  One could maintain this information using an array of size 8, or one could use a single variable (if it is internally stored using at least 8 bits or 1 byte, which is usually the case) and represent each option with a single bit.  In addition to saving space, the program is often cleaner if a single variable is involved rather than an array.  Bits strings are often used to maintain a set where values are either in the set or not. Shifting of bits is also used to multiply or divide by powers of 2.&lt;br /&gt;
&lt;br /&gt;
Mastering this topic is essential for systems programming, programming in assembly language, optimizing code, and hardware design.&lt;br /&gt;
&lt;br /&gt;
== Operators==&lt;br /&gt;
&lt;br /&gt;
=== Bitwise Operators ===&lt;br /&gt;
&lt;br /&gt;
The logical operators are  &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; (~ or $\neg$), &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; (&amp;amp;), &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; (|), and &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; ($\oplus$). These operators should be familiar to ACSL students from the [[Boolean Algebra]] and [[Digital Electronics]] categories.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; is a unary operator that performs logical negation on each bit. Bits that are 0 become 1, and those that are 1 become 0. For example: ~101110 has a value of 010001.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; is a binary operator that performs the logical &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; of each bit in each of its operands.  The &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; of two values is 1 only if both values are 1. For example, &amp;#039;&amp;#039;&amp;#039;1011011 and 011001&amp;#039;&amp;#039;&amp;#039; has a value of &amp;#039;&amp;#039;&amp;#039;001001&amp;#039;&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; function is often used to isolate the value of a bit in a bit-string or to clear the value of a bit in a bit-string.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; is a binary operator that performs the logical &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; of each bit in each of its operands. The &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; of two values is 1 only if one or both values are 1. For example, &amp;#039;&amp;#039;&amp;#039;1011011 or 011001&amp;#039;&amp;#039;&amp;#039; has a value of &amp;#039;&amp;#039;&amp;#039;111011&amp;#039;&amp;#039;&amp;#039;. The &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; function is often use to force the value of a bit in a bit-string to be 1, if it isn&amp;#039;t already.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; is a binary operator that performs the logical &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; of each bit in each of its operands. The &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; of two values is 1 if the values are different and 0 if they are the same. For example, 1011011 xor 011001 = 110010. The &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; function is often used to change the value of a particular bit.&lt;br /&gt;
&lt;br /&gt;
All binary operators (and, or, or xor) must operate on bit-strings that are of&lt;br /&gt;
the same length. If the operands are not the same&lt;br /&gt;
length, the shorter one is padded with 0&amp;#039;s on the left as needed. For &lt;br /&gt;
example, &amp;#039;&amp;#039;&amp;#039;11010 and 1110&amp;#039;&amp;#039;&amp;#039; would have value of &amp;#039;&amp;#039;&amp;#039;11010 and 01110 = 01010&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The following table summarizes the operators:&lt;br /&gt;
&lt;br /&gt;
::{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;&lt;br /&gt;
!&amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&lt;br /&gt;
! &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;&lt;br /&gt;
!&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&lt;br /&gt;
!&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&lt;br /&gt;
!&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; &amp;#039;&amp;#039;&amp;#039;xor&amp;#039;&amp;#039;&amp;#039; &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!0&lt;br /&gt;
!0&lt;br /&gt;
| 1 &lt;br /&gt;
| 0 &lt;br /&gt;
| 0 &lt;br /&gt;
| 0 &lt;br /&gt;
|-&lt;br /&gt;
!0&lt;br /&gt;
!1&lt;br /&gt;
| 1 &lt;br /&gt;
| 0 &lt;br /&gt;
| 1 &lt;br /&gt;
| 1 &lt;br /&gt;
|-&lt;br /&gt;
!1&lt;br /&gt;
!0&lt;br /&gt;
| 0 &lt;br /&gt;
| 0 &lt;br /&gt;
| 1 &lt;br /&gt;
| 1 &lt;br /&gt;
|-&lt;br /&gt;
!1&lt;br /&gt;
!1&lt;br /&gt;
| 0 &lt;br /&gt;
| 1 &lt;br /&gt;
| 1 &lt;br /&gt;
| 0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Shift Operators ===&lt;br /&gt;
&lt;br /&gt;
Logical shifts (LSHIFT-x and RSHIFT-x) “ripple” the bit-string x positions in the indicated direction, either to the left or to the right.  Bits shifted out are lost; zeros are shifted in at the other end.   &lt;br /&gt;
&lt;br /&gt;
Circulates (RCIRC-x and LCIRC-x) “ripple” the bit string x positions in the indicated direction.  As each bit is shifted out one end, it is shifted in at the other end. The effect of this is that the bits remain in the same order on the other side of the string.&lt;br /&gt;
&lt;br /&gt;
The size of a bit-string  does not change with shifts, or circulates.  If any bit strings are initially of different lengths, all shorter ones are padded with zeros in the left bits until all strings are of the same length.  &lt;br /&gt;
&lt;br /&gt;
The following table gives some examples of these operations:&lt;br /&gt;
&lt;br /&gt;
::{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align: right&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!x&lt;br /&gt;
!(LSHIFT-2 x)&lt;br /&gt;
!(RSHIFT-3 x)&lt;br /&gt;
!(LCIRC-3 x)&lt;br /&gt;
!(RCIRC-1 x)&lt;br /&gt;
|-&lt;br /&gt;
!01101&lt;br /&gt;
| 10100&lt;br /&gt;
| 00001&lt;br /&gt;
| 01011&lt;br /&gt;
| 10110&lt;br /&gt;
|-&lt;br /&gt;
!10&lt;br /&gt;
| 00&lt;br /&gt;
| 00&lt;br /&gt;
| 01&lt;br /&gt;
| 01&lt;br /&gt;
|-&lt;br /&gt;
!1110&lt;br /&gt;
| 1000&lt;br /&gt;
| 0001&lt;br /&gt;
| 0111&lt;br /&gt;
| 0111&lt;br /&gt;
|-&lt;br /&gt;
!1011011&lt;br /&gt;
| 1101100&lt;br /&gt;
| 0001011&lt;br /&gt;
| 1011101&lt;br /&gt;
| 1101101&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Order of Precedence ===&lt;br /&gt;
&lt;br /&gt;
The order of precedence (from highest to lowest) is: NOT; SHIFT and CIRC; AND; XOR; and finally, OR.  In other words, all unary operators are performed on a single operator first.  Operators with equal precedence are evaluated left to right; all unary  operators bind from right to left.&lt;br /&gt;
&lt;br /&gt;
== Sample Problems ==&lt;br /&gt;
&lt;br /&gt;
=== Problem 1 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression: &lt;br /&gt;
:(0101110 AND NOT 110110 OR (LSHIFT-2 101010))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The expression evaluates as follows:&lt;br /&gt;
:(0101110 AND &amp;#039;&amp;#039;&amp;#039;001001&amp;#039;&amp;#039;&amp;#039; OR (LSHIFT-2 101010))&lt;br /&gt;
:(&amp;#039;&amp;#039;&amp;#039;001000&amp;#039;&amp;#039;&amp;#039; OR (LSHIFT-2 101010))&lt;br /&gt;
:(001000 OR &amp;#039;&amp;#039;&amp;#039;010000&amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
:&amp;#039;&amp;#039;&amp;#039;011000&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== Problem 2 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression: &lt;br /&gt;
:(RSHIFT-1 (LCIRC-4 (RCIRC-2 01101))) &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The expression evaluates as follows, starting at the innermost parentheses:&lt;br /&gt;
:(RCIRC-2 01101) =&amp;gt; 01011&lt;br /&gt;
:(LCIRC-4 01011) =&amp;gt; 10101&lt;br /&gt;
:(RSHIFT-1 10101) = 01010&lt;br /&gt;
&lt;br /&gt;
=== Problem 3 ===&lt;br /&gt;
&lt;br /&gt;
List all possible values of x (5 bits long) that solve the following equation.&lt;br /&gt;
:(LSHIFT-1 (10110 XOR (RCIRC-3 x) AND 11011)) = 01100&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Since x is a string 5 bits long, represent it by abcde.  (RCIRC-3 x) is cdeab which, when ANDed with 11011 gives cd0ab.  This is XORed to 10110 to yield Cd1Ab (the capital letter is the NOT of its lower case).&lt;br /&gt;
Now, (LSHIFT-1 Cd1Ab) = d1Ab0 which has a value of 01100, we must have d=0, A=1 (hence a=0), b=0.  Thus, the solution must be in the form 00*0*, where * is an “I-don’t-care”.  The four possible values of x are: 00000, 00001, 00100 and 00101.&lt;br /&gt;
&lt;br /&gt;
=== Problem 4 ===&lt;br /&gt;
&lt;br /&gt;
Evaluate the following expression:&lt;br /&gt;
: ((RCIRC-14 (LCIRC-23 01101)) | (LSHIFT-1 10011) &amp;amp; (RSHIFT-2 10111))&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The problem can be rewritten as &lt;br /&gt;
: A | B &amp;amp; C&lt;br /&gt;
The AND has higher precedence than the OR.  &lt;br /&gt;
&lt;br /&gt;
The evaluation of expression A can be done in a straightforward way:  (LCIRC-23 01101) is the same as (LCIRC-3 01101) which has a value of 01011, and (RCIRC-14 01011) is the same as (RCIRC-4 01011) which has a value of 10110. Another strategy is to offset the left and right circulates.  So, ((RCIRC-14 (LCIRC-23 01101)) has the same value as (LCIRC-9 01101), which has the same value as (LCIRC-4 01101) which is also 11010.&lt;br /&gt;
&lt;br /&gt;
Expressions B and C are pretty easy to evaluate:&lt;br /&gt;
:B = (LSHIFT-1 10011) = 00110&lt;br /&gt;
:C = (RSHIFT-2 10111) = 00101&lt;br /&gt;
&lt;br /&gt;
The expression becomes&lt;br /&gt;
: A | B &amp;amp; C = 10110 | 00110 &amp;amp; 00101 = 10110 | 00100 = 10110&lt;br /&gt;
&lt;br /&gt;
== Video Resources ==&lt;br /&gt;
&lt;br /&gt;
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/IeMsD3harrE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/IeMsD3harrE &amp;#039;&amp;#039;Bit String Flicking (Intro)&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
A great two-part tutorial on this ACSL category. Part 1 covers bitwise operations AND, OR, NOT, and XOR. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/jbKw8oYJPs4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/jbKw8oYJPs4 &amp;#039;&amp;#039;Bit String Flicking Shifts and Circs&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CalculusNguyenify&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Part 2 covers logical shifts and circulate operations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/XNBcO25mgCw&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/XNBcO25mgCw &amp;#039;&amp;#039;Bit String Flicking&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Tangerine Code&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Shows the solution to the problem: (RSHIFT-3 (LCIRC-2 (NOT 10110)))&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/8J9AdxU5CW8&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/8J9AdxU5CW8 &amp;#039;&amp;#039;Bit String Flicking by Ravi Yeluru&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;hemsra&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Walks through two problems from the Junior Division.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;https://youtu.be/aa_lQ8gft60&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [https://youtu.be/aa_lQ8gft60 &amp;#039;&amp;#039;ACSL BitString Flicking Contest 2 Worksheet 1&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;misterminich&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
Solves a handful of problems given in previous years at the Intermediate Division level.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;youtube width=&amp;quot;300&amp;quot; height=&amp;quot;180&amp;quot;&amp;gt;URL&amp;lt;/youtube&amp;gt;&lt;br /&gt;
| [URL &amp;#039;&amp;#039;TITLE&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AUTHOR&amp;#039;&amp;#039;&amp;#039;)]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Williamhu</name></author>
	</entry>
</feed>