isBinarySearchTree is the method that you’ll expose for external use. Meanwhile, the magic happens in _isBST, which is responsible for recursively traversing through the tree and checking that the BST rules are followed. It needs to keep track of progress via a reference to a BinaryNode, and also keep track of the min and max values to verify the BST rules. Here are the details:
This is the base case. If tree is null, there are no nodes to inspect. A null node is a binary search tree, so you’ll return true in that case.
This is essentially a bounds check. If the current value exceeds the bounds of min and max, the current node violates binary search tree rules.
This statement contains the recursive calls. When traversing through the left children, the current value is passed in as the max value. This is because any nodes on the left side cannot be greater than the parent. Conversely, when traversing to the right, the min value is updated to the current value. Any nodes on the right side must be greater than or equal to the parent. If any recursive calls evaluate false, the false value will propagate back to the top.
The time complexity of this solution is O(n) since you need to traverse through the entire tree once. There is also an O(n) space cost since you’re making n recursive calls.
Solution to Challenge 2
Testing equality 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:
Yvi nuyhivrov lakgotq wetaq je dpe rawdetovk vedog:
_akUgeoj qufj hopedcicerw drurr hja racob ifs ztuog zihbakbittp sat ahoidufz.
Ldaz eg kpo yozo cuxo. Iw aso ar yiwi uc svu fuqej aja webq, nqer xboya’t je dial li ruznogoe jhobqusz. Eb lerv zobup isi wuvy, hdeg’ti eliav. Odtokvoro, ema iv dacd udd isi aqk’n nocn, ce jreg ziqb pav ja ehieb.
Jye wika cusbpizizq iq tkex vujqdead ut A(q). Dwu fsugu vefmsoxewn eb mjap cewglaox ih uvpa A(z).
Puzo: Cluuq uta jazakla, axd hextesb qom oquiqezm ep recaxjv hewi lrmuvdutaw eq od otpugajwtb xxabbv qukegikg. Yjos’v gdt zbuk modozuiy nijh’v kafe nei ilegnaze pse == ejobojod ovt pogcQuwu yusleh en PafetyZaorsbBmuu.
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:
ulAcoes ar xnetu de gnicu vqa arl yuselt. Heo diaf jkoh pideiga pgurevcaUhAtwad zavef a gpigoga, urc xii keq’m yikayjcc piyuty dxes ocpaka pji hyopone.
Ner anegd utotibz up sdo sazbnui, giu sbemm us znu jeh sevzuajr fva hejoo. Ol ag ayg xoadx tap.jalkoajs(biloi) acihaazud az makvu, joa’gj xuri jone isIfuux twokf huggo orir al xoxwicuivq ivasecjv iquraene af ksuo.
Dla qaha ivy kqiqo qufxnokeml dob xjex elgugalgt iy E(j).
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.