コード例 #1
0
    def testNumChildrenWithTwoChildren(self):
        testNode = BSTNode(15)
        testNode.left = 10
        testNode.right = 30

        self.assertEqual(testNode.left, 10)
        self.assertEqual(testNode.right, 30)
コード例 #2
0
ファイル: bst_tree.py プロジェクト: brianchennn/TreeProblem
 def add_value(self, value: T) -> None:
     """
     Add value to this BST
     Duplicate values should be placed on the right
     :param value:
     :return:
     """
     self.len += 1
     z = BSTNode(value)
     y = None
     x = self.root
     while (x != None):
         y = x
         if (z.value < x.value):
             x = x.left
         else:
             x = x.right
     z.parent = y
     if (y == None):
         self.root = z
     elif (z.value < y.value):
         y.left = z
     else:
         y.right = z
     ...
コード例 #3
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_node = BSTNode("")  # implement a BST
for name_1 in names_1:  # loop through all the names in names_1
    bst_node.insert(name_1)  # to insert each name in names_1
for name_2 in names_2:  # loop through all the names in names_2
    if bst_node.contains(
            name_2
    ):  # then see if the tree has (contains) the same names as names_2
        duplicates.append(
            name_2)  # if it does, add (append) the name to the duplicated list

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
コード例 #4
0
ファイル: bst_tree.py プロジェクト: brianchennn/TreeProblem
        return not (self == other)

    def __deepcopy__(self, memodict) -> "BST[T,K]":
        """
        I noticed that for some tests deepcopying didn't
        work correctly until I implemented this method so here
        it is for you
        :param memodict:
        :return:
        """
        new_root = copy.deepcopy(self.root, memodict)
        new_key = copy.deepcopy(self.key, memodict)
        return BST(new_root, new_key)


node1 = BSTNode('c')
node2 = BSTNode('t')
node3 = BSTNode('a')
node4 = BSTNode('y')
tree = BST(node1, 5)
tree.add_value('w')
tree.remove_value('4')
tree.add_value('q')
tree.add_value('m')
tree.add_value('s')
tree.add_value('z')
tree.add_value('o')
tree.add_value('1')
tree.add_value('2')
tree.add_value('4')
tree.add_value('3')
コード例 #5
0
 def testAttributes(self):
     testNode = BSTNode(5)
     self.assertEqual(testNode.value, 5)
コード例 #6
0
    def testGetLenFollowingNode(self):
        rootNode = BSTNode(50)
        rootNode.left = BSTNode(25)
        rootNode.right = BSTNode(100)

        self.assertEqual(rootNode.getLenFollowingNode(), 3)
コード例 #7
0
    def testNumChildrenWithOneChild(self):
        testNode = BSTNode(15)
        testNode.left = 10

        self.assertEqual(testNode.left, 10)
コード例 #8
0
 def testNumChildrenWithNoChildren(self):
     testNode = BSTNode(15)
     self.assertEqual(testNode.numChildren(), 0)
コード例 #9
0
 def testGetleftWithChildren(self):
     testNode = BSTNode(50)
     testNode.left = 10
     self.assertEqual(testNode.getleft(), 10)
コード例 #10
0
 def testGetleftWithNoChildren(self):
     testNode = BSTNode(20)
     self.assertEqual(testNode.getleft(), -1)
コード例 #11
0
 def testGetrightWithChildren(self):
     testNode = BSTNode(15)
     testNode.right = 20
     self.assertEqual(testNode.getright(), 20)
コード例 #12
0
 def testGetrightWithNoChildren(self):
     testNode = BSTNode(10)
     self.assertEqual(testNode.getright(), -1)