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
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
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
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
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
#! /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)
from Tree import BinaryTree, inorder, postorder, preorder r = BinaryTree(1) r.insertLeft(3) r.insertRight(2) r.insertLeft(5) preorder(r)
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)
def new_binary_tree(): node = Node(1) return BinaryTree(node)
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()
# @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))
#! /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)
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()
#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