This is a Python program to check whether a binary tree is a binary search tree.
The program creates a binary tree and presents a menu to the user to perform operations on the tree including checking whether the tree is a binary search tree.
1. Create a class BinaryTree with instance variables key, left and right.
2. Define methods set_root, insert_left, insert_right, search and is_bst_p.
3. The method set_root takes a key as argument and sets the variable key equal to it.
4. The methods insert_left and insert_right insert a node as the left and right child respectively.
5. The method search returns a node with a specified key.
6. The method is_bst_p returns True iff the binary tree with the current object as root is a binary search tree.
Here is the source code of a Python program to check whether a binary tree is a binary search tree. The program output is shown below.
class BinaryTree: def __init__(self, key=None): self.key = key self.left = None self.right = None def set_root(self, key): self.key = key def insert_left(self, new_node): self.left = new_node def insert_right(self, new_node): self.right = new_node def search(self, key): if self.key == key: return self if self.left is not None: temp = self.left.search(key) if temp is not None: return temp if self.right is not None: temp = self.right.search(key) return temp return None def is_bst_p(self): if self.left is not None: if self.key < self.left.key: return False elif not self.left.is_bst_p(): return False if self.right is not None: if self.key > self.right.key: return False elif not self.right.is_bst_p(): return False return True btree = None print('Menu (this assumes no duplicate keys)') print('insert <data> at root') print('insert <data> left of <data>') print('insert <data> right of <data>') print('bst') print('quit') while True: do = input('What would you like to do? ').split() operation = do[0].strip().lower() if operation == 'insert': data = int(do[1]) new_node = BinaryTree(data) suboperation = do[2].strip().lower() if suboperation == 'at': btree = new_node else: position = do[4].strip().lower() key = int(position) ref_node = None if btree is not None: ref_node = btree.search(key) if ref_node is None: print('No such key.') continue if suboperation == 'left': ref_node.insert_left(new_node) elif suboperation == 'right': ref_node.insert_right(new_node) elif operation == 'bst': if btree is not None: if btree.is_bst_p(): print('Tree is a binary search tree.') else: print('Tree is not a binary search tree.') else: print('Tree is empty.') elif operation == 'quit': break
1. A variable is created to store the binary tree.
2. The user is presented with a menu to perform operations on the binary tree.
3. The corresponding methods are called to perform each operation.
4. The method is_bst_p is called on the tree to determine whether it is a binary search tree.
Case 1: Menu (this assumes no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> bst quit What would you like to do? insert 1 at root What would you like to do? bst Tree is a binary search tree. What would you like to do? insert 0 left of 1 What would you like to do? bst Tree is a binary search tree. What would you like to do? insert 2 right of 1 What would you like to do? bst Tree is a binary search tree. What would you like to do? insert 3 left of 2 What would you like to do? bst Tree is not a binary search tree. What would you like to do? quit Case 2: Menu (this assumes no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> bst quit What would you like to do? insert 5 at root What would you like to do? insert 1 left of 5 What would you like to do? insert 10 right of 5 What would you like to do? insert 0 left of 1 What would you like to do? insert 3 right of 1 What would you like to do? insert 15 right of 10 What would you like to do? bst Tree is a binary search tree. What would you like to do? quit
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Practice Programming MCQs
- Check Python Books
- Check Information Technology Books
- Apply for Python Internship
- Apply for Programming Internship