示例#1
0
def testTreeWithOneNode():
    print(divider + "Executing testTreeWithOneNode()...")
    tree = BinaryTree()
    tree.insert(Node(0))
    assert (tree.size == 1)
    assert (tree.height == 0)
    print("Passed" + divider)
示例#2
0
def testBFSSingleInsert():
    print(divider + "Executing testBFSSingleInsert()...")
    nodes = []
    tree = BinaryTree()
    tree.insert(Node(7))
    nodes = tree.getNodesBFS()
    print(nodesToString(nodes))
    assert (nodes[0].data == 7)
    print("Passed" + divider)
示例#3
0
def testSingleInserts(loops):
    print(divider + "Executing testSingleInserts()...")
    for i in range(loops):
        tree = BinaryTree()
        val = randint(MIN_INT_32, MAX_INT_32)
        tree.insert(Node(val))
        assert (tree.size == 1)
        assert (tree.height == 0)
    print("Passed" + divider)
示例#4
0
def testSequentialInsert():
    print(divider + "Executing testSequentialInsert()...")
    tree = BinaryTree()
    for i in range(1, 11):
        tree.insert(Node(i))
    assert (tree.root.data == 1)
    assert (tree.size == 10)
    #print("Tree height is {}, expected to be {}".format(tree.height, 9))
    assert (tree.height == 9)
    print("Passed" + divider)
示例#5
0
def testBFSRandom():
    print(divider + "Executing testBFSRandom()...")
    tree = BinaryTree()
    expectedNodes = [32, 16, 50, 2, 30, 48, 53, 5, 18, 12]
    for num in randomNumbers:
        tree.insert(Node(num))
    bfsNodes = tree.getNodesBFS()
    print(nodesToString(bfsNodes))
    # Ensure that the order of the nodes is in the proper order for BFS
    for i in range(0, len(expectedNodes)):
        assert (bfsNodes[i].data == expectedNodes[i])
    print("Passed" + divider)
示例#6
0
def testRandomInsert():
    print(divider + "Executing testRandomInsert()...")
    numbers = [7, 2, 10, 4, 3, 1, 5, 8, 6, 9]
    tree = BinaryTree()
    for number in numbers:
        tree.insert(Node(number))
    assert (tree.root.data == 7)
    assert (tree.size == 10)
    print("Tree height is {}, expected to be {}".format(tree.height, 4))
    tree.printDFS()
    print()
    tree.printBFS()
    assert (tree.height == 4)
    print("Passed" + divider)
示例#7
0
def testDFSRandom():
    print(divider + "Executing testDFSRandom()...")
    tree = BinaryTree()
    expectedNodes = [32, 16, 2, 5, 12, 30, 18, 50, 48, 53]
    for number in randomNumbers:
        tree.insert(Node(number))
    assert (tree.size == len(randomNumbers))
    nodes = tree.getNodesDFS()
    print("Returned nodes = {}".format(nodesToString(nodes)))
    print("Expected nodes = {}".format([n for n in expectedNodes]))

    # Ensure that the order of the nodes is in the proper order for DFS
    for i in range(0, len(nodes)):
        assert (nodes[i].data == expectedNodes[i])
    print("Passed" + divider)
示例#8
0
def testFind():
    print(divider + "Executing testFind()...")
    tree = BinaryTree()
    lowerBound = 0
    upperBound = 100

    # Insert values
    for i in range(lowerBound, upperBound):
        tree.insert(Node(i))

    # Ensure all inserted values are found
    for i in range(lowerBound, upperBound):
        assert (tree.find(i, tree.root))

    # Ensure values outside of range are not found
    for i in range(-10000, lowerBound):
        assert (not tree.find(i, tree.root))

    for i in range(upperBound, 10000):
        assert (not tree.find(i, tree.root))

    print("Passed " + divider)
示例#9
0
    for index in range(len(sorted_list) - 1):
        if sorted_list[index] > sorted_list[index + 1]:
            return False

    return True
# -----------------------------------------------------------------------------|

head_value = 15
head_node = Node(value = head_value)
binary_tree = BinaryTree(head = head_node)

my_random_list = [head_value]

# trial insert
for iteration in range(20000):
    random_number = random.randint(0, 2000)
    new_node = Node(value = random_number)
    binary_tree.insert(new_node = new_node)
    my_random_list.append(random_number)

# print("List was", my_random_list)
print("Sorting status: ", check_sort(my_random_list, binary_tree.sort()))
print(len(binary_tree))

# slow method. use numpy
my_random_list.remove(15)

print("Removed 15 = ", binary_tree.remove(15))
print("Sorting status: ", check_sort(my_random_list, binary_tree.sort()))

print(len(binary_tree))
import random
from binaryTree import BinaryTree

tree = BinaryTree()

#Insertion
values = [
    5, 7, 10, 11, 12, 14, 17, 21, 22, 23, 30, 34, 35, 41, 52, 56, 61, 66, 70,
    77, 82, 84, 89, 91, 99, 100
]
for value in values:
    tree.insert(tree.root, value)

print('Initial tree:\n')

tree.printTree(tree.root)

print('\nTree after removal: \n')

#Removal
tree.remove(tree.root, 5)
tree.remove(tree.root, 10)
tree.remove(tree.root, 21)
tree.remove(tree.root, 99)

tree.printTree(tree.root)
示例#11
0
def mirrorTree(root):
    if not root:
        return root
    newRoot = root
    helper(newRoot)
    return newRoot


def helper(node):
    if not node:
        return
    left = node.left
    node.left = node.right
    node.right = left
    helper(node.left)
    helper(node.right)


tree = BinaryTree(5)
tree.insert(3)
tree.insert(4)
tree.insert(6)
tree.insert(7)

display(tree.root)

mirroredTree = mirrorTree(tree.root)
print("Mirrored Tree")
display(mirroredTree)
    # Get the result from left child
    left_result = _get_kth_smallest_helper(root.left, k, pos)
    # Check if you have already found the result
    if left_result[0]:
        return left_result
    else:
        # Update the position and see if it's kth smallest node.
        # If so, return the True value.
        pos = left_result[2] + 1
        if pos == k:
            return (True, root, pos)
    # Get the result from the right child.
    return _get_kth_smallest_helper(root.right, k, pos)


tree = BinaryTree(3)
tree.insert(1)
tree.insert(4)
tree.insert(2)

assert get_kth_smallest(tree.root, 1) == 1

tree = BinaryTree(5)
tree.insert(3)
tree.insert(2)
tree.insert(6)
tree.insert(4)
tree.insert(1)

assert get_kth_smallest(tree.root, 3) == 3