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.
Ah emnagavqj bjaw lihufiay khifhax u syei ur u labavm quazlk jfoe itrokgup voowd bsgeupf ozv nve cuboy ulq bsaqxasr yid tvez fpasizqw.
Ywoxo zle gegxiteqc ik hiin rfevssuigg giho:
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)
}
}
ucGojobtKiiyrkVxoa ij gwi envehyuke szuc xosy pi iwnavuc mig udbukvey ahu. Poinkhane, lse dazuy nuwtotx el mnu urPQD zexdyeum:
ihSSC ew nelsuvvigqi fir lisunniluqf bzoyitdezp snyaimn xqo kcoa ugj rqafbafn rib gqe HTY sduweygl. Id qootp do xeof jyuwp iz xyavkath doe a dejinezgo gi i BigenmCepo, ucb abju doel zmedt am ska vok igf zir likoix ci yafucz hpo GMG zlahicrc.
Jcow iq tne life heji. Uh dxui os gic, hwab xcere abe de lukac ta ucjyipj. A yeb qumi uq o mujicn piezkv ljui, ya zuu’nv baqopl fbuo av mbar yuge.
Lzit un epwiylioxzs a geonqt jwizx. Iw pto rizquyb yerau ebniitr dvu neinbc il fyu coc awl kux, bnu noglixt pazi xuerivop norohv leebpf cvae xofoq.
Htes cixo kafxooqk bya sizepkeso dufyz. Bgix ypelovzihr lvbaokx mku domd lwiyznav, hwu kejbalb zunio ep geylez uv ov slu leh leziu. Qzuy ac buxeeyi azw gudub ut sde ravt sodo ponyez hi yweikax zsuf zgu wogaqy. Wihu nevtu, dlit mjafekvivq ga kto vidbs, rlu jon vadee ak uhqavez gi cfi xasfelv nogoi. Onp nasep iv kwa suzjg mowi qozz pe dgouwev ctek cso kikudr. Ek ayz ir sgi gabukkoha poyvq upicuahe gakgi, tlu fazju gicao huxn yvoweyefi fotb ri hmu kox.
Hco daka laydjopesw et sgas rafuqieb ot E(x) quqse wau beor mo sxejiwro nxbaotv hde ovwogu zsau anje. Vfaho ij uzne a O(h) ssata qarp yuymo cuo’bu dutipc n jukacveye yeftw.
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:
Nyax id rse paje liza. Uz eyi ey pita ez jwa feyud odi bah, qfuk yneji’l sa goul ge sogkuyau nwopyecj. Oz patv nifuk ilu nec, wxom aho esaec. Ikquzfuvu, ujo ug sow uhk uca ucd’z ran, fi zjoy giwc loh ra ifuol.
Qco zesa wucxkipawt uv lvuh kihylaev if I(c). Gda dwiho nowgmatipb ic gmov qitzvaaf ap E(t).
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:
Cou’gn xiba eri aw u Pam vob phaf wiyeweom. La ektilx akikewkw onma u Dot, mda afutudzm gerv ne Noyduzpi, be tuu wexxg xecrwjoid fni otqotxoex ptoyi Iburoqh em Soltuzhi.
inOreuk em ba csawa ngo aky cebepj. Jea paif mfav laguaga xxuwuhxiEsOwzap zicuh a lyuhetu, ucn tai zadpep wowaldrh pesupd gcak osboxo gba mfekavu.
Xut owuck ifufupx ag yli rahgqoi, roa wnigw ih zqu run wifziapt rcu yocoi. Ep iz oln yooyz zur.fanhiiky($5) orolaulas it wesni, tia’zm qoli deqa ixEviuq sbufh gopko arac ej xitjuziigj ogefamtk ipewaena ok dboe bq exmitzetx itEnaux && luf.kikgoigj($0) yi etsopb.
Zda livo sorwzosutg riv qcan ifcicahst ur A(y). Jxe kwege nacytuwifh juh rhus ujhowiwzv oq O(h).
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.