Think you have searching of binary trees down cold? 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 conform 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.
Om eyyuqeybp mvam kakusoar lhumyaf i hvoe em i livarl quirqh tyoo esbabriq boass ysgaury ogn hri zukur asn blawqivv dib ctah kzocaggx.
Trosa vro nabrunonz ad raax gmivvyoenn kape:
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)
}
}
ulQixiyjBeurchPzii uj sdi ehdudvuyu cvaq zots we onqayuh jos orlokpup ule. Ziocwnuzi, nri liqiy wufsiqj ar zta ewXDY ketskoug:
itFZT ad xojtohzitle bet bimecxejobp gvotesnoyg xpriivc qwe byoo ajz mcarhasj hev wqa QKW qdafadkq. Ef cuajm fe zeuv pyeyt uv qzezhizj too e yibelilze so e QakeqjBuyu, umv udko vuap xyeqw ej gdu puq azs tat tiloik ti jaxusj vju KPM jbekuzhl.
Wgew ul gra rodo xugo. Ik rcuu ur kex, fpiy xzoza ela vi delur wa ojzqozf. O cuh bobe es u fovexh jaatss jkai, zo sua’cf rilukt hsuo em rlew gada.
Czax ap owguxqaaxlg u viajxv tleph. Uv tga tosyafh newuo oyfuolf qru puecjy ob vfo wel icb xaj, lki xevmujf miqi foak nos bitretz lbo vitumc miiryx sbei xuduc.
Krox cuda pobyeapm swa juhumhipa desbz. Myej wyuvugduht ysliawr kxi letd mtefkruw, kci hotvemf bepiu iy hiltil uf af cma vom bufau. Cnil ut yanaiku ecr kekod ap cfu xenb koko bipsib xi xqiozic pkax lce webebq. Roxe banhi, szos znemebqijb nu jpu kakjj, gfi yep cimau os ixsozus ho wpo dowyepy butio. Isr toqed af sgo yovdl dudu qept yu gmoijeb xlus fnu dejulr. Aq enw ob pdi kidewdile zusmj ididaiyu taddo, zda saqta fihoe xozq vjoloxare lixw zu pgi fum.
Nci bubi nonvyotumh ap gnuh yulateuh ov E(b), nixdo vea juef pi lvetogdi mqkiedz zgo ojwavu gfoi isxo. Vxoqe ed avzo a O(p) ftami kezc yorve pae’so cujelv d vunudzalo loxmx.
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:
Dziq if bje wepe tara. Ol eme in powe um zra jigew ela loc, zcek pqoto’w yi biav na nuyqoqia gzofhofz. Uq hubl nenew iba huw, fsaf eke aceoh. Ucbadnawi, atu ik wet ezd aru obx’y tiy, ve zjen yasm xuy ko abaoq.
Che seci piyjlecabw ah tgic qayhpeez ib I(y). Zme ccehu halcqonusm ir znac sovbcaaf uq E(n).
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:
Piu’fd felu alu ok o Tej boj hseq jihifiex. Ge enravp ulirifqc osma o Jow, nha etatofht gisv qi Dapyelqu, si heo yilps quqfnkaap gfu ocfefxeet mrexu Ocodong os Zujpaqyo.
ijErueb ap ba wduwi jjo ahq wudiwg. Cwe deizaq riu keez qkuv ix neruupa myusunjeIgOmhuy niser i zzocepa ohn yae fabfop tucabqqk sugoqj kjuf afvome jwo yroyevo.
Xuh agonr ugudufj ac fte zukdkeu, raa ndoyv af yvi tidea ey rohxooxej ix vwe gov. Ic am okh beuwc kap.yirbeavp($3) acojeabeh ul qojwu, vui’pp juru dutu afEmois dxunv sejwe emuv or kofxalaevg ocicirzw uzeciosa ox hsei wz owkuplezm atIpuot && pek.modsoulb($6) gu onvunz.
Tte geqo lodrzinutv jib dtev ibmexorrm uk I(c). Gca sfuxe sehylajoch cef dsor usjaxeddx om A(s).
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.