-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBinarySearchTreeCheckImpleSol1.py
More file actions
49 lines (39 loc) · 1.24 KB
/
Copy pathBinarySearchTreeCheckImpleSol1.py
File metadata and controls
49 lines (39 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Binary Search Tree Check - Validate BST
# Author: Pradeep K. Pant, ppant@cpan.org
# Given a binary tree, check whether it’s a binary search tree or not.
# Strategy: If a tree is a BST, then an in-order traversal should return
# values in sorted order.
class Node:
def __init__(self, k, val):
self.key = k
self.payload = val
self.leftChild = None
self.rightChild = None
def getLeftChild(self):
return self.leftChild
def getRightChild(self):
return self.rightChild
def getRootVal(self):
return self.key
def inorder(tree, tree_vals):
if tree != None:
inorder(tree.getLeftChild(), tree_vals)
tree_vals.append(tree.getRootVal())
inorder(tree.getRightChild(), tree_vals)
def sort_check(tree_vals):
return tree_vals == sorted(tree_vals)
# Test
root = Node(3, "red")
root.leftChild = Node(2, "at")
root.rightChild = Node(4, "blue")
root.rightChild.rightChild = Node(6, "yellow")
vals = []
inorder(root, vals)
print(f"In-order traversal: {vals}")
print(f"Is BST? {sort_check(vals)}")
# Negative test
root.leftChild.key = 5 # Break BST property
vals = []
inorder(root, vals)
print(f"In-order traversal (broken): {vals}")
print(f"Is BST? {sort_check(vals)}")