SHARE
TWEET

Untitled

a guest Dec 14th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type
  2.     BinarySearchTree = ref object
  3.         Value: int
  4.         LeftNode, RightNode: BinarySearchTree
  5.  
  6. proc isValidBinarySearchTree(tree : BinarySearchTree, lowerBound : int, upperBound : int) : bool =
  7.     if isNil(tree):
  8.         return true
  9.     elif tree.Value > upperBound or tree.Value < lowerBound:
  10.         return false
  11.     elif not isValidBinarySearchTree(tree.LeftNode, lowerBound, tree.Value):
  12.         return false
  13.     elif not isValidBinarySearchTree(tree.RightNode, tree.Value, upperBound):
  14.         return false
  15.     else:
  16.         return true
  17.  
  18. let example1 =
  19.     BinarySearchTree(Value: 5,
  20.         LeftNode: BinarySearchTree(Value: 1),
  21.         RightNode: BinarySearchTree(Value: 4,
  22.             LeftNode: BinarySearchTree(Value: 3),
  23.             RightNode: BinarySearchTree(Value: 6)))
  24.  
  25. let example2 =
  26.     BinarySearchTree(Value: 5,
  27.         LeftNode: BinarySearchTree(Value: 1),
  28.         RightNode: BinarySearchTree(Value: 6,
  29.             LeftNode: BinarySearchTree(Value: 4),
  30.             RightNode: BinarySearchTree(Value: 7)))
  31.  
  32. let example3 =
  33.     BinarySearchTree(Value: 5,
  34.         LeftNode: BinarySearchTree(Value: 1),
  35.         RightNode: BinarySearchTree(Value: 6,
  36.             LeftNode: BinarySearchTree(Value: 7),
  37.             RightNode: BinarySearchTree(Value: 8)))        
  38.  
  39. let example4 =
  40.     BinarySearchTree(Value: 5,
  41.         LeftNode: BinarySearchTree(Value: 1),
  42.         RightNode: BinarySearchTree(Value: 7,
  43.             LeftNode: BinarySearchTree(Value: 6),
  44.             RightNode: BinarySearchTree(Value: 8)))        
  45.  
  46. echo "Example1: ", isValidBinarySearchTree(example1, low(int), high(int))
  47. echo "Example2: ", isValidBinarySearchTree(example2, low(int), high(int))
  48. echo "Example3: ", isValidBinarySearchTree(example3, low(int), high(int))
  49. echo "Example4: ", isValidBinarySearchTree(example4, low(int), high(int))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top