Ejemplo n.º 1
0
def find_suspects(people):
    people_inside = BST()
    # min heap (ordered by exit time)
    building = Heap(key=lambda person: person.exit)
    # list of SuspectGroups
    suspects = []
    for person in people:
        next_to_leave = building.top()
        while next_to_leave is not None and not next_to_leave.shares_time_with(
                person):
            same_time = [building.pop()]
            while building.top(
            ) is not None and next_to_leave.exits_at_same_time(building.top()):
                same_time.append(building.pop())
            append_if_are_suspects(suspects, people_inside, next_to_leave)
            for next in same_time:
                people_inside.remove(next)
            next_to_leave = building.top()
        people_inside.insert(person)
        building.push(person)

    while len(people_inside) > 4:
        next_to_leave = [building.pop()]
        while building.top(
        ) is not None and next_to_leave[0].exits_at_same_time(building.top()):
            next_to_leave.append(building.pop())
        append_if_are_suspects(suspects, people_inside, next_to_leave[0])
        for next in next_to_leave:
            people_inside.remove(next)
    return suspects
Ejemplo n.º 2
0
def get_one_blog_posts(blog_post_id):
    blog_posts = BlogPost.query.all()
    random.shuffle(blog_posts)

    bst = BST()

    for post in blog_posts:
        bst.insert({
            "id": post.id,
            "title": post.title,
            "body": post.body,
            "user_id": post.user_id,
        })

    post = bst.search_blog(blog_post_id)

    if not post:
        return jsonify({"message": "Blog Post not found"}), 404

    return jsonify(post), 200
Ejemplo n.º 3
0
def main():
    pass
    b = BST()
    b.insert(12)
    b.insert(9)
    b.insert(13)
    b.preorder()
class BSTTest(unittest.TestCase):

    def setUp(self):
        self.bst = BST()
        # node0 = BinarySearchTreeNode(1)
        # node1 = BinarySearchTreeNode(2)
        # node2 = BinarySearchTreeNode(3)
        # node3 = BinarySearchTreeNode(4)
        # node4 = BinarySearchTreeNode(5)
        # self.bst.insert(node0)
        # self.bst.insert(node4)
        # self.bst.insert(node2)
        # self.bst.insert(node1)
        # self.bst.insert(node3)

        self.bst.insert(Node(3))
        self.bst.insert(Node(7))
        self.bst.insert(Node(1))
        self.bst.insert(Node(5))
        # self.bst.print_in_order()

    def test_is_valid(self):
        self.assertEqual(True, self.bst.is_valid())
Ejemplo n.º 5
0
"""
source: https://leetcode.com/problems/same-tree/

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

"""

from binary_search_tree import BST

p = BST()
p.insert(17)
p.insert(12)
p.insert(25)
p.insert(9)
p.insert(21)
p.insert(27)

q = BST()
q.insert(17)
q.insert(12)
q.insert(25)
q.insert(9)
q.insert(21)
# q.insert(27)


def isSameTree(p, q):
    # algo1
    # def fn(p, q):
Ejemplo n.º 6
0
class BinaryTree:
    def __init__(root: Node):
        self.root = Node
        self.left = None
        self.right = None

    def insert_node(n: Node):
        # TODO: implement generic binary tree insert.
        pass


def breath_first_search(T: BST):
    queue = [T]
    result = []
    while queue:
        cur_node = queue.remove(0)
        if cur.left is not None:
            queue.append(cur.left)
        if cur.right is not None:
            queue.append(cur.right)
        result.append(cur.key)
    return result


if __name__ == "__main__":
    bst = BST(1)
    bst.insert(4)
    bst.insert(2)
    bst.insert(3)
    bst.insert(3)
Ejemplo n.º 7
0
# given a binary search tree, count how many nodes are greater than the root's value

#         10
#        /   \
#       5     12
#     /   \      \
#    3      7      14
#                /    \
#              13       20

from binary_search_tree import BST

t = BST()
t.insert(10)
t.insert(5)
t.insert(12)
t.insert(3)
t.insert(7)
t.insert(14)
t.insert(13)
t.insert(20)

tree_root_value = t.root.value


def is_greater_root(node):
    if node is None: return 0
    count = 0

    count = is_greater_root(node.left)
    count += 1
from binary_search_tree import BST
bst = BST(9)
bst.insert(3)
bst.insert(2)
bst.insert(5)
bst.insert(4)
bst.insert(6)
bst.insert(7)
bst.insert(9)
bst.insert(8)
bst.insert(11)
bst.insert(13)
bst.insert(12)
bst.insert(10)
bst.insert(15)
bst.insert(14)
arr = bst.inorder_traverse()
Bbst = BST()
print(arr)


# print(Bbst.get_height())
def create_Bbst(a):
    n = len(a)
    if len(a) != 0:
        if n == 1:
            Bbst.insert(a[0])
        else:
            mid = int(n / 2)
            Bbst.insert(a[mid])
            create_Bbst(a[:mid])
Ejemplo n.º 9
0
import sys
sys.path.append('..')

from binary_search_tree import BST

# Set up tree
tree = BST(4)

# Insert elements
tree.insert(2)
tree.insert(1)
tree.insert(3)
tree.insert(5)

# Check search
# Should be True
assert (tree.search(4) == True), "Failed in tree.search"
# Should be False
assert (tree.search(6) == False), "Failed in tree.search"

print("ALL TEST PASSED")
Ejemplo n.º 10
0
from binary_search_tree import BST

bst = BST()

bst.insert(-1)
bst.insert(0)
bst.insert(1)
bst.insert(2)


print(bst.getMin())
print(bst.getMax())
print(bst.traverseInOrder())
Ejemplo n.º 11
0
from binary_search_tree import BST

bst = BST()
bst.insert(60, 'sixty')
bst.insert(40, 'forty')
bst.insert(70, 'seventy')
bst.insert(75, 'seventy five')
bst.insert(80, 'eighty')
bst.insert(81, 'eighty one')
bst.insert(79, 'seventy nine')
bst.insert(77, 'seventy seven')
bst.insert(65, 'sixty five')
# bst.insert(76, 'seventy six')
bst.insert(30, 'thirty')
bst.insert(20, 'twenty')
bst.insert(32, 'thirty two')
bst.insert(15, 'fifteen')
bst.insert(18, 'eighteen')
bst.insert(22, 'twenty two')
# bst.insert(28, 'twenty eight')
# bst.insert(31, 'thirty one')
bst.print_self()
# bst.insert(30, 'thirty')
# bst.insert(50, 'fifty')
# bst.insert(45, 'forty five')
# bst.insert(41, 'forty one')
# bst.insert(46, 'forty six')
# bst.insert(75, 'seventy five')
# min = bst.find_min()
# max = bst.find_max()
# print('max', max.value)