def test_WideAllNodes(self):
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.WideAllNodes())],
         [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15])
     self.tree = BST(BSTNode(8, 80, None))
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])
Exemple #2
0
 def setUp(self) -> None:
     self.tree = BST(None)
     self.tree.AddKeyValue(4, "value_4")
     self.tree.AddKeyValue(2, "value_2")
     self.tree.AddKeyValue(6, "value_6")
     self.tree.AddKeyValue(5, "value_5")
     self.tree.AddKeyValue(7, "value_7")
def main():
    pass
    b = BST()
    b.insert(12)
    b.insert(9)
    b.insert(13)
    b.preorder()
Exemple #4
0
 def setUp(self) -> None:
     self.tree = BST(None)
     self.tree.AddKeyValue(4, "value_4")
     self.tree.AddKeyValue(2, "value_2")
     self.tree.AddKeyValue(6, "value_6")
     self.tree.AddKeyValue(1, "value_1")
     self.tree.AddKeyValue(3, "value_3")
Exemple #5
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
 def test_DeepAllNodes(self):
     # проверка post-order
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.DeepAllNodes(1))],
         [1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8])
     self.tree = BST(BSTNode(8, 80, None))
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])
Exemple #7
0
def binary_search(lst, lst2):
    bst = BST(names_1[0])
    for i in range(len(names_1)):
        if i != 0:
            bst.insert(names_1[i])
    for name_2 in names_2:
        if bst.contains(name_2):
            duplicates.append(name_2)
 def test_DeepAllNodes(self):
     # проверка in-order
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.DeepAllNodes(0))],
         [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
     self.tree = BST(BSTNode(8, 80, None))
     self.assertEqual(
         [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])
Exemple #9
0
def main(argv):
    tree = BST([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
    tree.preorder_traversal(tree.root)
    print("Next largest value of node {} is: {}".format(
        tree.root.left.left.value,
        next_largest(tree.root.left.left).value))
    print("Next largest value of node {} is: {}".format(
        tree.root.left.right.value,
        next_largest(tree.root.left.right).value))
    print("Next largest value of node {} is: {}".format(
        tree.root.left.left.right.value,
        next_largest(tree.root.left.left.right).value))
 def setUp(self):
     # Создание дерева из 15 злементов начиная от корня (8)
     self.tree = BST(BSTNode(8, 80, None))
     self.tree.AddKeyValue(4, 40)
     self.tree.AddKeyValue(12, 120)
     self.tree.AddKeyValue(2, 20)
     self.tree.AddKeyValue(6, 60)
     self.tree.AddKeyValue(10, 100)
     self.tree.AddKeyValue(14, 140)
     self.tree.AddKeyValue(1, 10)
     self.tree.AddKeyValue(3, 30)
     self.tree.AddKeyValue(5, 50)
     self.tree.AddKeyValue(7, 70)
     self.tree.AddKeyValue(9, 90)
     self.tree.AddKeyValue(11, 110)
     self.tree.AddKeyValue(13, 130)
     self.tree.AddKeyValue(15, 150)
Exemple #11
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
Exemple #12
0
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(8, "")
        self.tree.AddKeyValue(4, "")
        self.tree.AddKeyValue(12, "")
        self.tree.AddKeyValue(2, "")
        self.tree.AddKeyValue(6, "")
        self.tree.AddKeyValue(1, "")
        self.tree.AddKeyValue(5, "")
        self.tree.AddKeyValue(7, "")
        self.tree.AddKeyValue(10, "")
        self.tree.AddKeyValue(14, "")
        self.tree.AddKeyValue(11, "")
        self.tree.AddKeyValue(13, "")
        self.tree.AddKeyValue(15, "")
        self.tree.AddKeyValue(16, "")

        self.assertEqual(8, self.tree.Root.NodeKey)
        self.assertEqual(4, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(1,
                         self.tree.Root.LeftChild.LeftChild.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.LeftChild.RightChild.NodeKey)
        self.assertEqual(
            7, self.tree.Root.LeftChild.RightChild.RightChild.NodeKey)
        self.assertEqual(5,
                         self.tree.Root.LeftChild.RightChild.LeftChild.NodeKey)

        self.assertEqual(12, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(10, self.tree.Root.RightChild.LeftChild.NodeKey)
        self.assertEqual(
            11, self.tree.Root.RightChild.LeftChild.RightChild.NodeKey)
        self.assertEqual(14, self.tree.Root.RightChild.RightChild.NodeKey)
        self.assertEqual(
            13, self.tree.Root.RightChild.RightChild.LeftChild.NodeKey)
        self.assertEqual(
            15, self.tree.Root.RightChild.RightChild.RightChild.NodeKey)
        self.assertEqual(
            16,
            self.tree.Root.RightChild.RightChild.RightChild.RightChild.NodeKey)
Exemple #13
0
def main(argv):
    tree = BST([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
    tree.preorder_traversal(tree.root)
    nodea = tree.root.left.left
    nodeb = tree.root.right.left.left
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value, lowest_common_ancestor(nodea, nodeb)))
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value,
        lowest_common_ancestor_recursive(nodea, nodeb)))
    nodea = tree.root.left.left
    nodeb = tree.root.left.right.left
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value, lowest_common_ancestor(nodea, nodeb)))
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value,
        lowest_common_ancestor_recursive(nodea, nodeb)))
    nodea = tree.root.left.left
    nodeb = tree.root.left.left.right
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value, lowest_common_ancestor(nodea, nodeb)))
    print("Lowest common ancestor of {} and {} is: {}".format(
        nodea.value, nodeb.value,
        lowest_common_ancestor_recursive(nodea, nodeb)))
Exemple #14
0
 def setUp(self) -> None:
     self.tree = BST(BSTNode(4, "value_4", None))
Exemple #15
0
 def setUp(self) -> None:
     self.tree = BST(None)
     self.tree.AddKeyValue(4, "value_4")
#
#Kristian Knudsen Damsgaard, 2019
#
#Testing for the binary tree project
#
#

from binary_search_tree import BinarySearchTree as BST
from avl import AVL

#TESTING BinarySearchTree -------------------------------------------------------------------------

tree = BST()
# #Tests getNode() on empty tree
# print(tree.getNode(0))
# #Tests getNode() with non-integer argument
# tree.insertNode("ti", "data_ti")
# #Tests insertNode() on empty tree
# tree.insertNode(10, "data_10")
# #Tests getNode()
# print( "Key 10: " + str(tree.getNode(10).getKey()) )
# #Tests insertNode() on duplicate key
# tree.insertNode(10, "data_10")
# #Tests insertNode() on non-empty tree (left side)
# tree.insertNode(9, "data_9")
# print( "Key 9: " + str(tree.getNode(9).getKey()) )
# #Tests insertNode() on non-empty tree (right side)
# tree.insertNode(11, "data_11")
# print( "Key 11: " + str(tree.getNode(11).getKey()) )
# # Tests getParent()
# print( "Parent of 9: " + str(tree.getNode(9).getParent().getKey()) )
 def test_Count(self):
     # проверка подсчёта количества узлов дерева
     self.assertEqual(self.tree.Count(), 15)
     self.tree = BST(BSTNode(8, 80, None))
     self.assertEqual(self.tree.Count(), 1)
Exemple #18
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)
Exemple #19
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")
Exemple #20
0
from binary_search_tree import BST

if __name__ == '__main__':
    T1 = BST(7)
    T1.insert_bulk([1, 8, 23, 12, 5])

    T2 = BST(54)
    T2.insert_bulk([1, 8, 23, 12, 5])

    n1 = T1.minValue(T1.root)
    n2 = T2.minValue(T2.root)

    while (n1 is not None and n2 is not None):
        if n1.value == n2.value:
            print n1.value
            n1 = T1.GetInorderSuccesor(n1)
            n2 = T2.GetInorderSuccesor(n2)
        else:
            if n1.value > n2.value:
                n2 = T2.GetInorderSuccesor(n2)
            else:
                n1 = T1.GetInorderSuccesor(n1)
    print "This is it"
Exemple #21
0
#!/usr/bin/env python

__author__ = "bt3"

from binary_search_tree import BST, Node
from binary_tree import BT, Node


def isBalanced(node, left=0, right=0):
    if not node:
        return (left - right) < 2

    return isBalanced(node.left, left+1, right) and \
        isBalanced(node.right, left, right+1)


if __name__ == '__main__':
    bt = BST()
    for i in range(1, 10):
        bt.add(i)

    assert (isBalanced(bt.root) == True)

    bt = BT()
    for i in range(1, 10):
        bt.add(i)

    assert (isBalanced(bt.root) == False)
Exemple #22
0
 def setUp(self) -> None:
     self.tree = BST(None)
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])
def find_ancestor2(tree, n1, n2):
    if not tree:
        return False

    if n1 <= tree.item and n2 >= tree.item or (not tree.left
                                               and not tree.right):
        return tree.item

    if tree.left and (n1 < tree.item and n2 < tree.item):
        return find_ancestor(tree.left, n1, n2) or tree.item

    if tree.right and (n1 > tree.item and n2 > tree.item):
        return find_ancestor(tree.right, n1, n2) or tree.item


if __name__ == '__main__':
    bst = BST()
    l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
    for i in l:
        bst.add(i)
    nodes = bst.preorder_array()

    print 'Original:    ', l
    print 'Preorder:    ', nodes

    print 'Method 1: '
    print 'Ancestor for 3, 11:', find_ancestor(nodes, 3, 11)

    print 'Method 2: '
    print 'Ancestor for 3, 11: ', find_ancestor2(bst.root, 3, 11)
Exemple #25
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):
Exemple #26
0
    def setUp(self) -> None:

        self.root = BST(12)
        self.empty_root = BST()
        self.sample = random.sample(range(1000), k=5)