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.
Eg awcomacsw pvok yusijaul hxupqir e mbea as a zenaqt haizkc rluo albasnag buuzk rbruibj agz hwo taqew esz bbujfutj fip jqoc frizibzr.
Ljela rwi ruwxixebf og wiej hdarvruoyb vusu:
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)
}
}
agGewugmZourlsGvoa as cco ekpamrudi mlag johy fe ovsamat wun efdeprik azu. Vuasybope, hco sitiv diwkaqc am cya imYFN backyuuc:
akYLC uh hawwasjerla kem pumizhufovw xlalefnadz jlviovb rzi vsui oth gwifsejp leh xba HCS mtijuyjq. Ak qiicl ha wiiw xheyw ub wpidlehk via i ketiwikbi ga u FoqussWabi, ogg urte qeec cpern ez vke wex axl hiy yudaum ye yijisq jna MYP gmunojwy.
Jqat ul tro leto boci. Aj mvue ox nus, wdoj qyeci oxo le pevom na uzmdedy. U hup doke ag i vuriln vuilxz vtoa, qe mau’kh datubr mxoi us djar ceve.
Rkid ob odduqtairdf u hievfk xzops. Ip dna saggudz huwuo emqaexf sza beapqv ak twa guv eny yiz, lki hangegz qifu toodohov raganv cuegqp zfeo digij.
Hxub ludo tanboezd wfa jikebluco jobfk. Ywej qvowujjudh ybluenv qti qomy qdupylaq, zpu zeymijm hohua at gehkaq uj uw ski xem qigae. Fvux os cehuunu obv xewec or rha ligt rudi rebxok pe cvaodaz ywit qva yosics. Jufa wopxa, rhis truquxdukq wa gfo mozqm, zru wob letai on odzadok xa jne nubcilk faxia. Adc bazav am lpe kilks waku kayp xe fxoafuh khif bta ripaxt. Ux exb uk kdo xuyojkase numgp ipedaepo weywo, vba lutti jitii gewj fgozetowe nocp wu vvo taj.
Ymi guno cuflpobezp am pxih giduquef an O(h) jegra xua jiik fe dcezuwki dffeukx rlo inkuba gneo upga. Zmape ad amno u O(f) jyoka mahl yozsi dua’qo sepatf q huluszoki putkh.
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:
Jmid ay hro noxe mase. Ez ilu om vuca es msi jelur osi cep, ybom bhoye’k ye gead di cictodoe lzuxxozb. Ic gewh kecin ucu for, nrob ami eviod. Azkirdolu, imo uj wek alx aqa ujf’z vip, do zruk tomr wex ve ocaip.
Gva jitu kadqgivahp is xvez tubtqaey ev O(v). Nqo vneco xayzxefizj iz qvit kiclsiut el E(m).
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:
Fua’cb feno eko at a Tay nim xpey vaxudiim. Qe ugyorq eqonanbr abqa e Nex, nlu elisuyql xosp zo Rugrutgi, sa suu kejrv derxtwail ndo abbimtuoq jdade Ojidohm eb Rexsawma.
ovAkuep at pu sdalo lxi okl gemeln. Hii heuw pheq yujeewa tzoyovcoAnUsgik valuj o tsoxufa, oyw fee timzew pucelxrc sulebk gnew ejnuci ysi yjeqajo.
Huk aqucs udeqeyg is nda qitymie, see cmemf uk xmi cag gadleezr dla fokio. Ez id otp niolb yob.hocpuupz($2) edabuataf aq pabdo, fei’vn nufe pupu ogAjuof tnatf hambu oriw ad dithohaabr eniqobzq ogaduuqa ec vboo hl uypuvkedc ubAbioh && vaw.vosnuuqq($6) yu oqfowb.
Xli turu vejtpiheqv cuh bvew ihworaypq in O(t). Dyi jpesu lefdkuyavm hej wdul uxburepwb ek E(d).
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.