<#16954#>(define-struct<#16954#> <#16955#>node<#16955#> <#16956#>(ssn<#16956#> <#16957#>name<#16957#> <#16958#>left<#16958#> <#16959#>right))<#16959#>Here we have decided to record the social security number, the name, and two other trees. The latter are like the parent fields of family trees, though the relationship between a <#63423#><#16963#>node<#16963#><#63423#> and its <#63424#><#16964#>left<#16964#><#63424#> and <#63425#><#16965#>right<#16965#><#63425#> trees is not based on family relationships. The corresponding data definition is just like the one for family trees:
A <#63426#><#16967#>binary tree<#16967#><#63426#> (<#63427#><#16968#>BT<#16968#><#63427#>) is eitherThe choice of <#63435#><#16983#>false<#16983#><#63435#> to indicate lack of information is arbitrary. We could have chosen <#63436#><#16984#>empty<#16984#><#63436#> again, but <#63437#><#16985#>false<#16985#><#63437#> is an equally good and an equally frequent choice that we should become familiar with. Here are two binary trees:
- <#63428#><#16970#>false<#16970#><#63428#> or
- <#63429#><#16971#>(make-node<#16971#>\ <#16972#>soc<#16972#>\ <#16973#>pn<#16973#>\ <#16974#>lft<#16974#>\ <#16975#>rgt)<#16975#><#63429#>
where <#63430#><#16976#>soc<#16976#><#63430#> is a number, <#63431#><#16977#>pn<#16977#><#63431#> is a symbol, and <#63432#><#16978#>lft<#16978#><#63432#> and <#63433#><#16979#>rgt<#16979#><#63433#> are <#63434#><#16980#>BT<#16980#><#63434#>s.
<#16989#>(m<#16989#><#16990#>ake-node<#16990#> <#16991#>15<#16991#> <#16992#>'<#16992#><#16993#>d<#16993#> <#16994#>false<#16994#> <#16995#>(make-node<#16995#> <#16996#>24<#16996#> <#16997#>'<#16997#><#16998#>i<#16998#> <#16999#>false<#16999#> <#17000#>false<#17000#><#17001#>))<#17001#>~~
<#17007#>(m<#17007#><#17008#>ake-node<#17008#> <#17009#>15<#17009#> <#17010#>'<#17010#><#17011#>d<#17011#> <#17012#>(make-node<#17012#> <#17013#>87<#17013#> <#17014#>'<#17014#><#17015#>h<#17015#> <#17016#>false<#17016#> <#17017#>false<#17017#><#17018#>)<#17018#> <#17019#>false<#17019#><#17020#>)<#17020#>
Figure~#figbst#17023>
<#17029#>Exercise 14.2.1<#17029#>
Solution<#63452#><#63452#>
<#17052#>Figure: A binary search tree and a binary tree<#17052#>
The sequence for tree A is sorted in ascending order, the one for B is not. A binary tree that has an ordered sequence of information is a <#63453#><#17060#>BINARY SEARCH TREE<#17060#><#63453#>. Every binary search tree is a binary tree, but not every binary tree is a binary search tree. We say that the class of binary search trees is a <#63454#><#17061#>PROPER SUBCLASS<#17061#><#63454#> of that of binary trees. To define the class of binary search trees rigorously, we formulate a condition that distinguishes a binary search tree from a binary tree:
The second and third condition are different from what we have seen in previous data definitions. They place an additional and unusual burden on the construction <#63471#><#17091#>BST<#17091#><#63471#>s. We must inspect all numbers in these trees and ensure that they are smaller (or larger) than <#63472#><#17092#>soc<#17092#><#63472#>.<#17064#>The BST Invariant<#17064#> A <#63455#><#17065#>binary search tree<#17065#><#63455#> (<#63456#><#17066#>BST<#17066#><#63456#>) is a <#63457#><#17067#>BT<#17067#><#63457#>:
- <#63458#><#17069#>false<#17069#><#63458#> is always a <#63459#><#17070#>BST<#17070#><#63459#>;
- <#63460#><#17071#>(make-node<#17071#>\ <#17072#>soc<#17072#>\ <#17073#>pn<#17073#>\ <#17074#>lft<#17074#>\ <#17075#>rgt)<#17075#><#63460#> is a <#63461#><#17076#>BST<#17076#><#63461#> if
- <#63462#><#17078#>lft<#17078#><#63462#> and <#63463#><#17079#>rgt<#17079#><#63463#> are <#63464#><#17080#>BST<#17080#><#63464#>s,
- all <#63465#><#17081#>ssn<#17081#><#63465#> numbers in <#63466#><#17082#>lft<#17082#><#63466#> are smaller than <#63467#><#17083#>soc<#17083#><#63467#>, and
- all <#63468#><#17084#>ssn<#17084#><#63468#> numbers in <#63469#><#17085#>rgt<#17085#><#63469#> are larger than <#63470#><#17086#>soc<#17086#><#63470#>.
<#17105#>(append<#17105#> <#17106#>(list<#17106#> <#17107#>1<#17107#> <#17108#>2<#17108#> <#17109#>3)<#17109#> <#17110#>(list<#17110#> <#17111#>4)<#17111#> <#17112#>(list<#17112#> <#17113#>5<#17113#> <#17114#>6<#17114#> <#17115#>7))<#17115#> <#17116#>=<#17116#> <#17117#>(list<#17117#> <#17118#>1<#17118#> <#17119#>2<#17119#> <#17120#>3<#17120#> <#17121#>4<#17121#> <#17122#>5<#17122#> <#17123#>6<#17123#> <#17124#>7)<#17124#>What does <#63476#><#17128#>inorder<#17128#><#63476#> produce for a binary search tree?~
<#17148#>(make-node<#17148#> <#17149#>66<#17149#> <#17150#>'<#17150#><#17151#>a<#17151#> <#17152#>L<#17152#> <#17153#>R)<#17153#>If we are looking for <#63486#><#17157#>66<#17157#><#63486#>, we have found it. Now suppose we are looking for <#63487#><#17158#>63<#17158#><#63487#>. Given the above <#63488#><#17159#>node<#17159#><#63488#>, we can focus the search on <#63489#><#17160#>L<#17160#><#63489#> because <#17161#>all<#17161#> <#63490#><#17162#>node<#17162#><#63490#>s with <#63491#><#17163#>ssn<#17163#><#63491#>s smaller than <#63492#><#17164#>65<#17164#><#63492#> are in <#63493#><#17165#>L<#17165#><#63493#>. Similarly, if we were to look for <#63494#><#17166#>99<#17166#><#63494#>, we would ignore <#63495#><#17167#>L<#17167#><#63495#> and focus on <#63496#><#17168#>R<#17168#><#63496#> because <#17169#>all<#17169#> <#63497#><#17170#>node<#17170#><#63497#>s with <#63498#><#17171#>ssn<#17171#><#63498#>s larger than <#63499#><#17172#>65<#17172#><#63499#> are in <#63500#><#17173#>R<#17173#><#63500#>.
<#17229#>(make-node<#17229#> <#17230#>N<#17230#> <#17231#>S<#17231#> <#17232#>false<#17232#> <#17233#>false<#17233#><#17234#>)<#17234#>Test the function with <#63536#><#17238#>(create-bst<#17238#>\ <#17239#>false<#17239#>\ <#17240#>66<#17240#>\ <#17241#>'<#17241#><#17242#>a)<#17242#><#63536#>; this should create a single <#63537#><#17243#>node<#17243#><#63537#>. Then show that the following holds:
<#17248#>(create-bst<#17248#> <#17249#>(create-bst<#17249#> <#17250#>false<#17250#> <#17251#>66<#17251#> <#17252#>'<#17252#><#17253#>a)<#17253#> <#17254#>53<#17254#> <#17255#>'<#17255#><#17256#>b)<#17256#> <#17257#>=<#17257#> <#17258#>(make-node<#17258#> <#17259#>66<#17259#> <#17260#>'<#17260#><#17261#>a<#17261#> <#17262#>(make-node<#17262#> <#17263#>53<#17263#> <#17264#>'<#17264#><#17265#>b<#17265#> <#17266#>false<#17266#> <#17267#>false<#17267#><#17268#>)<#17268#> <#17269#>false<#17269#><#17270#>)<#17270#>Finally, create tree A from figure~#figbst#17274>
A <#63543#><#17287#>list of numbers and names<#17287#><#63543#> (<#63544#><#17288#>list-of-number/name<#17288#><#63544#>) is eitherConsider the following examples:
- <#63545#><#17290#>empty<#17290#><#63545#> or
- <#63546#><#17291#>(cons<#17291#>\ <#17292#>(list<#17292#>\ <#17293#>ssn<#17293#>\ <#17294#>nom)<#17294#>\ <#17295#>lonn)<#17295#><#63546#>
where <#63547#><#17296#>ssn<#17296#><#63547#> is a number, <#63548#><#17297#>nom<#17297#><#63548#> a symbol,
and <#63549#><#17298#>lonn<#17298#><#63549#> is a <#63550#><#17299#>list-of-number/name<#17299#><#63550#>.
They are equivalent, although the left one is defined with the quote abbreviation, the right one using <#63551#><#17377#>list<#17377#><#63551#>. The left tree in figure~#figbst#17378><#17306#>(d<#17306#><#17307#>efine<#17307#> <#17308#>sample<#17308#> <#17309#>'<#17309#><#17310#>(<#17310#><#17311#>(99<#17311#> <#17312#>o)<#17312#> <#17313#>(77<#17313#> <#17314#>l)<#17314#> <#17315#>(24<#17315#> <#17316#>i)<#17316#> <#17317#>(10<#17317#> <#17318#>h)<#17318#> <#17319#>(95<#17319#> <#17320#>g)<#17320#> <#17321#>(15<#17321#> <#17322#>d)<#17322#> <#17323#>(89<#17323#> <#17324#>c)<#17324#> <#17325#>(29<#17325#> <#17326#>b)<#17326#> <#17327#>(63<#17327#> <#17328#>a)))<#17328#><#17334#>(d<#17334#><#17335#>efine<#17335#> <#17336#>sample<#17336#> <#17337#>(list<#17337#> <#17338#>(list<#17338#> <#17339#>99<#17339#> <#17340#>'<#17340#><#17341#>o)<#17341#> <#17342#>(list<#17342#> <#17343#>77<#17343#> <#17344#>'<#17344#><#17345#>l)<#17345#> <#17346#>(list<#17346#> <#17347#>24<#17347#> <#17348#>'<#17348#><#17349#>i)<#17349#> <#17350#>(list<#17350#> <#17351#>10<#17351#> <#17352#>'<#17352#><#17353#>h)<#17353#> <#17354#>(list<#17354#> <#17355#>95<#17355#> <#17356#>'<#17356#><#17357#>g)<#17357#> <#17358#>(list<#17358#> <#17359#>15<#17359#> <#17360#>'<#17360#><#17361#>d)<#17361#> <#17362#>(list<#17362#> <#17363#>89<#17363#> <#17364#>'<#17364#><#17365#>c)<#17365#> <#17366#>(list<#17366#> <#17367#>29<#17367#> <#17368#>'<#17368#><#17369#>b)<#17369#> <#17370#>(list<#17370#> <#17371#>63<#17371#> <#17372#>'<#17372#><#17373#>a)))<#17373#>