Advertisement
djangopdx

isthisbinary.py

Aug 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #!/bin/python
  2. """ Using In Order Traversal of a potentially Binary Search Tree to determine if the tree is in fact a Binary Search Tree.
  3. The data value of every node's left subtree must be less than the data value of the node
  4. The data value of every node's right subtree must be greater than the data value of the node
  5. Our definition states that a binary search tree may not have any duplicate values
  6.  
  7. The method that I am using is create an in order traversal of the tree and compare that to a sorted list of the same node data. If they are the same then the tree is a binary search tree.
  8.  
  9. 1) Create an empty stack S.
  10. 2) Initialize current node as root
  11. 3) Push the current node to S and set current = current->left until current is NULL
  12. 4) If current is NULL and stack is not empty then
  13.     a) Pop the top item from stack.
  14.     b) Append the popped item's data value to the in order list, set current = popped_item->right
  15.     c) Go to step 3.
  16. 5) If current is NULL and stack is empty then we are done.
  17. """
  18. global_data_values_set = set()
  19. # above line creates an empty set to hold every data value as the tree is traversed
  20. visited_nodes_set = set()
  21. # above line creates an empty set to hold a pointer to every node as it is visited to ensure that no node is visited twice.
  22.  
  23. class node:
  24.     def __init__(self, data):
  25.         self.data = data
  26.         self.left = None
  27.         self.right = None
  28.  
  29. def check_node(n):
  30.     # function assumes that an object of node type is the argument
  31.     in_order_list = []
  32.     stack = []
  33.     current_node = n
  34.     done = False
  35.     while not done:
  36.         if current_node is not None:
  37.             stack.append(current_node)
  38.             current_node = current_node.left
  39.         elif current_node is None and len(stack) != 0:
  40.             current_node = stack.pop()
  41.             if current_node in visited_nodes_set:
  42.                 return False
  43.             if current_node.data in global_data_values_set:
  44.                 return False
  45.             in_order_list.append(current_node.data)
  46.             global_data_values_set.add(current_node.data)
  47.             visited_nodes_set.add(current_node)
  48.             current_node = current_node.right
  49.         else:
  50.             done = True
  51.     # Next step is to compare the in_order list to a sorted version of same list
  52.     comparison_list = sorted(in_order_list)
  53.     if comparison_list == in_order_list:
  54.         return True
  55.     else:
  56.         return False
  57.  
  58.  
  59. def checkBST(root):
  60.     if root is not None:
  61.         return check_node(root)
  62.     else:
  63.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement