def robot_grid_aux(tree, m, n, aux):
        global end
        tree.e = (m, n)
        # end = tree
        # end.e = (m,n)
        # print(tree.e)

        if (m == len(aux) - 1) and (n == len(aux[0]) - 1):
            print(m, n)
            for a in aux:
                print(a)
            end = tree
            return  # return tree

        if (aux[m][n] != 0):
            pass

        if (m + 1 < len(aux)) and (aux[m + 1][n] == 0):
            tree.left = BSTNode()
            tree.left.parent = tree
            aux[m][n] = -1
            robot_grid_aux(tree.left, m + 1, n, aux)
            # aux[m][n] = -1

        if (n + 1 < len(aux[0])) and (aux[m][n + 1] == 0):
            tree.right = BSTNode()
            tree.right.parent = tree
            aux[m][n] = -1
            robot_grid_aux(tree.right, m, n + 1, aux)
Exemplo n.º 2
0
def _build_tree(array, l, r):
    if not array or l > r:
        return None

    mid = (l + r) // 2

    root = BSTNode(array[mid])
    root.left = _build_tree(array, l, mid - 1)
    root.right = _build_tree(array, mid + 1, r)
    return root
Exemplo n.º 3
0
 def test_append_multi(self):
     test_node = BSTNode(5)
     test_node.append(BSTNode(3))
     test_node.append(BSTNode(1))
     test_node.append(BSTNode(4))
     self.assertEqual(test_node.left.right.value, 4)
     self.assertEqual(test_node.left.left.value, 1)
def robot_grid(mat):
    tree = BSTNode()
    global end
    end = tree

    def robot_grid_aux(tree, m, n, aux):
        global end
        tree.e = (m, n)
        # end = tree
        # end.e = (m,n)
        # print(tree.e)

        if (m == len(aux) - 1) and (n == len(aux[0]) - 1):
            print(m, n)
            for a in aux:
                print(a)
            end = tree
            return  # return tree

        if (aux[m][n] != 0):
            pass

        if (m + 1 < len(aux)) and (aux[m + 1][n] == 0):
            tree.left = BSTNode()
            tree.left.parent = tree
            aux[m][n] = -1
            robot_grid_aux(tree.left, m + 1, n, aux)
            # aux[m][n] = -1

        if (n + 1 < len(aux[0])) and (aux[m][n + 1] == 0):
            tree.right = BSTNode()
            tree.right.parent = tree
            aux[m][n] = -1
            robot_grid_aux(tree.right, m, n + 1, aux)
            # aux[m][n] = -1

    # return tree

    robot_grid_aux(tree, 0, 0, mat)
    path = [end]
    p = end.parent
    while p != None:
        path.append(p)
        p = p.parent

    return path[::-1]
            path_dist = d[(node.parent, node)]

            parent = node.parent
            while parent.parent != None:
                path_dist += d[(parent.parent, parent)] - parent.e
                d[(parent.parent, node)] = path_dist
                parent = parent.parent

    if node.left != None:
        compute_path_sums(node.left, d)
    if node.right != None:
        compute_path_sums(node.right, d)


if __name__ == "__main__":

    a = BSTNode(e=4)
    a.insert(2)
    a.insert(5)
    a.insert(-1)
    a.insert(3)
    a.insert(-5)
    a.insert(-3)
    a.insert(7)
    a.insert(10)

    d = {}
    compute_path_sums(a, d)
    for thing in d:
        print(thing, d[thing])
    b = get_height(root.right)
    
    if (a == -np.inf):
        return -np.inf
    if (b == -np.inf):
        return -np.inf4
    
    if abs(a-b) > 1:
        return -np.inf
    else:
        return max(a,b)+1

def is_balanced(root):
    return get_height(root) != -np.inf

a = BSTNode("A")
b = BSTNode("B")
c = BSTNode("C")
d = BSTNode("D")
e = BSTNode("E")

a.left = b
a.right = c
b.left = d
b.right = e

print(is_balanced(a))
print(is_balanced(b))
print(is_balanced(d))

f = BSTNode("F")
Exemplo n.º 7
0
 def test_append(self):
     test_node = BSTNode(5)
     test_node.append(BSTNode(3))
     self.assertEqual(test_node.left.value, 3)
     self.assertEqual(test_node.right, None)
Exemplo n.º 8
0
 def test_init(self):
     test_node = BSTNode(5)
     self.assertEqual(test_node.value, 5)
        l_0 = l_1
        l_1 = []

    return l_0


def insert_all_aux(a, e):
    l = []
    for i in range(len(a) + 1):
        new_a = a[:]
        new_a.insert(i, e)
        l.append(new_a)
    return l


if __name__ == "__main__":

    a = BSTNode(e=4)
    b = BSTNode(e=2)
    c = BSTNode(e=5)
    d = BSTNode(e=1)
    e = BSTNode(e=3)
    a.left = b
    a.right = c
    b.left = d
    b.right = e

    for e in bst_sequences(a):
        print(e)
Exemplo n.º 10
0
from tree import BSTNode, build_default_tree


def check_balance(t):
    _, balanced = _height(t)
    return balanced


def _height(root):
    if not root:
        return 0, True
    l, ok = _height(root.left)
    if not ok:
        return None, False
    r, ok = _height(root.right)
    if not ok:
        return None, False

    return max([l, r]) + 1, abs(l - r) <= 1


if __name__ == "__main__":
    t = build_default_tree()
    print(check_balance(t))

    t = BSTNode(4)
    t.insert(2)
    t.insert(1)
    t.insert(3)
    print(check_balance(t))
def are_trees_identical(t1, t2):
    if (t1 == None and t2 != None) or (t1 != None and t2 == None):
        return False
    if t1.e != t2.e:
        return False
    if t1.right == None and t1.left == None and t2.left == None and t2.right == None:
        return True
    else:
        return (are_trees_identical(t1.left, t2.left)
                and are_trees_identical(t1.right, t2.right))


if __name__ == "__main__":

    a = BSTNode(e=4)
    b = BSTNode(e=2)
    c = BSTNode(e=5)
    d = BSTNode(e=1)
    e = BSTNode(e=3)
    a.left = b
    a.right = c
    b.left = d
    b.right = e

    f = BSTNode(e=2)
    g = BSTNode(e=1)
    h = BSTNode(e=3)
    f.left = g
    f.right = h
Exemplo n.º 12
0

def validate_bst(root, min_=-np.inf, max_=np.inf):
    if root == None:
        return True

    if root.e < min_:
        return False
    if root.e > max_:
        return False

    return validate_bst(root.left, min_, root.e) and validate_bst(
        root.right, root.e, max_)


a = BSTNode("A")
b = BSTNode("B")
c = BSTNode("C")
d = BSTNode("D")
e = BSTNode("E")
f = BSTNode("F")
g = BSTNode("G")
h = BSTNode("H")
i = BSTNode("I")
j = BSTNode("J")
k = BSTNode("K")
l = BSTNode("L")
m = BSTNode("M")
n = BSTNode("N")
o = BSTNode("O")
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

# This function has O(n^2) runtime complexity
# 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)
#             break

# Start tree with middle-ish letter
bst = BSTNode('L')
# Insert all names from first list into Binary Search Tree
for name in names_1:
    bst.insert(name)

# Search for names from second list in BST, append to duplicates if found
for name in names_2:
    if bst.contains(name) == True:
        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
    
    if n.parent != None:
        if n == n.parent.left:
            return n.parent
        
    p = n
    while p.parent != None:
        if p == p.parent.left:
            return p.parent
        p = p.parent
        
    return None
    
    

a = BSTNode("A")
b = BSTNode("B")
c = BSTNode("C")
d = BSTNode("D")
e = BSTNode("E")
f = BSTNode("F")
g = BSTNode("G")
h = BSTNode("H")
i = BSTNode("I")
j = BSTNode("J")
k = BSTNode("K")
l = BSTNode("L")
m = BSTNode("M")
n = BSTNode("N")

a.assign((b,c))