Ejemplo n.º 1
0
 def test_greater_number_at_right_node(self):
     expected = TreeNode('4', None, TreeNode('5', None, None))
     self.assertTreeEqual(BinarySearchTree(['4', '5']).data(), expected)
from binary_search_tree import BinarySearchTree
start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

#  RUBRIC ITEM: The runtime of the original nested loop was O(n^c).
#  It is therefore worse than O(nlogn) and better than O(c^n).
tree = BinarySearchTree(names_1[0])
for name in names_1[1:]:
    tree.insert(name)
for name in names_2:
    if tree.contains(name):
        duplicates.append(name)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
 def test_handle_dupe_insert_gls(self):
     self.bst2 = BinarySearchTree(1)
     self.bst2.insert_guided_lecture_solution(1)
     self.assertEqual(self.bst2.right.value, 1)
Ejemplo n.º 4
0
f = open(
    r'C:\Users\navo1\Desktop\Sprint-Challenge--Data-Structures-Python\names\names_1.txt',
    'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open(
    r'C:\Users\navo1\Desktop\Sprint-Challenge--Data-Structures-Python\names\names_2.txt',
    'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

bst = BinarySearchTree('names')
# Replace the nested for loops below with your improvements
for name in names_1:
    bst.insert(name)

for name in names_2:
    if bst.contains(name):
        duplicates.append(name)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print("The runtime complexity of the previous code is O(n^2)")
print(f"runtime: {end_time - start_time} seconds")
print("The runtime complexity is O(log n)")
# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

evergreen = BinarySearchTree(None)

for name in names_1:
    if evergreen.value == None:
        evergreen.value = name
    else:
        evergreen.insert(name)

for name in names_2:
    if evergreen.contains(name):
        duplicates.append(name)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"This is the MVP solution runtime: {end_time - start_time} seconds")
 def test_search_value_in_BST_for_empty_BST(self):
     a = BinarySearchTree()
     self.assertEqual(a.search_value_in_BST(10), False)
 def test_find_parent_for_rootNode(self):
     a = BinarySearchTree()
     a.insert_value(10)
     a.insert_value(7)
     a.insert_value(3)
     self.assertEqual(a.find_parent(a.search_node(10)), None)
Ejemplo n.º 8
0
import random
from binary_search_tree import BinarySearchTree

test_bst = BinarySearchTree(1)

for i in range(50):
    value = random.randint(1, 100)
    test_bst.insert(value)

test_bst.in_order_print_recursively(test_bst)
Ejemplo n.º 9
0
print(f"runtime: {end_time - start_time} seconds")

# optimized list solution compared to original: O(n^2) but the outer loop continues if a name is found,
# instead of always looping n times in inner loop as it did in the starter solution

start_time = time.time()
limited_duplicates = [name for name in names_1 if name in names_2]

end_time = time.time()
print(f"{len(limited_duplicates)} duplicates:\n\n{', '.join(limited_duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# runtime using binary search tree: O(n log(n))

start_time = time.time()
names_1_bst = BinarySearchTree(names_1[0])
for name in names_1:
    names_1_bst.insert(name)

duplicates = [name for name in names_2 if names_1_bst.contains(name)]

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.

# runtime using hash table: O(n)
Ejemplo n.º 10
0
 def test_can_sort_complex_tree(self):
     self.assertEqual(
         BinarySearchTree(['2', '1', '3', '6', '7', '5']).sorted_data(),
         ['1', '2', '3', '5', '6', '7'])
Ejemplo n.º 11
0
 def test_data_is_retained(self):
     expected = TreeNode('4', None, None)
     self.assertTreeEqual(BinarySearchTree(['4']).data(), expected)
Ejemplo n.º 12
0
 def test_can_sort_if_second_number_is_greater_than_first(self):
     self.assertEqual(
         BinarySearchTree(['2', '3']).sorted_data(), ['2', '3'])
Ejemplo n.º 13
0
 def test_can_sort_if_second_number_is_same_as_first(self):
     self.assertEqual(
         BinarySearchTree(['2', '2']).sorted_data(), ['2', '2'])
Ejemplo n.º 14
0
 def test_can_sort_single_number(self):
     self.assertEqual(BinarySearchTree(['2']).sorted_data(), ['2'])
# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

# the first for loop checks each name in names_1, so the speed of that depends on n,
# the number of names in the list. the next for loop also checks for every name in names_2
# so depends on the number of names in the list, also n. So, we have n * n complexity, of n^2

# this solution ^^ is 0(n^2) for each entry in name_1 your also looping through each entry in name_2. # runtime: 6.446194887161255 seconds

# optimized solution  below os o(n)(logn) - looping once through each name/node(names_1 - 0(n)) in the bst (names_2 - logn)  runtime: 0.11794686317443848 seconds

# inserting names_1 list into bst (logn)
bst = BinarySearchTree(names_1[0])  # adding first name in tree
for i in range(
        1,
        len(names_1)):  # looping though remaining names and inserting into bst
    bst.insert(names_1[i])

# loop through each name in names_2 list and see if there's duplicates in the names_1 bst tree
for name in names_2:  # o(n)
    # check if bst contains duplicates by using contains method on bst class
    # if target name from names_2 is in tree then return true and append
    if bst.contains(name):
        duplicates.append(name)

# bst is optimized for searches, faster b/c each comparison allows the operations to skip about half of the tree, reduces potential answer by 2, or eliminate one side. avg case is logn, worst case to find an element is 0(n) b/c it could be a LL, and need to search thru the whole tree one at a time.

# using a hash set (hash table is the underlying data structure) is even fast than bst
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

BST = BinarySearchTree("Headquarters of the World Meme Database")
for name in names_1:
    BST.insert(name)

for name in names_2:
    if BST.contains(name):
        duplicates.append(name)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
Ejemplo n.º 17
0
class Solution(object):
    def searchBST(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        if root is None:
            return None
        elif root.val == val:
            return root
        elif root.val > val:
            return self.searchBST(root.left, val)
        elif root.val < val:
            return self.searchBST(root.right, val)


if __name__ == "__main__":
    bstArr = [4, 2, 7, 1, 3]
    bst = BinarySearchTree(bstArr=bstArr)
    root = TreeNode(val=bst.bstArr[0], left=None, right=None)
    bst.constructBST(root, 0)
    print("BST Constructed")
    bst.printBST(root)

    node = Solution().searchBST(root=root, val=7)
    if node is not None:
        print("Found TreeNode: {}".format(node.val))
    else:
        print("Node not found")
Ejemplo n.º 18
0
 def test_generator(self):
     bst = BinarySearchTree()
     for value in (random.randint(0, 99) for value in range(1000)):
         bst.insert(value)
     BinarySearchNode.__iter__ = generator
     self.assertEqual(bst.list(), list(bst))
 def test_insert_value_when_no_item_is_inserted(self):
     a = BinarySearchTree()
     self.assertEqual(a.print_BST(), "()")
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

name_tree = BinarySearchTree(names_2[0])
for name in names_2[1:]:
    name_tree.insert(name)

for name in names_1:
    if name_tree.contains(name):
        duplicates.append(name)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
 def test_insert_value_when_one_item_is_inserted(self):
     a = BinarySearchTree()
     a.insert_value(11)
     self.assertEqual(a.print_BST(), "(11)")
Ejemplo n.º 22
0
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
binary_search_tree = BinarySearchTree('duplicate_names')

for name in names_1:
    binary_search_tree.insert(name)

for name in names_2:
    if binary_search_tree.contains(name):
        duplicates.append(name)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
Ejemplo n.º 23
0
from binary_search_tree import BinarySearchTree
from random import randint
# Developed by Ben Muzzy

bst = BinarySearchTree()
# will store the random numbers generated
seq = []
#start at zero instead of 1
key = 0

print("Keys and random values for Binary Search Tree:")
# This loop will generate 5 random numbers between 1 and 1000
for index in range(5):
    seq.append(randint(1, 1000))
    bst.put(key, seq[index])
    # each number generated will get correpsonding key to go with it.
    print(key, seq[index])
    key += 1
    
# prints a random list of numbers from the above for statement
print()

#prints the binary tree
print("Print Tree:")
bst.printtree()

print("\n")


#shows traversing the tree (navigating the tree)
print("Traverse Tree:")
Ejemplo n.º 24
0
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

bst_1 = BinarySearchTree('')
bst_2 = BinarySearchTree('')
for x in names_1:
    bst_1.insert(x)

for x in names_2:
    bst_2.insert(x)

for x in names_1 + names_2:
    if bst_1.contains(x) and bst_2.contains(x) and x not in duplicates:
        duplicates.append(x)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")
import time
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []

bst = BinarySearchTree("value")

for name_1 in names_1:
    bst.insert(name_1)

for name_2 in names_2:
    if bst.contains(name_2):
        duplicates.append(name_2)
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")
Ejemplo n.º 26
0
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()
'''
duplicates = []
for name_1 in names_1:
    for name_2 in names_2:
        if name_1 == name_2:
            duplicates.append(name_1)
'''
duplicates = []

namesTree1 = BinarySearchTree(names_1[0])

for x in names_1:
    namesTree1.insert(x)

for name in names_2:
    if namesTree1.contains(name):
        duplicates.append(name)

end_time = time.time()

print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")
'''
cb = lambda x: print(x)
namesTree1.for_each(cb)
 def test_handle_dupe_insert(self):
     self.bst2 = BinarySearchTree(1)
     self.bst2.insert(1)
     self.assertEqual(self.bst2.right.value, 1)
import time
from binary_search_tree import BinarySearchTree


start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
names_tree2 = BinarySearchTree('')
for i in names_2:
    names_tree2.insert(i)
f.close()

duplicates = []
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)
for name_1 in names_1:
    if names_tree2.contains(name_1):
        duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
 def setUp(self):
     self.bst = BinarySearchTree(5)
Ejemplo n.º 30
0
 def test_same_number_at_left_node(self):
     expected = TreeNode('4', TreeNode('4', None, None), None)
     self.assertTreeEqual(BinarySearchTree(['4', '4']).data(), expected)