def init_bst():
    bst = binary_search_tree.BinarySearchTree(25)
    bst.insert(bst.root, 10)
    bst.insert(bst.root, 15)
    bst.insert(bst.root, 500)
    bst.insert(bst.root, 1000)
    bst.insert(bst.root, 4)
    return bst
Example #2
0
def get_one_blog_post(blog_post_id):
    blog_posts = BlogPost.query.all()
    random.shuffle(blog_posts)
    bst = binary_search_tree.BinarySearchTree()

    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_post_id)
    if not post:
        return jsonify({"message": "post not found"}), 404
    return jsonify(post), 200
Example #3
0
def get_one_blog_post(blog_post_id):
    blog_posts = BlogPost.query.all()  #BlogPost is a database which we query
    # list of all posts, ascending, want to insert into BST
    # Want to search the BST using the unique blog_post_id
    # Make BST more balanced by randomising inserted blog posts
    bst = binary_search_tree.BinarySearchTree()

    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_post_id)  # returns the post given the blog_post_id
    if not post:
        # if no id found
        return jsonify({"message": "post not found"})
    return jsonify(post)
Example #4
0
def get_one_blog_post(blog_post_id):
    """Search for a blog post using the binary search method and
    return blog post data as a JSON object or provide an error
    message if requesting blog post id does not exist.

    Args:
        blog_post_id (int): numeric blog post id

    Returns:
        JSON: binary search result
    """

    # Query all the blog post data
    blog_posts = BlogPost.query.all()

    # Shuffle data to optimize future search tree
    random.shuffle(blog_posts)

    # Create BinarySearchTree instance
    bst = binary_search_tree.BinarySearchTree()

    # Insert all retrieved data to the tree
    for post in blog_posts:
        bst.insert({
            "id": post.id,
            "title": post.title,
            "body": post.body,
            "user_id": post.user_id,
        })

    # Search post using binary search method
    post = bst.search(blog_post_id)

    if not post:
        return jsonify({"message": "post not found"})

    return jsonify(post)
Example #5
0
def get_one_blog_post(blog_post_id):
    # this retrieves all the blog posts from the BlogPost table in ascending order by default
    blog_posts = BlogPost.query.all()
    # performing a shuffle over the retrieved data so that it is not in any particular order
    random.shuffle(blog_posts)

    # creating an object of BinarySearchTree to access all the methods belonging to the binary search tree
    bst = binary_search_tree.BinarySearchTree()
    # traversing through each of the post
    for post in blog_posts:
        # calling the user defined insert function of BinarySearchTree to insert the data
        bst.insert({
            "id": post.id,
            "title": post.title,
            "body": post.body,
            "user_id": post.user_id
        })
    # calling the user defined search function of BinarySearchTree to search for a particular blog post
    post = bst.search(blog_post_id)
    print(post)
    if not post:
        return jsonify({"message": "post not found"}), 400
    else:
        return jsonify(post), 200

def successor(tree, data):
    node = tree.lookup(data)
    if tree.maximum(tree.get_root()) == data:
        return None
    if node.right is not None:
        return tree.minimum(node.right)
    else:
        while node == node.parent.right:
            node = node.parent
        return node.parent.data


if __name__ == "__main__":
    tree = binary_search_tree.BinarySearchTree(25)
    tree.insert(15)
    tree.insert(50)
    tree.insert(10)
    tree.insert(22)
    tree.insert(35)
    tree.insert(70)
    tree.insert(4)
    tree.insert(12)
    tree.insert(18)
    tree.insert(24)
    tree.insert(31)
    tree.insert(44)
    tree.insert(66)
    tree.insert(90)
    # tree.inorder(tree.get_root(), tree.print_data)
Example #7
0
#################################################
# ###  Author: Samyuel Danyo
# ###  Date: 07/06/2020
# ###  Last Edit: 07/06/2020
##################################################
# ## imports
# Python Standard Library
from random import randint

# Local Application/Library Specific Imports.
import binary_search_tree as bst
##################################################
# Test
##################################################
BST_ROOT = bst.BSTNode(2)
BST = bst.BinarySearchTree(BST_ROOT)
for data in range(5):
    print("Insert: {} | Feedback: {}"
          .format(data, BST.insert(data)))
for i in range(15):
    data = randint(-100, 100)
    print("Insert: {} | Feedback: {}"
          .format(data, BST.insert(data)))
print("--- Return(__str__) ---")
print(BST)
print("Remove: {} | Feedback: {}"
      .format(1, BST.remove(1)))
print("BST.size() =", BST.size())
print("Remove: {} | Feedback: {}"
      .format(200, BST.remove(200)))
print("--- Pre-Order Recursive ---")
Example #8
0
    stack = []
    result = []
    curr = tree.get_root()
    while curr is not None or len(stack) > 0:
        if curr is not None:
            stack.append(curr)
            curr = curr.left
        else:
            curr = stack.pop()
            result.append(curr.data)
            curr = curr.right
    return result


if __name__ == "__main__":
    tree1 = binary_search_tree.BinarySearchTree(25)
    tree1.insert(15)
    tree1.insert(50)
    tree1.insert(10)
    tree1.insert(22)
    tree1.insert(35)
    tree1.insert(70)
    tree1.insert(4)
    tree1.insert(12)
    tree1.insert(18)
    tree1.insert(24)
    tree1.insert(31)
    tree1.insert(44)
    tree1.insert(66)
    tree1.insert(90)
    #tree1.inorder(tree1.get_root(), tree1.print_data)
Example #9
0
import graph
import binary_search_tree

n = -1
bst = binary_search_tree.BinarySearchTree()
for i in 8, 3, 1, 6, 10, 4, 7, 14, 13:
    n += 1
    bst.insert(i)
    graph.render(bst, 3, 'img_%02d' % n)

n += 1
bst.delete_left(8)
graph.render(bst, 3, 'img_%02d' % n)

n += 1
bst.delete_right(7)
graph.render(bst, 3, 'img_%02d' % n)

n += 1
bst.delete_right(3)
graph.render(bst, 3, 'img_%02d' % n)
 def setUp(self):
     self.SUT = binary_search_tree.BinarySearchTree([4, 1, 6, 0, 12, 7, 13, 5])