Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see four errors in your code.</p> <ol> <li><p>First, your check for null children is backwards in <code>tree_min</code>. That is, you're checking if <code>node.right</code> exists before accessing <code>node.left</code>, and vise versa.</p></li> <li><p>Second, <code>tree.min</code> returns negative infinity when called on a leaf node. You need to use positive infinity in the min calculation (negative infinity is correct in the max version).</p></li> <li><p>Third, you have a logic error within <code>verify</code>, as it unconditionally calls <code>tree_min</code> or <code>tree_max</code> and itself on it's child nodes, even if one or both of them are <code>None</code>. I suggest making all the functions handle being passed <code>None</code>, rather than relying on the caller to do the right thing. This also simplifies the <code>min</code> and <code>max</code> code a bit!</p></li> <li><p>Lastly, you're doing your comparisons on <code>node.value</code>, which is the string you're giving each node. I suspect you want to be comparing using <code>node.key</code> instead. Comparing a float (like <code>float("-inf")</code>) to a string (like <code>"ten"</code>) is an error in Python 3, and even in Python 2 where it is legal, it probably doesn't work like you would expect.</p></li> </ol> <p>With those issues fixed, I get expected results when I create valid and invalid trees. Your two examples are both invalid though, so if you were using them to test, you will always get a <code>False</code> result.</p> <p>Finally, a couple of minor style issues (that aren't bugs, but still things that could be improved). Python supports chained comparisons, so you can simplify your first <code>if</code> statement in <code>verify</code> to <code>tree_max(node.left) &lt;= node.key &lt;= tree_min(node.right)</code>. You can further simplify that part of the code by connecting the checks with <code>and</code> rather than nesting an additional <code>if</code> statement.</p> <p>Here's a version of your code that works for me (using Python 3, though I think it is all backwards compatible to Python 2):</p> <pre><code>class Node: def __init__(self, k, val): self.key = k self.value = val self.left = None self.right = None def tree_max(node): if not node: return float("-inf") maxleft = tree_max(node.left) maxright = tree_max(node.right) return max(node.key, maxleft, maxright) def tree_min(node): if not node: return float("inf") minleft = tree_min(node.left) minright = tree_min(node.right) return min(node.key, minleft, minright) def verify(node): if not node: return True if (tree_max(node.left) &lt;= node.key &lt;= tree_min(node.right) and verify(node.left) and verify(node.right)): return True else: return False root= Node(10, "Hello") root.left = Node(5, "Five") root.right= Node(30, "Thirty") print(verify(root)) # prints True, since this tree is valid root = Node(10, "Ten") root.right = Node(20, "Twenty") root.left = Node(5, "Five") root.left.right = Node(15, "Fifteen") print(verify(root)) # prints False, since 15 is to the left of 10 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload