Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. from BinarySearchTree import BinarySearchTree, Node
  2. import random, string
  3.  
  4. # MAKE CHANGES AND DO YOUR OWN TESTING
  5.  
  6. def randomword():
  7. letters = string.ascii_lowercase
  8. return ''.join(random.choice(letters) for i in range(random.randint(0,12)))
  9.  
  10. def main():
  11. bst = BinarySearchTree()
  12. bst.insert(5, "Dasher")
  13. bst.insert(9, "Dancer")
  14. bst.insert(4, "Prancer")
  15. bst.insert(12, "Vixen")
  16. bst.insert(7, "Comet")
  17. bst.insert(2, "Cupid")
  18.  
  19. print("-----TEST 1-----\n ", bst.inorder(bst.root, []), "\n Root ", bst.root, "\n")
  20.  
  21. bst.insert(8, "Donner")
  22. bst.insert(3, "Blitzen")
  23. bst.remove(12) # Remove a leaf
  24.  
  25. print("-----TEST 2-----\n ", bst.inorder(bst.root, []), "\n Root ", bst.root, "\n")
  26.  
  27. bst.insert(6, "Rudolph")
  28. bst.remove(5) # Remove the root
  29.  
  30. print("-----TEST 3-----\n ", bst.inorder(bst.root, []), "\n Root\t", bst.root, "\n")
  31.  
  32. print("-----TEST 4-----\n ", bst.inorder(bst.root, []), "\n Root\t", bst.root)
  33. # Test the other functions
  34. print(" Find\t", bst.find_name(7))
  35. print(" Min\t", bst.min(bst.root))
  36. print(" Max\t", bst.max(bst.root))
  37. print(" Size\t", bst.get_size())
  38.  
  39. print("")
  40. bst.insert(2, "Cupid") # Update a value
  41. for i in range(0, 500):
  42. bst.remove(i)
  43.  
  44. count = 1
  45. for i in range(0, 2000):
  46. randkey = random.randint(0, 1000)
  47. randword = randomword()
  48. if randkey and randword:
  49. bst.insert(randkey, randword)
  50.  
  51. if i%200 == 0:
  52. print("-----custom test -- insert {}-----\n ".format(count), bst.inorder(bst.root, []), "\n Root\t", bst.root)
  53. count += 1
  54.  
  55. print("\n")
  56. count = 1
  57. for i in range(0, 100000):
  58. randkey = random.randint(0, 200)
  59. randword = randomword()
  60.  
  61.  
  62. if bst.root:
  63. if i % random.randint(1, 500) == 0:
  64. bst.remove(bst.root.key)
  65.  
  66. bst.insert(randkey, randword)
  67. bst.remove(i)
  68.  
  69. # if i % 200 == 0:
  70. # print("-----custom test -- remove {}-----\n ".format(count), bst.inorder(bst.root, []), "\n Root\t", bst.root)
  71. # count += 1
  72.  
  73. print("\n")
  74.  
  75.  
  76.  
  77. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement