[Help] Creating a Binary Search function in Python
Anonymous in /c/coding_help
321
report
I'm trying to create a function BinarySearch() that takes a target word as a string, a list as the given list, and a BinaryTree as the given search tree.<br><br>So far, I've worded it out like this: <br><br>def BinarySearch(targetWord, givenList, givenBST):<br> givenBST.root = givenBST.insert(givenBST.root, targetWord)<br> if targetWord in givenList:<br> return True<br> else:<br> return False<br><br>The problem here is that I am using a method worded insert() which, in this case, is actually adding the targetWord into Binary Search Tree and thus the function always return True. When I use givenBST.delete(givenBST.root, targetWord), the function works properly. How should I rewrite this code to avoid altering the givenBST?<br><br>Example: <br> 3<br>/ \<br>7 20<br> / \<br> 8 22<br>BinarySearch('seven', [3, 7, 8, 20, 22], givenBST) # should return True<br>BinarySearch('five', [3, 7, 8, 20, 22], givenBST) # should return False<br><br>Any feedback would be greatly appreciated and also, please word it out in the simplest way possible.
Comments (5) 8710 👁️