Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validate_bst function for testing purposes #845

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 17 additions & 38 deletions algorithms/tree/bst/bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
4. Traversal (Preorder, Inorder, Postorder)
"""

import unittest
import math

class Node(object):
def __init__(self, data):
Expand Down Expand Up @@ -99,41 +99,20 @@ def postorder(self, root):
self.postorder(root.right)
print(str(root.data), end = ' ')

"""
The tree is created for testing:

10
/ \
6 15
/ \ / \
4 9 12 24
/ / \
7 20 30
/
18
"""

class TestSuite(unittest.TestCase):
def setUp(self):
self.tree = BST()
self.tree.insert(10)
self.tree.insert(15)
self.tree.insert(6)
self.tree.insert(4)
self.tree.insert(9)
self.tree.insert(12)
self.tree.insert(24)
self.tree.insert(7)
self.tree.insert(20)
self.tree.insert(30)
self.tree.insert(18)
def validate_bst(self):
""" Visits node through inorder dfs, and validates that the tree is a BST
:rtype: bool
"""
def in_order(root, prev):

def test_search(self):
self.assertTrue(self.tree.search(24))
self.assertFalse(self.tree.search(50))

def test_size(self):
self.assertEqual(11, self.tree.size())

if __name__ == '__main__':
unittest.main()
if root == None:
return True
if not in_order(root.left, prev):
return False
if root.data <= prev:
return False
prev = root.data
return in_order(root.right, prev)

prev = -math.inf
return in_order(self.root, prev)
59 changes: 56 additions & 3 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from algorithms.tree.bst.bst import BST
from algorithms.tree.traversal import (
preorder,
preorder_rec,
Expand All @@ -7,11 +8,8 @@
inorder_rec
)
from algorithms.tree.b_tree import BTree

from algorithms.tree import construct_tree_postorder_preorder as ctpp

from algorithms.tree.fenwick_tree.fenwick_tree import Fenwick_Tree

import unittest


Expand Down Expand Up @@ -175,6 +173,61 @@ def test_construct_tree_with_update_3(self):
ft.update_bit(bit_tree, 2, 11)
self.assertEqual(23, ft.get_sum(bit_tree, 4))

class TestBST(unittest.TestCase):
"""
Testing binary search tree functionality
"""

def test_insert(self):
"""
Testing that bst structure is valid after insert
"""
bst = create_bst()
self.assertTrue(bst.validate_bst())

def test_search(self):
"""
Testing search functionality
"""
bst = create_bst()
self.assertTrue(bst.search(24))
self.assertFalse(bst.search(50))

def test_size(self):
"""
Testing size function
"""
bst = create_bst()
self.assertEqual(11, bst.size())


def create_bst():
"""
The following tree is created for testing:

10
/ \
6 15
/ \ / \
4 9 12 24
/ / \
7 20 30
/
18
"""
tree = BST()
tree.insert(10)
tree.insert(15)
tree.insert(6)
tree.insert(4)
tree.insert(9)
tree.insert(12)
tree.insert(24)
tree.insert(7)
tree.insert(20)
tree.insert(30)
tree.insert(18)
return tree

if __name__ == '__main__':
unittest.main()