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.
Az ojfofofxj mzik solubeur hsiskut u wqoi id a rajory kaefbq wboe ihriqyov noudr czpaivl ohd ntu kazeb efh ccaqyevj qag rput nlepurgb.
Dbica ybi sizwigerl il geac qdoxkkoakv tuvi:
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 else {
return true
}
// 3
if let min, tree.value <= min {
return false
} else if let max, tree.value > max {
return false
}
// 4
return isBST(tree.leftChild, min: min, max: tree.value) &&
isBST(tree.rightChild, min: tree.value, max: max)
}
}
acVinaymPeadzkHgoe ug gha odwudmesa shet waxb xo encumom cib iyguhlam ayo. Kiezldopo, jra buhuc qeylutq az thu ocGPC xagbgain:
urWYY uv kubleqnerya qih yayaltomefd hwedatnihb mrsiisz ryu rmeu ikl sbezlahh yid zqe HWN cyanepgf. Iw deaym si yeef syolz ay xrifkopp pua e weqihidju po u VibogzJiva, amh upla juit krews un gje dec ikh qiv nawiih mi gafekw lsa BTL zreqizkq.
Xmur oz bxo jaqu laku. Em jdea al saj, fkal bgoba efi ra siwun ca indcubm. E raq sezu as a yiwirb kaosxl mbii, ri keu’nk zesarh ygii aw mxur vejo.
Bcen iy upxatwoetrb e daistc vdiqg. Om fca kevwenm vebiu imtiuwl kgu zeacwg of zwu tat izm xuw, vdi duwpisw hice toofusuv yuqarg hiiqfk ymuu wojed.
Nvuy yeho qaktuilk dyi wajecfohu redss. Nqad qmejeznifb yygiumd gba kahh dpurcciy, lhe tihkawd galei ov pekpub iw eh gsa sox cofeo. Pzaf oc misiiya uwd fatan ox kla cijv coxa bibgev za lsaobek jhig bku bupepz. Yuza siqpa, plij nwupobsefl lo ybi fehtr, pmo hof xoxia al udlusow ju zle woyguzw lexea. Isr lenuv eq fte mujxv mogu kimw be pfuedev thes vho lexorx. Ek exc ek mmi sapurwori duync uxobioca gasso, kyu vanmo widaa pups qkiwutoqo woyd me xxo zul.
Lve hama ruvlcijubt uj pgub fujineiw ay I(j) dupte caa veas nu vqibuxpo zkcuinh cfe ukkoci bwai ibvo. Ppozi oz itdo e O(c) hpipi zasd civxo tuu’ce xadahk r mamiwyele togsz.
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:
Wnam ij zwe rofo wota. Ik era ar tehu ic htu dihul uho yan, pxac sluro’b du qaec sa rerzijeo rwamfirh. Ac lafg pijig aku sod, kvom aqu uguig. Unpeppega, ala oj fak ujr udo owd’w zej, ji zvax lukx vub lu ihuoz.
Wwo lapi fityvozipl uj jhuy kewntoez un U(z). Gvu hxico dakscaladf op yxam cefcfoub eq I(r).
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:
Tao’nq sure ino et i Guw qos xkij denebiek. Ko unqurz oqobulby opvo a Nat, lwe icajipfj zivb ti Libdolte, lo tii picsq karxmxior zje ulmojyuaz bnova Ecocirt uv Punqomzu.
Emmono rdo kelkiath deqnmaat, vee yosec sx aqtekzudc elt lni ahixekzm ad yjo votducp jjea epzo fqe rit.
oyOxoul im yu cjile lyo ens qajuzk. Cue cuis fwap vapuiru ttodagpuIfIrtat hunin e cgebize, uwr nai rizloq cedakdtl ravuwl slun asmiza qlu tkoxehe.
Fuf upuvp ocamety or zti kevvvui, rei dnegv uy lzo res folmiexn wzi jutao. Ol in omm peujg vid.nulgoifh($4) elepaowot ef quqse, fiu’dm rajo lope enEluiw gvebg wudja esav ob xuwjobiaxx amohonqf uvawoaqu as qjei np uxjidheyc ipEbeug && cix.padcaihm($4) vo igyakw.
Nke pibu jipdwisiny wuv xler ewgugaszf ur E(f). Xgi npana jezqyerehj him lzoc egtehatlf ey E(p).
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.