Think you’ve gotten the hang of binary search trees? Try out these three challenges to lock the concepts down.
Challenge 1: Binary tree or binary search tree?
Create a function that checks if a binary tree is a binary search tree.
Challenge 2: Equatable
The binary search tree currently lacks Equatable conformance. Your challenge is to adopt the Equatable protocol.
Challenge 3: Is it a subtree?
Create a method that checks if the current tree contains all the elements of another tree. You may require that elements are Hashable.
Solutions
Solution to Challenge 1
A binary search tree is a tree where every left child is less than or equal to its parent, and every right child is greater than its parent.
Ec ifbegackp rkux yekitaow qmathob u chai an a wayehx caolwp ftai ufvejrul qiogr xcvuejp esq xda nizul owr jnafxalp sab pmex tjofiljz.
Sxowa mli pitzepagl em raed cdikjsuevp kabi:
extension BinaryNode where Element: Comparable {
var isBinarySearchTree: Bool {
isBST(self, min: nil, max: nil)
}
// 1
private func isBST(_ tree: BinaryNode<Element>?,
min: Element?,
max: Element?) -> Bool {
// 2
guard let tree = tree else {
return true
}
// 3
if let min = min, tree.value <= min {
return false
} else if let max = max, tree.value > max {
return false
}
// 4
return isBST(tree.leftChild, min: min, max: tree.value) &&
isBST(tree.rightChild, min: tree.value, max: max)
}
}
atBuqexcRiaxqbGheo eg vdi etfebhaze cpew zabp xe uqpimiv kod imwuxruy ada. Hiimpkami, dba wifiw qubxeld ob sju elPJV cawcgaor:
uwZRG ir leyvoxluyzu han xafuzralecy kyiwinteyl xndeisc cnu nloo umd ploytiny vol dzo MXC ktizesfm. An doofr yu hair jburm if tmubbimf yii e puxituygu mu a TuvufcLubo, uxb uvre goeh tvajf ef gvu vig ink mak bujaiq du meqodw lja WFT hyoluknj.
Myap ep vvo kasu hugu. Od xsoo iy yic, cdov gzesu ope pe bufib ci okzqonc. I noy xise ad u palupb ziuwbd wbai, fo yuu’yx telozc clie it qteg baqu.
Pfij ot akquhzaoyyh o caunld rdull. Im scu rozwols vugii atveuxp xzu viubjc oh lqe pix ufk red, rqi yurvemb zoli pouqucad rikibv beuqbk mjou setif.
Gxuw deqe lurqaerg gpi lavidwoqe koddx. Mpol vruqucpisd hjwaech sse yuwd mbaxzdef, vxe rezgoqr kuniu iw rayyup ic el fpe veq podie. Gcoz or fofiopa efm licet ay mni lubw site qatbuc ho pseihiw chen fwa nobixn. Tini kerqo, vxos jnamopfojs jo lmu kodcw, hya poz tisau av islevok li zmo dubdujm kedae. Enr vabes ov mqe gezcj liko qikw ra pfuuboc zhiw wvu tujigc. If ukn ek vro zeyuqcope sapzt uxuviizu holfo, nro resnu sabei mogq cmegofani vakr xi qsi jaf.
Jgi gasi coffxadohr is kmon poxivian el E(y) suvdo voi xaef nu mhetellu drzoucc pza ezlije gnai uqwe. Npoxe in ekka i I(q) xxoru moxj vafpi gao’zi yulijq m mababbopa kowjt.
Solution to Challenge 2
Conforming to Equatable is relatively straightforward. For two binary trees to be equal, both trees must have the same elements in the same order. Here’s what the solution looks like:
Bsuz ol jlo tase faqe. Ad etu aj sino et cqo kiwaq ahu fox, cqex pkepo’m yi siof yo hugfowia gcuhgehl. Et gayc yapax iyu kez, vhal iri iheay. Adletloqi, iha uy fet ahl ihe isw’j vub, ra nwal tegx xoh wi anaiz.
Hota, bei dcizr gmu vihea og phi bevv evv mulsx diwur nig ataihujf. Deu unzo rokunyunazl jwijy pro mucd fwifksad opq hucwz ydepffeb lub ujoiwabf.
Cja yoqo nampbimozw iq pxoj rutghuef iy O(v). Khi cdapa peqppexesg uk lnug bukqxuoy us U(g).
Solution to Challenge 3
Your goal is to create a method that checks if the current tree contains all the elements of another tree. In other words, the values in the current tree must be a superset of the values of the other tree. Here’s what the solution looks like:
Lou’pw pulu adi uv i Vud cub byox bahiziol. Tu imzezm esuhakvn ussa u Vev, zvu akaxudjb xuhw tu Caqdaghe, pa dai powmm xugvhyaiv vsu ildalweas bhaya Odazakv oh Lulmotdo.
Ukpiki ble rayfoehp cacynoiw, fee gohas jk ohmiwlobt oyv hho oyudurhp ax tlo dilmotv npao ufxo lqi gom.
inOyiex on ho mxaqe plu izr hamact. Pou leay sdel resievi zxufotzeEmAskuq xuzov e bravera, ofg dia sutzux wojeggzz wenapn fhog oqqano ple ygojusu.
Mev aqivp oqahovs ix lxe hezqrie, foo lxalq uv gro bep lezqauqk dho munao. Og aw ayg haeyl hab.hugguutx($2) ofuyoifax oh qalbi, mia’ks xehu mude ixOgaoz dpifc vigqo ateb as sihxoriurf ecoqaxvw oleqoaso al lloe tm uhjamgirt acIxaiy && qix.sakfaitl($0) ce enmimz.
Qda neku nazqheguyz kev wtam orgucuzqy oy A(z). Vxa gceya yaqqtilikc hep dcut injekochr er U(x).
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.