Ejemplo n.º 1
0
class key_return:

	def __init__(self):
		complicated_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'crypt_db.xlsx')
		excel_sheet = pd.read_excel(complicated_path, "Sheet1", parse_cols=1)
		# The headers are name and key but if you wanted something more robust do
		'''
			headers = [i for i in x]
		'''
		# Creating a Binary Tree
		self.key_tree = BinaryTree()
		
		keys = [i for i in excel_sheet['Name']]
		values = [i for i in excel_sheet['Key']]
		
		key_bank = dict(((keys[i],values[i]) for i in range(len((keys)))))
		
		for key in key_bank.keys():
			self.key_tree.put(key, key_bank[key])

	# Returns None if nothing is found
	def get_key(self, key):
		# return the crypted version of the key 
		value = self.key_tree.get(key)
		return value
Ejemplo n.º 2
0
def data_set(node, file):
    '''
    This function sets the categorgy and data of the file
    '''

    file_read = file.readline()  # variable created
    file_read2 = file.readline()  # variable created
    node.setData(
        file_read2)  # calls the setData function from the BinaryTree class
    if file_read != "Question:\n":
        return
    node.left = BinaryTree()  # it goes to the left
    data_set(node.left, file)  # recursive
    node.right = BinaryTree()  # it goes to the right
    data_set(node.right, file)  # recursive
Ejemplo n.º 3
0
def main():
    head = BinaryTree()  # sets the class we imported to head
    open_file = open("gamedata.txt", 'r')  #opens and reads the txt file
    data_set(head, open_file)  # we call the function from above
    while head.left != None and head.right != None:
        print(head.data)  # prints the data of head
        user_input = input(
            "Enter Yes or No: ")  # asks for a answer "yes" or "no"
        if user_input.lower() == "yes":  # if yes than it goes to the right
            head = head.right
        elif user_input.lower() == "no":  # if no than it goes to the left
            head = head.left
    print(head.data)
    make_sure = input(
        "Is the guess of the animal correct? ")  # asks user for confirmation
    if make_sure.lower() == "yes":
        print("GRRAA! I knew it!")
    else:
        TheCorrectAnimal = input(
            "What is the animal that you guessed? ") + "\n"
        question_help = input(
            "Insert a question that whould allow the AI to make a correct guess?: "
        ) + "\n"
        node_creator(head.data, TheCorrectAnimal,
                     question_help)  # calls the function from above
Ejemplo n.º 4
0
def built_binary_tree():
    node = Node(1)
    ret_tree = BinaryTree(node)
    two_node = Node(2)
    three_node = Node(3)
    ret_tree.insert(ret_tree.root, two_node)
    ret_tree.insert(ret_tree.root, three_node)
    return ret_tree
Ejemplo n.º 5
0
def experimental_evaluation_data_bin_tree(repetitions):
    chart_data = []
    for i in range(repetitions):
        current_node_count = 100 + i * 10000
        current_tree = BinaryTree()
        for _ in range(current_node_count):
            num = random.randint(0, 160)
            current_tree.insert_node(current_tree.root, num)

        start_time = time.time()
        for _ in range(100 + i * 10000):
            current_tree.find(random.randint(0, 160))
        duration = time.time() - start_time

        chart_data.append(dict(size=current_node_count, time=duration))
    return chart_data
def minimal_tree(arr):
    med = len(arr) / 2

    left_arr = arr[:med]
    right_arr = arr[med + 1:]

    val = arr[med]

    B = BinaryTree(val)

    if len(left_arr) > 0:
        B.addLeft(minimal_tree(left_arr))

    if len(right_arr) > 0:
        B.addRight(minimal_tree(right_arr))

    return B
Ejemplo n.º 7
0
from Tree import BinaryTree, inorder, postorder, preorder
r = BinaryTree(1)
r.insertLeft(3)
r.insertRight(2)
r.insertLeft(5)
preorder(r)
Ejemplo n.º 8
0
    while len(queue) > 0:
        linked_lst = []
        for i in range(len(queue)):
            subTree = queue.pop()
            linked_lst.append(subTree.value)

            if subTree.left != None:
                queue.insert(0, subTree.left)
            if subTree.right != None:
                queue.insert(0, subTree.right)
        print linked_lst
        print "----"


B = BinaryTree(5)
lB = BinaryTree(4)
llB = BinaryTree(3)
rlB = BinaryTree(4.5)
rB = BinaryTree(7)
rrB = BinaryTree(9)
'''
                5
        4               7
    3       4.5             9
'''

lB.addLeft(llB)
lB.addRight(rlB)
rB.addRight(rrB)
B.addLeft(lB)
        return 0
    else:
        left_height = check_height_reduce(tree.getLeftChild())
        if left_height == -1:
            return -1
        right_height = check_height_reduce(tree.getRightChild())
        if right_height == -1:
            return -1

        height_diff = left_height - right_height
        if height_diff > 1:
            return -1
        else:
            return max([left_height, right_height]) + 1


if __name__=="__main__":
    r = BinaryTree('a')
    print "root:", (r.getRootVal())
    r.insertLeft('b')
    print "left child:", (r.getLeftChild().getRootVal())
    r.insertRight('c')
    print "right child:", (r.getRightChild().getRootVal())
    r.insertRight('d')
    print "right child:", (r.getRightChild().getRootVal())
    r.insertLeft('e')
    #print "right child:", (r.getRightChild().getRootVal())


    print "traverse_node", traverse_node(r)
Ejemplo n.º 10
0
def build_ten_tree():
    node = Node(7)
    ret_tree = BinaryTree(node)
    one_node = Node(1)
    two_node = Node(2)
    three_node = Node(3)
    four_node = Node(4)
    five_node = Node(5)
    six_node = Node(6)
    seven_node = Node(7)
    eight_node = Node(8)
    nine_node = Node(9)
    ten_node = Node(10)
    ret_tree.insert(ret_tree.root, two_node)
    ret_tree.insert(ret_tree.root, three_node)
    ret_tree.insert(ret_tree.root, nine_node)
    ret_tree.insert(ret_tree.root, six_node)
    ret_tree.insert(ret_tree.root, one_node)
    ret_tree.insert(ret_tree.root, ten_node)
    ret_tree.insert(ret_tree.root, eight_node)
    return ret_tree
            current = tree
            par = current.getParent()
            while par and par.getLeftChild() is not current:
                par = par.getParent()
            return par


def left_more(tree):
    if tree is None:
        return None
    else:
        while tree.getLeftChild():
            tree = tree.getLeftChild()
        return tree


if __name__=="__main__":
    r = BinaryTree(0)
    print "root:", (r.getRootVal())

    r.insertLeft(1)
    print "left child:", (r.getLeftChild().getRootVal())

    r.insertRight(3)
    print "right child:", (r.getRightChild().getRootVal())

    r.insertRight(5)
    print "right child:", (r.getRightChild().getRootVal())


    print find_next(r, 5)
Ejemplo n.º 12
0
from Tree import BinaryTree
from BPlusTree import BPlusTree
import plotly.express as px
import pandas
import time
import random

tree = BinaryTree()

for _ in range(16):
    num = random.randint(0, 150)
    tree.insert_node(tree.root, num)

# tree.insert_node(tree.root, 5)
# tree.insert_node(tree.root, 2)
# tree.insert_node(tree.root, 7)
# tree.insert_node(tree.root, 10)
# tree.print()
# tree.find(11)


def experimental_evaluation_data_bin_tree(repetitions):
    chart_data = []
    for i in range(repetitions):
        current_node_count = 100 + i * 10000
        current_tree = BinaryTree()
        for _ in range(current_node_count):
            num = random.randint(0, 160)
            current_tree.insert_node(current_tree.root, num)

        start_time = time.time()
Ejemplo n.º 13
0
    # @param root: TreeNode
    # @return int
    # return >= 0: Tree depth
    # return -1: Not balanced
    def getDepth(self, root):
        if root is None:
            return 0
        depth_l, depth_r = self.getDepth(root.left), self.getDepth(root.right)
        # If not balanced, return -1 to notice parent that one of or both of its subtree is not balanced.
        if abs(depth_l - depth_r) > 1 or depth_l < 0 or depth_r < 0:
            return -1
        # If valid, keep accumulate tree depth
        return max(depth_l, depth_r) + 1


# Main
if __name__ == '__main__':
    # nodes = [3, 9, 20, 'null', 'null', 15, 7]
    # nodes = [1, 2, 2, 3, 3, 'null', 'null', 4, 4]
    nodes = [
        1, 2, 2, 3, 3, 'null', 8, 4, 4, 'null', 'null', 'null', 'null', 'null',
        10
    ]
    root = BinaryTree().makeTree(nodes, 0)
    print(BinaryTree().levelOrder(root))

    # isBalancedDepth
    print(Solution().isBalanced(root))
    print(kamyu104().isBalanced(root))
Ejemplo n.º 14
0
#! /usr/bin/env python

from Tree import Node, BinaryTree

node = Node(7)
ret_tree = BinaryTree(node)
one_node = Node(1)
two_node = Node(2)
three_node = Node(3)
four_node = Node(4)
five_node = Node(5)
six_node = Node(6)
seven_node = Node(7)
eight_node = Node(8)
nine_node = Node(9)
ten_node = Node(10)
twelve_node = Node(12)
fourteen_node = Node(14)
eighteen_node = Node(18)

ret_tree.insert(ret_tree.root, two_node)
ret_tree.insert(ret_tree.root, three_node)
ret_tree.insert(ret_tree.root, nine_node)
ret_tree.insert(ret_tree.root, six_node)
ret_tree.insert(ret_tree.root, one_node)
ret_tree.insert(ret_tree.root, ten_node)
ret_tree.insert(ret_tree.root, eight_node)
#
# ten_tree = BinaryTree(ten_node)
# ten_tree.insert(ten_tree.root, five_node)
# ten_tree.insert(ten_tree.root, seven_node)
        if path[i-1]>path[i]:
            return False
    return True


def _valid_BST(root, path):
    if root is None:
        return None
    _valid_BST(root.getLeftChild(), path)
    path.append(root.getRootVal())
    _valid_BST(root.getRightChild(), path)
    return path




if __name__=="__main__":
    r = BinaryTree(20)
    print "root:", (r.getRootVal())

    r.insertLeft(10)
    print "left child:", (r.getLeftChild().getRootVal())

    r.insertRight(30)
    print "right child:", (r.getRightChild().getRootVal())

    r.getLeftChild().insertRight(25)
    print r.getLeftChild().getRightChild().getRootVal()

    print valid_BST(r)
			return root

		left = self.lowestCommonAncestor(root.leftChild, A, B)
		right = self.lowestCommonAncestor(root.rightChild, A, B)

		if left is not None and right is not None:
			return root
		if left is not None and right is None:
			return left
		if right is not None and left is None:
			return right
		return None


# Construct a binary tree to test
Node_3 = BinaryTree(3)
Node_3.insertLeft(5)
Node_3.insertRight(1)
Node_5 = Node_3.getLeftChild()
Node_1 = Node_3.getRightChild()

Node_5.insertLeft(6)
Node_6 = Node_5.getLeftChild()
Node_5.insertRight(2)
Node_2 = Node_5.getRightChild()

Node_2.insertLeft(7)
Node_7 = Node_2.getLeftChild()
Node_2.insertRight(4)
Node_4 = Node_2.getRightChild()
@email: [email protected]

"""
from Tree import BinaryTree

def check_identical(T1, T2):
    if T1 is None and T2 is None:
        return True
    if T1 is None and T2 is not None or T1 is not None and T2 is None:
        return False
    else:
        if T1.getRootVal() != T2.getRootVal():
            return False
        q1 = check_identical(T1.getLeftChild(), T2.getLeftChild())
        q2 = check_identical(T1.getRightChild(), T2.getRightChild())
        return q1 and q2


if __name__=="__main__":
    r1 = BinaryTree(0)
    r1.insertLeft(1)
    r1.insertRight(3)
    r1.insertRight(5)

    r2 = BinaryTree(0)
    r2.insertLeft(1)
    r2.insertRight(3)
    r2.insertRight(5)

    print check_identical(r1, r2)
Ejemplo n.º 18
0
def new_binary_tree():
    node = Node(1)
    return BinaryTree(node)
Ejemplo n.º 19
0
#! /usr/bin/env python

from Tree import Node, BinaryTree

tarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
mid = int(len(tarray)/2)
print("Mid: {}".format(mid))

rootnode = Node(tarray[mid])
print("rootnode: {}".format(rootnode))

bintree = BinaryTree(rootnode)
for j,item in enumerate(tarray):
    if item == tarray[mid]:
        continue
    in_node = Node(tarray[j])
    bintree.insert(bintree.root, in_node)

print("bintree in_order_traversal from root:")
bintree.in_order_traversal(bintree.root)
Ejemplo n.º 20
0
#creating a sample tree

from Tree import BinaryTree
root = BinaryTree(1)
root.addLeftChild(2)
root.addRightChild(3)
root.left.addLeftChild(4)
root.left.addRightChild(5)
root.left.left.addLeftChild(8)
root.left.left.addRightChild(9)
root.left.right.addLeftChild(10)
root.left.right.addRightChild(11)
root.right.addLeftChild(6)
root.right.addRightChild(7)
root.right.left.addLeftChild(12)
root.right.left.addRightChild(13)
root.right.right.addLeftChild(14)
root.right.right.addRightChild(15)

#               1
#
#         2                     3
#     4        5          6          7

#  8    9   10   11    12   13   14     15