class BinaryTreeSearch: def __init__(self, fileName): self.tree = BinaryTree() with open(fileName) as f: flag = False lastWord = '' preLastWord = '' oneWordLineWord = '' for line in f: words = re.split(r'\W+|\d+|_+', line) words = list(filter(bool, words)) if len(words) == 0: continue if oneWordLineWord != '': words.insert(0, oneWordLineWord) oneWordLineWord = '' if len(words) == 1: oneWordLineWord = words[0].lower() continue if flag: #pre last first node = self.tree.find( NodeWithData.makeKey(preLastWord, lastWord)) if node == None: node = NodeWithData(preLastWord, lastWord) self.tree.add(node) node.UpdateDict(preLastWord, lastWord, words[0].lower()) #last first second node = self.tree.find( NodeWithData.makeKey(lastWord, words[0].lower())) if node == None: node = NodeWithData(lastWord, words[0].lower()) self.tree.add(node) node.UpdateDict(lastWord, words[0].lower(), words[1].lower()) else: flag = True lastWord = words[-1].lower() preLastWord = words[-2].lower() for i in range(len(words) - 2): w1 = words[i].lower() w2 = words[i + 1].lower() node = self.tree.find(NodeWithData.makeKey(w1, w2)) if node == None: node = NodeWithData(w1, w2) self.tree.add(node) node.UpdateDict(w1, w2, words[i + 2].lower()) if oneWordLineWord + preLastWord + lastWord != '': node = self.tree.find( NodeWithData.makeKey(preLastWord, lastWord)) if node == None: node = NodeWithData(preLastWord, lastWord) self.tree.add(node) node.UpdateDict(preLastWord, lastWord, oneWordLineWord) def search(self, first, second): first, second = first.lower(), second.lower() key = NodeWithData.makeKey(first, second) node = self.tree.find(key) if node == None: return [] return node.GetTrigrams(first, second)
def test_features(self): bt = BinaryTree(10) bt.add(6) bt.add(18) bt.add(4) bt.add(8) bt.add(15) bt.add(21) self.assertEqual(bt.count(), 7) self.assertEqual(bt.min(), 4) self.assertEqual(bt.max(), 21) self.assertEqual(bt.has(8), True) self.assertEqual(bt.has(11), False) self.assertNotEqual(bt.find(15), None) self.assertEqual(bt.find(11), None) bt.delete(10) self.assertEqual(bt.root.data, 15) self.assertEqual(bt.find(10), None) self.assertEqual(bt.count(), 6) self.assertEqual([x for x in bt.items()], [4, 6, 8, 15, 18, 21]) self.assertEqual([x for x in bt.items('pre')], [15, 6, 4, 8, 18, 21]) self.assertEqual([x for x in bt.items('pos')], [4, 8, 6, 21, 18, 15]) self.assertEqual([x for x in bt.items('lev')], [15, 6, 18, 4, 8, 21])
return ret if __name__ == '__main__': # Ugly but I'm building a binary tree here with depth 5 # Letters and tree come from image in the epi book k = Node(1) k.left = Node(401) k.left.right = Node(641) k.right = Node(257) j = Node(2, right=k) i = Node(6, left=j) i.right = Node(271) i.right.right = Node(28) c = Node(271) c.left = Node(28) c.right = Node(0) f = Node(561) f.right = Node(3) f.right.left = Node(17) b = Node(6, left=c, right=f) root = Node(314, left=b, right=i) tree = BinaryTree(root) t = compute(tree) print(t) tree.add(971) t = compute(tree) print(t)
from binarytree import BinaryTree from node import Node tree = BinaryTree(Node(6)) nodes = [5, 3, 9, 7, 8, 7.5, 12, 11] [tree.add(Node(n)) for n in nodes] tree.delete(9) tree.inorder()
def check(root): if not root: return 0 L = check(root.left) if L == 2: return 2 R = check(root.right) if R == 2: return 2 found = root is p or root is q M = 1 if found else 0 if M + L + R == 2: cls.lca_node = root return M + L + R check(root) return cls.lca_node.data if __name__ == '__main__': bt = BinaryTree() for i in range(1, 16): bt.add(i) p = bt.root.left.left.right q = bt.root.left.right.right print(LCA.compute(bt.root, p, q))
All of the nodes' values will be unique. p and q are different and both values will exist in the BST. ''' from binarytree import BinaryTree, Node def LCA(input: BinaryTree, p: Node, q: Node): lca = input.root while q.parent != None and p.parent != None: if p == q.parent and p.value < lca.value: lca = p elif q == p.parent and q.value < lca.value: lca = q elif p.parent == q.parent and p.parent.value < lca.value: lca = p.parent else: p = p.parent q = q.parent return lca input1 = [6, 2, 8, 0, 4, 7, 9, 3, 5] tree = BinaryTree() for item in input1: tree.add(item) print(LCA(tree, tree.find_last(2), tree.find_last(8))) print(LCA(tree, tree.find_last(2), tree.find_last(4)))
if not L and not R: continue if L and R: if L.data != R.data: return False dq.append(L.left) dq.append(R.right) dq.append(L.right) dq.append(R.left) else: return False return True if __name__ == '__main__': left = Node(6) left.right = Node(2) left.right.right = Node(3) right = Node(6) right.left = Node(2) right.left.left = Node(3) root = Node(314, left, right) print(symmetric(root)) nodes = [1, 2, 2, 4, 3, 3, 4] tree = BinaryTree() for node in nodes: tree.add(node) print(sym_iterative(tree.root))
q = [] q.append(root) h = 0 while h < height: u = q.pop(0) if u.left != None: q.append(u.right) q.append(u.left) if u.value > root.value and u.value < second_min: second_min = u.value h += 1 if second_min > root.value and second_min != math.inf: return second_min else: return -1 tree1 = BinaryTree() tree1.add(2) tree1.add(2) tree1.add(5) tree1.add(5) tree1.add(7) tree2 = BinaryTree() tree2.add(2) tree2.add(2) tree2.add(2) print(second_min(tree1)) print(second_min(tree2))