def setUp(self): self.BST = BST(NodeType=BSTNode) self.BSTmin = BST(NodeType=MinBSTNode) self.BSTmax = BST(NodeType=MaxBSTNode) self.BSTminmax = BST(NodeType=MinMaxBSTNode) ''' .50. / \ 25 75 /\ / \ 10 30 60 90 ''' # setup BST def insertions(tree): tree.insert(50) tree.insert(25) tree.insert(75) tree.insert(30) tree.insert(10) tree.insert(90) tree.insert(60) return tree self.BST = insertions(self.BST) self.BSTmin = insertions(self.BSTmin) self.BSTmax = insertions(self.BSTmax) self.BSTminmax = insertions(self.BSTminmax)
def testManyInsert(): _tree = BST() _tree.insert(25) _tree.insert(10) _tree.insert(30) _tree.insert(35) assert _tree.exists(10) assert _tree.exists(25) assert _tree.exists(30) assert _tree.exists(35)
def create_tree(): b = BST() b.insert(5) b.insert(3) b.insert(4) b.insert(2) b.insert(3.5) b.insert(10) b.insert(8) b.insert(7) b.insert(11) return b
def binarySearchTree(): dic = defaultdict(list) bst = BST() # for i in range(len(df)): for i in range(3): url = df['Link'][i] resp = requests.get(url) soup = BeautifulSoup(resp.text, 'html.parser') data = soup.find("body").text.lower() tokens = set(word_tokenize(data)) tokens = [w for w in tokens if w not in stop_words] tokens = sorted(tokens) for word in tokens: dic[word].append(i + 1) print(dic)
def __create_tree(): _tree = BST() _tree.insert(7) _tree.insert(5) _tree.insert(3) _tree.insert(1) _tree.insert(4) _tree.insert(6) _tree.insert(12) _tree.insert(9) _tree.insert(8) _tree.insert(10) _tree.insert(15) _tree.insert(13) _tree.insert(17) return _tree
def testBinarySearch(self): bst = BST() bst.addNode(10, "Value 1") self.assertEqual(bst.size(), 1) bst.addNode(5, "Value 2") self.assertEqual(bst.size(), 2) bst.addNode(30, "Value 2") self.assertEqual(bst.size(), 3) self.assertListEqual(bst.inOrderTraverse(), [15, 20, 30]) self.assertListEqual(bst.preOrderTraverse(), [20, 15, 30]) self.assertListEqual(bst.postOrderTraverse(), [15, 30, 20]) self.assertListEqual(bst.searchSmallestKey(), 5) self.assertListEqual(bst.searchLargestKey(), [30]) self.assertEqual(bst.searchForNode(10), [1])
from Node import Node; from BinarySearchTree import BST; bst = BST(); bst.insert(12); bst.insert(10); bst.insert(-2); bst.insert(1); bst.traverseInOrder(); print(bst.getMin()); bst.remove(10); bst.traverseInOrder();
from BinarySearchTree import BST from Node import Node tree = BST() tree.setRoot(5) tree.insert(3) tree.insert(8) tree.insert(4) tree.insert(6) tree.insert(2) tree.insert(7) print(tree.getPreOrder()) print(tree.getInOrder()) print(tree.getPostOrder())
def testOneInsert(): _tree = BST() _tree.insert(30) assert _tree.exists(30)
def testRemoveRootOne(): _tree = BST() _tree.insert(7) _tree.remove(7) assert _tree.is_empty()
def testIsEmpty(): _tree = BST() assert _tree.is_empty()
def test_bst(): # initialize BST bst = BST(23, 'Apple') # test insert bst.insert(14, 'Pear') bst.insert(105, 'Orange') assert bst.preorder_traversal() == 'Apple, Pear, Orange, ' assert bst.postorder_traversal() == 'Pear, Orange, Apple, ' assert bst.inorder_traversal() == 'Pear, Apple, Orange, ' bst.insert(65, 'Mango') bst.insert(2, 'Grape') bst.insert(34, 'Watermelon') bst.insert(20, 'Strawberry') assert bst.preorder_traversal( ) == 'Apple, Pear, Grape, Strawberry, Orange, Mango, Watermelon, ' assert bst.postorder_traversal( ) == 'Grape, Strawberry, Pear, Watermelon, Mango, Orange, Apple, ' assert bst.inorder_traversal( ) == 'Grape, Pear, Strawberry, Apple, Watermelon, Mango, Orange, ' # test has assert bst.has(23) assert bst.has(14) assert bst.has(105) assert bst.has(2) assert bst.has(65) assert bst.has(34) assert not bst.has(3) assert not bst.has(74) assert not bst.has(128) # test lookup assert bst.lookup(23) == 'Apple' assert bst.lookup(14) == 'Pear' assert bst.lookup(105) == 'Orange' assert bst.lookup(2) == 'Grape' assert bst.lookup(65) == 'Mango' assert bst.lookup(34) == 'Watermelon' assert bst.lookup(106) == '' # test height assert bst.height() == 3 # test delete bst.delete(23) assert bst.preorder_traversal( ) == 'Watermelon, Pear, Grape, Strawberry, Orange, Mango, ' assert bst.postorder_traversal( ) == 'Grape, Strawberry, Pear, Mango, Orange, Watermelon, ' assert bst.inorder_traversal( ) == 'Grape, Pear, Strawberry, Watermelon, Mango, Orange, ' assert bst.height() == 2 bst.delete(14) assert bst.preorder_traversal( ) == 'Watermelon, Strawberry, Grape, Orange, Mango, ' assert bst.postorder_traversal( ) == 'Grape, Strawberry, Mango, Orange, Watermelon, ' assert bst.inorder_traversal( ) == 'Grape, Strawberry, Watermelon, Mango, Orange, ' assert bst.height() == 2 bst.delete(65) assert bst.preorder_traversal( ) == 'Watermelon, Strawberry, Grape, Orange, ' assert bst.postorder_traversal( ) == 'Grape, Strawberry, Orange, Watermelon, ' assert bst.inorder_traversal( ) == 'Grape, Strawberry, Watermelon, Orange, ' bst.delete(20) assert bst.preorder_traversal( ) == 'Watermelon, Grape, Orange, ' assert bst.postorder_traversal( ) == 'Grape, Orange, Watermelon, ' assert bst.inorder_traversal( ) == 'Grape, Watermelon, Orange, ' bst.insert(120, 'Blueberry') bst.delete(34) assert bst.preorder_traversal( ) == 'Orange, Grape, Blueberry, ' assert bst.postorder_traversal( ) == 'Grape, Blueberry, Orange, ' assert bst.inorder_traversal( ) == 'Grape, Orange, Blueberry, ' assert bst.height() == 1 bst.delete(120) bst.delete(2) bst.delete(105) assert bst.preorder_traversal( ) == '' assert bst.postorder_traversal( ) == '' assert bst.inorder_traversal( ) == '' assert bst.height() == 0 # test is_empty assert bst.is_empty(bst.root) assert bst.is_empty(None) bst = BST(43, 'Blackberry') assert not bst.is_empty(bst.root) # test nuke bst.nuke() assert bst.is_empty(bst.root)
#!/usr/bin/env python3 #Authors: M269 Module Team #Date: 21/4/13 from BinarySearchTree import BST, TreeNode def inorder(aNode): if aNode != None: inorder(aNode.hasLeftChild()) print(aNode.key, aNode.payload) inorder(aNode.hasRightChild()) aBST = BST() aBST.insertNode(34, 'Fred') aBST.insertNode(12, 'Bill') aBST.insertNode(41, 'Jim') aBST.insertNode(9, 'Sue') aBST.insertNode(32, 'Ali') aBST.insertNode(36, 'Megan') aBST.insertNode(92, 'Roisin') aBST.insertNode(4, 'Thibault') aBST.insertNode(10, 'Osian') aBST.insertNode(55, 'Niamh') aBST.insertNode(97, 'Tudor') inorder(aBST.root)
def setUp(self): self.BST = BST(NodeType=BSTNode)
def test_node_count_empty(self): b = BST() assert b.get_node_count() == 0