Beispiel #1
0
def main():
    start = time.time()
    splayTree = SplayTree()
    print("First insert 20 random nodes into an empty tree and print the tree.")
    for i in range(20):
        n = random.randint(0, 1000)
        splayTree.insert(BSTNode(n, splayTree.nil, splayTree.nil, splayTree.nil))
    print()
    splayTree.print()
    print()

    print("\nThen insert 80 more random nodes into the tree.")
    for i in range(80):
        n = random.randint(0, 1000)
        splayTree.insert(BSTNode(n, splayTree.nil, splayTree.nil, splayTree.nil))
    print("These keys in inorder are: ")
    splayTree.inorder()
    print()

    print("\nNow do 200 searches. ")
    for i in range(200):
        n = random.randint(0, 1000)
        if splayTree.search(n) == splayTree.nil:
            print(n, " not found")
        else:
            print(n, " found")

    print("\nNow search for & delete some nodes.")
    for i in range(2000):
        n = random.randint(0, 1000)
        z = splayTree.search(n)
        if z != splayTree.nil:
            splayTree.delete(z)
            print(n)
    print("The keys in the trees in inorder are now: ")
    splayTree.inorder()
    print()

    print("\nThe final tree looks like this.")
    print()
    splayTree.print()
    print()
    runTime = time.time() - start
    print(runTime)
Beispiel #2
0
def build_min_height_bst_from_sorted_array(arr):
    """
    Time: O(N)
    """

    if not arr:
        return None
    mid = len(arr) // 2
    root = arr[mid]
    return BSTNode(root, build_min_height_bst_from_sorted_array(arr[:mid]),
                   build_min_height_bst_from_sorted_array(arr[mid + 1:]))
def rebuild_bst_from_preorder(preorder_sequence):
    """
    Example: [43, 23, 37 29, 31, 41, 47, 53], then the 43 is the ROOT, and [23, 37 29, 31, 41] are the left child
             since they are all less than 43, and also [47, 53] are the right child as they are all greater than or
             equal to the root, 43.
    Time Complexity: Page 209
    Improved Strategy: Page 210
    """
    if not preorder_sequence:
        return None
    root = preorder_sequence[0]
    left_sequence = [e for e in preorder_sequence[1:] if e < root]
    right_sequence = [e for e in preorder_sequence[len(left_sequence) + 1:] if e >= root]
    return BSTNode(root, rebuild_bst_from_preorder(left_sequence),
                         rebuild_bst_from_preorder(right_sequence))
Beispiel #4
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
#Runtime complexity is O(n2)
# 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)

tree = BSTNode(names_1[0])
names_1_checker = [name for name in names_1[1:]]
#Create a new array, without first value to distinguish

for name in names_1_checker:
    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")
Beispiel #5
0
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
# 0(n^2)
# for name_1 in names_1:0(n)
#     for name_2 in names_2: 0(n)
#         if name_1 == name_2: 0(1)
#             duplicates.append(name_1) 0(1)

bst = None  #0(1)
for name_1 in names_1:  #0(n) # followed the bst tester
    if bst is None:  #0(1)
        bst = BSTNode(name_1)  #0(1)
    else:
        bst.insert(name_1)  #0(log n)
#first loop 0(n log n)

for name_2 in names_2:  #0(n)
    if bst.contains(name_2):  #0(log n)
        duplicates.append(name_2)  #0(1)
#0(n log n)

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
from BST import BSTNode

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
bst = BSTNode("Root Node")

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)
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.
Beispiel #7
0
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)

#Using BST to cut runtime

head = names_1[0]
names_1 = names_1[1:]

bst = BSTNode(head)

for name in names_1:
    bst.insert(name)

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

# RUNTIME : 0.16530179977416992 seconds , 64 Duplicates

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 -----------
Beispiel #8
0
import time


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
list_one = BSTNode(names_1[0])

for name in names_1:
    list_one.insert(name)

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


# for name_1 in names_1: 
#     if name_1 in names_2:
#         duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
from BST import BSTNode

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()

## NOTE: Before BST implementation my first run time was 5.30 seconds
## COOLER NOTE: After BST implementation my run time is 0.08 seconds

bst = BSTNode("None")
duplicates = []

for name in names_1:
    bst.insertValue(name)
for name in names_2:
    if bst.containsTarget(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
Beispiel #10
0
def main():
    #start Binary Search Tree
    bst = BST()
    timeforInsertBST = time.time()
    print(
        "First insecd crt 50000 random nodes into an empty tree and print the tree."
    )
    for i in range(50000):
        n = random.randint(0, 1000000)
        bst.insert(BSTNode(n, bst.nil, bst.nil, bst.nil))
    print("time for BST to insert", time.time() - timeforInsertBST)

    timeforSearchBST = time.time()
    print("\nNow do 10000 searches. ")
    count = 0
    for i in range(10000):
        n = random.randint(0, 1000000)
        if bst.search(n) == bst.nil:
            count = count + 1

    print("time for BST to search =", time.time() - timeforSearchBST)
    ##print time.time()-start1, "time for BST search"
    print(count)

    timeForDeleteBST = time.time()
    print("\nNow search for & delete some nodes.")
    for i in range(1000):
        n = random.randint(0, 1000000)
        z = bst.search(n)
        if z != bst.nil:
            bst.delete(z)
    print("time for BST delete =", time.time() - timeForDeleteBST)

    ##start splay tree
    splayTree = SplayTree()
    timeforInsertSplay = time.time()
    print(
        "\nFirst insert 50000 random nodes into an empty tree and print the tree."
    )
    n = random.randint(0, 1000)
    for i in range(50000):
        n = n + 1
        splayTree.insert(
            BSTNode(n, splayTree.nil, splayTree.nil, splayTree.nil))
    print("time for splay to insert =", time.time() - timeforInsertSplay)

    timeForSearchSplay = time.time()
    print("\nNow do 10000 searches. ")
    count2 = 0
    n = random.randint(0, 1000)
    for i in range(10000):
        n = n + 1
        if splayTree.search(n) == splayTree.nil:
            count2 = count2 + 1

    print("time for splay to search =", time.time() - timeForSearchSplay)
    print(count2)

    timeForDeleteSplay = time.time()
    print("\nNow search for & delete some nodes.")
    n = random.randint(0, 1000)
    for i in range(1000):
        n = n + 1
        z = splayTree.search(n)
        if z != splayTree.nil:
            splayTree.delete(z)
    print("time for splay delete =", time.time() - timeForDeleteSplay)
Beispiel #11
0
 def __init__(self, key, left=None, right=None, parent=None):
     BSTNode.__init__(self, key, left, right, parent)
     self.height = self.node_height()
# end_time = time.time()
from BST import BSTNode
fin_start_time = time.time()

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

f = open('names_2.txt', 'r')
bst_names_2 = f.read().split("\n")  # List containing 10000 names
f.close()
# initialize an empty list for the duplicates
bst_duplicates = []
# initialize the BSTNode
names_bst = BSTNode('findnames')
# for every name in the first list
for name in bst_names_1:
    # add every name to the binary search tree
    names_bst.insert(name)
# for every name in teh second list
for name in bst_names_2:
    # check to see if the binary search tree already contains that name
    if names_bst.contains(name):
        # if it does, add it to the duplicates list
        bst_duplicates.append(name)

fin_end_time = time.time()

print(f"{len(bst_duplicates)} duplicates: \n\n{',' .join(bst_duplicates)}\n\n")
# print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")