def parse_tree(cyk_matrix, current_symbol, i, j, errors, nonterminals): """Takes a Matrix, a symbol, a start location, an end location, the best error distance for the string, and a list of nonterminals and returns a parse tree for the individual characters in the string. This can be used to find I'. """ if i == j - 1: tups = cyk_matrix.get(i, j) if current_symbol in tups: tup = tups[current_symbol] if tup[1] == errors: return Node(i, j, tup[2]) raise LookupError('Could not find {} in cyk_matrix at {}'.format( current_symbol, (i, j))) A, B, q_1, q_2, dab, k = [None] * 6 try: for k in range(i + 1, j): for rhs, dab in nonterminals[current_symbol].items(): A, B = rhs.split() if A in cyk_matrix.get(i, k) and B in cyk_matrix.get(k, j): q_1 = cyk_matrix.get(i, k)[A][1] q_2 = cyk_matrix.get(k, j)[B][1] if dab.errors + q_1 + q_2 == errors: raise BreakIt raise LookupError(('Could not find match for right hand side of any ' 'production of {} in cyk_matrix at {}').format( current_symbol, (i, j))) except BreakIt: pass left = parse_tree(cyk_matrix, A, i, k, q_1, nonterminals) right = parse_tree(cyk_matrix, B, k, j, q_2, nonterminals) root = Node(i, j, dab) root.left = left root.right = right return root
def create_graph(self): """Creates a Huffman graph from self.heap""" while(len(self.heap.l) > 1): node1 = self.heap.pop() node2 = self.heap.pop() merged = Node(None, node1.freq + node2.freq) merged.left = node1 merged.right = node2 self.heap.push(merged)
def MakeNewElem(list): tempNode = Node("cvor", list[0].freq + list[1].freq) list[0].parent = tempNode list[1].parent = tempNode tempNode.left = list[0] tempNode.right = list[1] list.pop(0) list.pop(0) list.append(tempNode) list.sort()
def test(): root = Node() root.right = Node() root.right.right = Node() root.right.right.right = Node() root.right.right.right.right = Node() root.right.left = Node() root.left = Node() root.left.right = Node() ibt = IsBalancedTree() print ibt.is_balanced(root)
def test(): root = Node(val=1) root.right = Node(val=2) root.right.right = Node(val=3) node1 = Node(val=4) root.right.right.right = node1 root.right.right.right.right = Node(val=5) node2 = Node(val=6) root.right.left = node2 root.left = Node(val=7) node3 = Node(8) root.left.right = node3 cm = CommonAncestor() print cm.find_anc(root, node1, node3).val
def parse_tree(cyk_matrix, current_symbol, i, j, errors, nonterminals): """Takes a Matrix, a symbol, a start location, an end location, the best error distance for the string, and a list of nonterminals and returns a parse tree for the individual characters in the string. This can be used to find I'. """ if i == j - 1: tups = cyk_matrix.get(i, j) if current_symbol in tups: tup = tups[current_symbol] if tup[1] == errors: return Node(i, j, tup[2]) raise LookupError('Could not find {} in cyk_matrix at {}'.format( current_symbol, (i, j))) A, B, q_1, q_2, dab, k = [None] * 6 try: for k in range(i+1, j): for rhs, dab in nonterminals[current_symbol].items(): A, B = rhs.split() if A in cyk_matrix.get(i, k) and B in cyk_matrix.get(k, j): q_1 = cyk_matrix.get(i, k)[A][1] q_2 = cyk_matrix.get(k, j)[B][1] if dab.errors + q_1 + q_2 == errors: raise BreakIt raise LookupError(( 'Could not find match for right hand side of any ' 'production of {} in cyk_matrix at {}').format( current_symbol, (i, j))) except BreakIt: pass left = parse_tree(cyk_matrix, A, i, k, q_1, nonterminals) right = parse_tree(cyk_matrix, B, k, j, q_2, nonterminals) root = Node(i, j, dab) root.left = left root.right = right return root