예제 #1
0
 def _insertKey(self, node, ele):
     if node.data <= ele:
         if node.right is None:
             node.right = BinaryTreeNode()
             node.right.data = ele
         else:
             return self._insertKey(node.right, ele)
     else:
         if node.left is None:
             node.left = BinaryTreeNode()
             node.left.data = ele
         else:
             return self._insertKey(node.left, ele)
예제 #2
0
 def __init__(self, name=None):
     """
     Constructs a 0 card hand
     """
     self.name = name
     self.hands: BinaryTree = BinaryTree(BinaryTreeNode(0))  # The root of the BinaryTree represents an "empty" hand
     self.cards: [Card] = deque()  # each individual card in the hand
     self.possible_scores = deque()  # can be multiple different scores due to aces double value possibilities
     self.has_non_bust = True  # if at least possible scoring is <= 21
예제 #3
0
def aux_create_bst(arr,l,r):
    if l > r:
        return None
    m = l + (r-l)/2
    
    root = BinaryTreeNode(arr[m])
    root.left = aux_create_bst(arr,l,m-1)
    
    root.right = aux_create_bst(arr,m+1,r)
    return root
예제 #4
0
 def insert(self, val):
     tempNode = root
     while tempNode:
         # Go to or create the right child
         if(val > tempNode.val):
             if(tempNode.right):
                 tempNode = tempNode.right
             else:
                 tempNode.right = BinaryTreeNode(val)
                 break
         # Go to or create the left child
         elif(val < tempNode.val):
             if(tempNode.left):
                 tempNode = tempNode.left
             else:
                 tempNode.left = BinaryTreeNode(val)
                 break
         # Value already in search tree so we quit
         else:
             break    
예제 #5
0
    def add_card(self, card: Card):
        """
        Adds the given card to the current hand and recalculates the possible scores
        given this new addition.

        :param card: a Card from a standard deck (e.g. ace)
        """
        self.cards.append(card)  # maintain the card for future reference
        self.possible_scores.clear()  # clear past scores
        leaves = self.hands.get_leaves() # get the most recent scores at play
        has_non_bust = False
        for leaf in leaves:
            for i, val in enumerate(card.get_values()):  # go through each possible value of this card
                possible_score = leaf.val + val
                # each branch of the BinaryTree is a possible value of the current hand
                if i == 0:
                    leaf.left = BinaryTreeNode(possible_score)
                else:
                    leaf.right = BinaryTreeNode(possible_score)
                has_non_bust = has_non_bust or possible_score <= 21  # bust is greater than 21
                self.possible_scores.append(possible_score)
            self.has_non_bust = has_non_bust
예제 #6
0
def binary_search_tree_runner():
    
    binary_obj = BinaryTreeNode()
    try:
        nodes_count = int(input("Enetr how many nodes to insert into list:"))
    
    except Exception as e:
        print(e)
        print("enter number of nodes in integer only")
    nodes_list = []
    print('now enter all test cases')
    for i in range(0, nodes_count):
        nodes_list.append(int(input("enter:")))

    result = binary_obj.count_binary_search_tree(nodes_list)
    print("No of binary tree possiable in each test case is as follow")
    for i in result:
        print(i)
예제 #7
0
def generateAllBinaryTrees(numNodes):
    result = list()

    if numNodes == 0:
        result.append('END')

    for numLeftTreeNodes in range(numNodes):
        numRightTreeNodes = numNodes - 1 - numLeftTreeNodes
        leftSubtrees = generateAllBinaryTrees(numLeftTreeNodes)
        rightSubtrees = generateAllBinaryTrees(numRightTreeNodes)

        # generate all combinations of left subtrees and right subtrees
        for left in leftSubtrees:
            for right in rightSubtrees:
                result.append(BinaryTreeNode(0, left, right))
                #print result[0].level_order()

    return result
def build_tree_helper(preorder, inorder, in_start, in_end):

    if (in_start > in_end):
        return None

    # Pich current node from Preorder traversal using
    # preIndex and increment preIndex
    tNode = BinaryTreeNode(preorder[build_tree.preindex])
    build_tree.preindex += 1

    # If this node has no children then return
    if in_start == in_end:
        return tNode

    # Else find the index of this node in Inorder traversal
    inIndex = search(inorder, in_start, in_end, tNode.val)

    # Using index in Inorder Traversal, construct left
    # and right subtrees
    tNode.left = build_tree_helper(preorder, inorder, in_start, inIndex - 1)
    tNode.right = build_tree_helper(preorder, inorder, inIndex + 1, in_end)

    return tNode
    """
예제 #9
0
        return root
    
    if is_ls:
        lca = lca_bst(root.left,node1,node2)
        if lca != None:
            return lca
    if is_rs:
        lca = lca_bst(root.right,node1,node2)
        if lca != None:
            return lca

if __name__=='__main__':    
    arr = [4,3,2,5,1,6,7]
    root = create_bst(arr)
    bst = BST(root)
    
    print bst.root.level_order()

    search_el = 6
    print '\nBST contains:'

    print bst.contains(10)

    print t2Sum(bst.root,21)


    
    lca = lca_bst(root,BinaryTreeNode(5),BinaryTreeNode(1))    
    print 'lca :',lca.val    
        
예제 #10
0
 def insertKey(self, ele):
     if self.root is None:
         self.root = BinaryTreeNode()
         self.root.data = ele
     else:
         return self._insertKey(self.root, ele)
예제 #11
0
def find_columns_helper(root, dist_nodes, cur_dist):
    if root == None:
        return

    ## preorder traversal
    if cur_dist in dist_nodes:
        dist_nodes[cur_dist].append(root.val)
    else:
        dist_nodes[cur_dist] = [root.val]

    find_columns_helper(root.left, dist_nodes, cur_dist - 1)
    find_columns_helper(root.right, dist_nodes, cur_dist + 1)


root = BinaryTreeNode(6)
node1 = BinaryTreeNode(3)
node2 = BinaryTreeNode(5)
node3 = BinaryTreeNode(9)

node2.right = node3
node1.left = node2
root.left = node1

node4 = BinaryTreeNode(2)
node5 = BinaryTreeNode(7)
node6 = BinaryTreeNode(4)

node4.left = node5
node4.right = node6
root.right = node4
예제 #12
0
파일: 9.07.py 프로젝트: pwang724/algorithms
    q.enqueue(first_node)

    out = []
    while (q.size()):
        l = []
        temp = Queue_List.Queue()
        while (q.size()):
            node = q.dequeue()
            l.append(node.data)

            if node.left:
                temp.enqueue(node.left)
            if node.right:
                temp.enqueue(node.right)
        out.append(l)
        q = temp
    return out


if __name__ == '__main__':
    z = BinaryTreeNode(28, None, None)
    e = BinaryTreeNode(271, z, None)
    f = BinaryTreeNode(561, None, None)
    g = BinaryTreeNode(2, None, None)

    b = BinaryTreeNode(5, e, f)
    c = BinaryTreeNode(6, g, None)
    a = BinaryTreeNode(314, b, c)

    print(breadth_first_search(a))
예제 #13
0
from BinaryTree import BinaryTreeNode

Root = BinaryTreeNode(5)

LeftNode = BinaryTreeNode(3)
Root.Left = LeftNode

RightNode = BinaryTreeNode(7)
Root.Right = RightNode

print(Root.Value)
print(Root.Left.Value)
print(Root.Right.Value)