Advertisement
Guest User

BST

a guest
Sep 24th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. """ Node is defined as
  2. class node:
  3.    def __init__(self, data):
  4.        self.data = data
  5.        self.left = None
  6.        self.right = None
  7. """
  8.  
  9. def checkBST(root):
  10.     max = 10000
  11.     min = 0
  12.     data = {root.data}
  13.    
  14.     def checkBounds(data, leftBound, rightBound):
  15.         #Case 1 - Not a BST
  16.         return False if data < leftBound or data > rightBound else True
  17.  
  18.    
  19.     def evaluateNode(node, lowerBound, upperBound):
  20.    
  21.         #Case 2 - Not a unique BST
  22.         if node.data in data:
  23.             return False
  24.         else:
  25.             data.add(node.data)
  26.        
  27.         if not checkBounds(node.data, lowerBound, upperBound): return False
  28.        
  29.         if node.left is not None:
  30.             if not evaluateNode(node.left, lowerBound, node.data): return False
  31.                
  32.         if node.right is not None:
  33.             if not evaluateNode(node.right, node.data, upperBound): return False
  34.  
  35.         return True
  36.    
  37.     #Left Side
  38.     if not evaluateNode(root.left, min, root.data): return False
  39.        
  40.    
  41.     #Right Side
  42.     if not evaluateNode(root.right, root.data, max): return False
  43.        
  44.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement