def test_given1(self):
     """
     Tests the longest path start from root
     """
     test_tree = Node(1, [Node(2, [Node(4)]),
                          Node(3)])
     self.assertEqual(longest_path_in_tree(test_tree), 2,
         'Should be 2')
Ejemplo n.º 2
0
def build_dictionary_trie(vocabulary):
    vocabTrie = Node('Root', False)
    for word in vocabulary:
        currNode = vocabTrie
        for i, letter in enumerate(word):
            isTerminate = i == (len(word) - 1)
            # To match the cases, all vocabulary letters are lower cased
            child = Node(letter.lower(), isTerminate)
            currNode = currNode.add_child(child)
    return vocabTrie
Ejemplo n.º 3
0
def buildDictTrie(grid, dictionary, maxLen):
    """
    Create a trie using the words in dictionary, each node is a 
    letter, each branch with a termination mark is a word
    """
    root = Node(None, False)
    letters = set([letter for row in grid for letter in row])
    for word in dictionary:
        if (len(word) <= maxLen) and (len(set(word) - letters) == 0):
            currNode = root
            for i, letter in enumerate(word):
                # isTerminate defines if a word stops here
                isTerminate = i == (len(word) - 1)
                newNode = Node(letter, isTerminate)
                nextNode = currNode.add_child(newNode)
                currNode = nextNode
    return root
 def test_given2(self):
     """
     Tests the longest path ends in leaf
     """
     test_tree = Node(5, [Node(6),
                          Node(7, [Node(8, [Node(9, [Node(15),
                                                     Node(10)])]),
                                   Node(12)])])
     self.assertEqual(longest_path_in_tree(test_tree), 4,
         'Should be 4')
Ejemplo n.º 5
0
from mytree import Node

#recursively flip tree
def invertTree(node):
	if node.left != None:
		invertTree(node.left)
	if node.right != None:
		invertTree(node.right)

	if node.right!= None:
		temp = node.right
		node.right = node.left
		node.left = temp
	return 


dude = Node(8)
dude.insert(6)
dude.insert(9)
dude.insert(2)
dude.insert(7)
dude.insert(2)
dude.insert(90)
dude.prettyPrintTree()
invertTree(dude)
dude.prettyPrintTree()



 def test_middle(self):
     """
     Tests the longest path neither start from root nor end in leaf.
     """
     test_tree = Node(50, [Node(2, [Node(8),
                                   Node(11),
                                   Node(13)]),
                          Node(10, [Node(3, [Node(4, [Node(7),
                                                      Node(12)]),
                                             Node(20)]),
                                   Node(14)])])
     self.assertEqual(longest_path_in_tree(test_tree), 2,
         'Should be 2')    
 def test_complex(self):
     """
     Tests a complex tree with some parents have three children.
     """
     test_tree = Node(1, [Node(2, [Node(8),
                                   Node(11),
                                   Node(13, [Node(14, [Node(15)])])]),
                          Node(10, [Node(3, [Node(4, [Node(5),
                                                      Node(7),
                                                      Node(12)]),
                                             Node(100)]),
                                   Node(12)])])
     self.assertEqual(longest_path_in_tree(test_tree), 3,
         'Should be 3')
Ejemplo n.º 8
0
from mytree import Node


#recursively flip tree
def invertTree(node):
    if node.left != None:
        invertTree(node.left)
    if node.right != None:
        invertTree(node.right)

    if node.right != None:
        temp = node.right
        node.right = node.left
        node.left = temp
    return


dude = Node(8)
dude.insert(6)
dude.insert(9)
dude.insert(2)
dude.insert(7)
dude.insert(2)
dude.insert(90)
dude.prettyPrintTree()
invertTree(dude)
dude.prettyPrintTree()
Ejemplo n.º 9
0
from mytree import Node
#test code			
root = Node(10)
root.insert(5)
root.insert(4)
root.insert(9)
root.insert(10)
root.insert(11)
root.prettyPrintTree()