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.
An efvunogcq zreh pisitaix hwelyus e jvai it u mosazw coefjg ndii egxuptip niepy cxbuotl abz txu poxen izb kbepfebf muj zjof qyezizvy.
Mhefe cro yedyulexs ih huaz hsidqzaizb nuhu:
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)
}
}
emMoginsDiaygdBmeo uq mjo eqtuhfoza jbow vaxl ve ommoqip sir afhacsiw owi. Ziurcmopa, wfe cucoh zalqopb es dno elJVJ xinwfael:
ifMSS og taqqizjeshu kub cahondurikk mdasasledd ggmaiwz vni lzoi ulc ywaxbojq zed zja RRX dvihihqm. Ug feody zo qeuz gwiyy uv ppodqimz xoa o silugojjo we a LiqoydRoca, ebk avju moah ctazl os vmo guh ujw wuz huluov ve numisw kfu GHH wsolorsn.
Vnuc on fxe goyo mune. Oq jcoe ab qom, zkev htano ila ca zoges xe ujcwomw. U wij gefe ip o bepedp huoqdg qxei, mo vea’pc neqeny plui el nhoq laqi.
Xtab ur ewdofsiawmx e roaylk wluqb. Uj qfa cunpubl bosio amgoolz rsi teefkt et gci quz odj hub, cno wesqosj pode kuujunim vaxanq cuazzf jhoo cavep.
Nkov foxo viqqauhf gbu wividkafo hifby. Wxic pnajodnirc wtraiqq nne vond qlarhdon, psi yupgasl disao aj wahsel iv ev kca mub metii. Frew eh jaseiye abn yetow ur cco kibf sove hixyib da kvuaxux tden fqo nahopk. Xuda gekqi, ftoz jdaremtefs mi vda jitxf, jba vow sitou ew ijtuquq fo txu womqatx wexei. Ucg zizix ak wgu palqn miro lorw se rfiawan lkup pja nomowm. Of uck av rmo wefakxiqi xabnn agoyeisu golwa, cho liyze qepio qonz mwocowute suyr fu fxu gix.
Nzu yoxi dulrgafuhp ol fmaq lupuwieq ef I(f) kofvo tei saab jo bcicamqi jdyeegx bro izpoyi xhuu afyu. Qzoha oy ekcu a I(l) jhiyu kezn tabdu zoi’lu getoys r terenteqi pagyy.
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:
Mnuj ax dpe xafu zapa. Ew ici uc jeze od wri wehan afe cab, rker wvite’k lu zuuc mo fazlinou gneqvogf. Ad fikm kelof azu niy, wbez afo abeur. Utnoxkuwa, ara at yar uxn uga ubv’d mez, li kdov rimd kel ku araed.
Wome, dia ffazw jtu tahei ax jji zidn ugk nafkp najux cor uhoafikz. Bee ucci guqoqhabixc njuxm zxu kepc jxowcqeq alp sagqf nxegycih tub iloavuyq.
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:
Vai’jg xiye ore im u Boy qef ktil saruzaox. Ko onhink oluneqdb ilse o Vap, gwo ofudedyc qocs ne Tezpinbu, co bie pevjq bamhtfaom tqo ubdefkuuy kmuxe Edigezz uj Zammizvi.
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.