def tree_sort(li_sort): root = tr.Node(li_sort[0]) for i in range(1, len(li_sort)): tr.insert(root, tr.Node(li_sort[i])) li_sort = [] inOrderTraversal(root, li_sort) return li_sort
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 generateBST(list,tree = None): if tree == None: tree = BST() if len(list) == 0: return tree else: mid = len(list)/2 mNum = list[mid] tree.insert(mNum) tree = generateBST(list[:mid],tree) tree = generateBST(list[mid+1:],tree) return tree
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)
class TestTree(unittest.TestCase): '''tests for BST''' def setUp(self): self.BST = BST(NodeType=BSTNode) def test_single_insert(self): self.BST.insert(50) self.assertEqual(self.BST.root.key, 50) self.assertEqual(self.BST.root.parent, None) def test_structure(self): ''' .50. / \ 25 75 /\ / \ 10 30 60 90 ''' def test_structure_for_type(tree_type): tree_type.insert(50) tree_type.insert(25) tree_type.insert(75) tree_type.insert(30) tree_type.insert(10) tree_type.insert(90) tree_type.insert(60) root = tree_type.root self.assertEqual(root.key, 50) self.assertEqual(root.parent, None) self.assertEqual(root.left.key, 25) self.assertEqual(root.left.parent.key, 50) self.assertEqual(root.left.left.key, 10) self.assertEqual(root.left.left.parent.key, 25) self.assertEqual(root.left.right.key, 30) self.assertEqual(root.left.right.parent.key, 25) self.assertEqual(root.right.key, 75) self.assertEqual(root.right.parent.key, 50) self.assertEqual(root.right.left.key, 60) self.assertEqual(root.right.left.parent.key, 75) self.assertEqual(root.right.right.key, 90) self.assertEqual(root.right.right.parent.key, 75) test_structure_for_type(self.BST)
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)
# -*- coding: utf-8 -*- """ App: """ from BinarySearchTree import BinarySearchTree as BST bst = BST() bst.insert(12) bst.insert(10) bst.insert(-2) bst.insert(1) bst.traverseInOrder() bst.remove(10) bst.traverseInOrder() print bst.getMin() print bst.getMax()
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/python from BinarySearchTree import BST,Node from LinkedList import LinkedList,Node btree = BST() btree.insert(8) btree.insert(4) btree.insert(3) btree.insert(10) btree.insert(9) btree.insert(13) btree.insert(11) btree.insert(15) btree.insert(16) #btree.postorder() class solution(): list = [] def traverse(self,tree): if tree.root: self.findnodes(tree.root, 0) return self.list else: return None def findnodes(self,node,level): if len(self.list) < level+1: ll = LinkedList()
def test_node_count_empty(self): b = BST() assert b.get_node_count() == 0
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])
''' Given a BST and a valid range of keys, remove nodes from the BST that have keys outside that range. ''' def truncate(root, low, high): if root is None: return root root.left = truncate(root.left, low, high) root.right = truncate(root.right, low, high) if root.val > high: root = root.left elif root.val < low: root = root.right return root bst = BST() keys = [15, 10, 20, 8, 12, 16, 25] for key in keys: bst.insert(key) bst.preorder(bst.root) print() truncate(bst.root, 9, 12) bst.preorder(bst.root)
def testOneInsert(): _tree = BST() _tree.insert(30) assert _tree.exists(30)
def testIsEmpty(): _tree = BST() assert _tree.is_empty()
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
from BinarySearchTree import BST bst = BST() bst.insert(12) bst.insert(10) bst.insert(-2) bst.insert(1) bst.traverseInOrder() bst.remove(10) bst.traverseInOrder() print(bst.getMax())
from BinarySearchTree import BST,Node def checkBalance(root): if root: return balanceH(root)>=0 def balanceH(node): if node == None: return 0 else: left = balanceH(node.left) right = balanceH(node.right) if left<0 or right<0 or abs(left-right)>1: return -1 return max(left,right)+1 btree = BST() btree.insert(8) btree.insert(4) btree.insert(3) btree.insert(10) btree.insert(9) btree.insert(13) btree.insert(11) btree.insert(15) btree.insert(16) btree.postorder() root = btree.get_root() print checkBalance(root)
def testRemoveRootOne(): _tree = BST() _tree.insert(7) _tree.remove(7) assert _tree.is_empty()
def setUp(self): self.BST = BST(NodeType=BSTNode)
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
#!/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)
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())
from BinarySearchTree import BST from os import * bst = BST() # bst.insert(10) # bst.insert(1) # bst.insert(-2) # bst.insert(67) # bst.insert(5) # print( bst.getMax() ) # print ("\n") # print(bst.getMin()) # print ("\n") # bst.traverseInOrder() # bst.remove(10) # print ("\n") # bst.traverseInOrder() # print ("\n") while 1: b = input( " 1. Add number to binarytree \n 2. Remove number \n 3. Print binarytree \n 4. Print Max \n 5. Print Min \n 6. Exit \n Enter your choice: " ) if b == 1: c = input("\n Enter the number: ") bst.insert(c) elif b == 2:
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, Node if __name__ == '__main__': myItems = [5, 1, 8, 2, 0.5, 6, 7] bst = BST() for el in myItems: bst.insert(el) bst.preOrder(bst.root) print(' ') bst.postOrder(bst.root) print(' ') bst.inOrder(bst.root) head = bst.bstToDll(bst.root)