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 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 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
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)
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())
''' 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 __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 testOneInsert(): _tree = BST() _tree.insert(30) assert _tree.exists(30)
def testRemoveRootOne(): _tree = BST() _tree.insert(7) _tree.remove(7) assert _tree.is_empty()
#!/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()
# 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: c = input("Enter the number you want to remove: ") bst.remove(c) elif b == 3: bst.traverseInOrder() elif b == 4: print(bst.getMax()) elif b == 5: print(bst.getMin()) elif b == 6: exit() else: print("Enter a valid choice!!")
# For passing DS from BinarySearchTree import BST; bst = BST() print("--- Prints the entire insert in ascending order ---") bst.insert(12) bst.insert(10) bst.insert(-2) bst.insert(1) # Let's order the nodes/values bst.traverseInOrder() print("--- Testing Remove Method---") # Let's try to remove bst.remove(10) bst.traverseInOrder() # print("--- Testing getMax Method---") # print(bst.getMax()) print("--- Testing getMin Method---") print(bst.getMin())
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, 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)