def bst_sort(a): """ ------------------------------------------------------- Sorts an array using the Tree Sort algorithm. Use: Sorts.bst_sort(a) ------------------------------------------------------- Parameters: a - an array of comparable elements (?) Returns: None ------------------------------------------------------- """ bst = BST() for v in a: bst.insert(v) a[:] = bst.inorder() return
""" from BST_linked import BST print("Testing BST#is_balanced") print("\n\nEmpty tree") bst = BST() print("Contents: {}".format([val for val in bst])) balanced = bst.is_balanced() print("Balanced? {}".format(balanced)) print("Expected: {}".format(True)) print("\n\nNon-empty tree not balanced") bst = BST() for val in [11,22,9,33,44,25]: bst.insert(val) print("Contents: {}".format([val for val in bst])) balanced = bst.is_balanced() print("Balanced? {}".format(balanced)) print("Expected: {}".format(False)) print("\n\nNon-empty tree balanced") bst = BST() for val in [7,11,9,15,5,6,4]: bst.insert(val) print("Contents: {}".format([val for val in bst])) balanced = bst.is_balanced() print("Balanced? {}".format(balanced)) print("Expected: {}".format(True)) print("\n\nTesting BST#is_valid")
""" ------------------------------------------------------- [program description] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-04-03" ------------------------------------------------------- """ from BST_linked import BST bst = BST() num = [4, 2, 9, 3, 6] for i in num: bst.insert(i) print(bst.node_counts()) print(bst.__contains__(2)) print(bst.parent(2)) print(bst.parent_r(2))
from functions import comparison_total, do_comparisons, \ letter_table from BST_linked import BST from Letter import Letter bst_1 = BST() bst_2 = BST() bst_3 = BST() DATA1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DATA2 = "MFTCJPWADHKNRUYBEIGLOQSVXZ" DATA3 = "ETAOINSHRDLUCMPFYWGBVKJXZQ" for data in DATA1: letter = Letter(data) bst_1.insert(letter) for data in DATA2: letter = Letter(data) bst_2.insert(letter) for data in DATA3: letter = Letter(data) bst_3.insert(letter) fh = open('otoos610.txt', 'r') do_comparisons(fh, bst_1) do_comparisons(fh, bst_2) do_comparisons(fh, bst_3) total_1 = comparison_total(bst_1) total_2 = comparison_total(bst_2) total_3 = comparison_total(bst_3)
""" from BST_linked import BST from functions import comparison_total , do_comparisons from Letter import Letter DATA1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DATA2 = "MFTCJPWADHKNRUYBEIGLOQSVXZ" DATA3 = "ETAOINSHRDLUCMPFYWGBVKJXZQ" bst1 = BST() bst2 = BST() bst3 = BST() for i in DATA1: bst1.insert(Letter(i[0])) for i in DATA2: bst2.insert(Letter(i[0])) for i in DATA3: bst3.insert(Letter(i[0])) f_variable = open("miserables.txt","r") print("Comparing by order: ABCDEFGHIJKLMNOPQRSTUVWXYZ") do_comparisons(f_variable, bst1)
""" ------------------------------------------------------------------------ Assignment 8, Task 3 ------------------------------------------------------------------------ Author: Nicolas Mills ID: 180856100 Email: [email protected] __updated__ = 2019-03-22 ------------------------------------------------------------------------ """ from functions import do_comparisons, comparison_total, \ letter_table from Letter import Letter from BST_linked import BST from t02 import DATA3 bst_3 = BST() for data in DATA3: letter = Letter(data) bst_3.insert(letter) fh = open('otoos610.txt', 'r') do_comparisons(fh, bst_3) total_3 = comparison_total(bst_3) print("DATA3 BST: {}".format(total_3)) letter_table(bst_3)
[program 1] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-03-25" ------------------------------------------------------- """ from BST_linked import BST bst1 = BST() bst2 = BST() bst1.insert(3) bst1.insert(4) bst1.insert(1) bst1.insert(5) bst1.insert(9) bst1.insert(2) bst2.insert(6) bst2.insert(4) bst2.insert(7) bst2.insert(2) bst2.insert(3) bst2.insert(1) empty = bst1.is_empty() print(empty)
from BST_linked import BST print('Testing BST#node_counts') print('\n\nEmpty BST') bst = BST() zero, one, two = bst.node_counts() print("Nodes w/ ZERO children: {}\tExpected: {}".format(zero, 0)) print("Nodes w/ ONE children: {}\t\tExpected: {}".format(one, 0)) print("Nodes w/ TWO children: {}\t\tExpected: {}".format(two, 0)) print('\n\nNon-empty BST with only two nodes') bst = BST() for val in [2,4]: bst.insert(val) zero, one, two = bst.node_counts() print("Nodes w/ ZERO children: {}\tExpected: {}".format(zero, 1)) print("Nodes w/ ONE child: {}\t\tExpected: {}".format(one, 1)) print("Nodes w/ TWO children: {}\t\tExpected: {}".format(two, 0)) print('\n\nNon-empty BALANCED BST with 3 nodes') bst = BST() for val in [2,4,1]: bst.insert(val) zero, one, two = bst.node_counts() print("Nodes w/ ZERO children: {}\tExpected: {}".format(zero, 2)) print("Nodes w/ ONE child: {}\t\tExpected: {}".format(one, 0)) print("Nodes w/ TWO children: {}\t\tExpected: {}".format(two, 1)) print('\n\nNon-empty UNBALANCED BST with 3 nodes')
""" ------------------------------------------------------- [program 3] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-03-27" ------------------------------------------------------- """ from functions import letter_table, do_comparisons from Letter import Letter from BST_linked import BST DATA1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DATA2 = "MFTCJPWADHKNRUYBEIGLOQSVXZ" DATA3 = "ETAOINSHRDLUCMPFYWGBVKJXZQ" fv = open("miserables.txt","r") bst1 = BST() for letter in DATA3: ob = Letter(letter) bst1.insert(ob) do_comparisons(fv, bst1) letter_table(bst1)
""" ------------------------------------------------------- [program description] ------------------------------------------------------- Author: Max Dann ID: 190274440 Email: [email protected] __updated__ = "2020-03-31" ------------------------------------------------------- """ from BST_linked import BST bst = BST() bst.insert(11) bst.insert(7) bst.insert(15) bst.insert(6) bst.insert(9) bst.insert(12) bst.insert(18) bst.insert(8) yes = 7 no = 67 print(bst.node_counts()) print(yes in bst) print(no in bst) print(bst.parent(yes)) print(bst.parent_r(yes))
""" start = time.time() bst1 = BST() bst2 = BST() bst3 = BST() DATA1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DATA2 = "MFTCJPWADHKNRUYBEIGLOQSVXZ" DATA3 = "ETAOINSHRDLUCMPFYWGBVKJXZQ" for i in DATA1: val = Letter(i) bst1.insert(val) for i in DATA2: val = Letter(i) bst2.insert(val) for i in DATA3: val = Letter(i) bst3.insert(val) test_file = open('otoos610.txt', 'r', encoding='utf-8') test_file = test_file.read() do_comparisons(test_file, bst1) total1 = comparison_total(bst1) do_comparisons(test_file, bst2) total2 = comparison_total(bst2)
""" ------------------------------------------------------- [program description] ------------------------------------------------------- Author: Max Dann ID: 190274440 Email: [email protected] __updated__ = "2020-03-23" ------------------------------------------------------- """ from BST_linked import BST tree = BST() tree.insert(5) tree.insert(6) tree.insert(3) tree.insert(1) tree.insert(4) tree.insert(8) ''' print(tree.is_valid()) tree._root._right._value = 4 print(tree.is_valid()) ''' ''' tree2 = BST() for i in range(1,7): tree2.insert(i) '''