def insert(self, key, data=None): """ Insert or update data for given key. Returns True for insert (key is new) and False for update (key already present). """ # TODO quick and dirty implementation if self.root is None: self.root = Node(key, data, tree=self) return True p = self.root parent = None isLeftChild = False while p is not None: if key == p.key: p.data = data return False elif key < p.key: parent = p p = p.left isLeftChild = True elif key > p.key: parent = p p = p.right isLeftChild = False p = Node(key, data, parent) if isLeftChild: parent.left = p else: parent.right = p return True
def main(): tree = Node("a") tree.left = Node("b") tree.right = Node("c") assert is_symmetric(tree) tree.left.left = Node("x") assert not is_symmetric(tree)
def main(): tree1 = c_balanced(4, "x") tree2 = Node("x") tree2.left = Node("x") tree2.right = Node("x") tree2.right.right = Node("x") assert tree1 == tree2
def c_balanced(c, value): if c == 0: return None root = Node(value) left_size = (c - 1) // 2 right_size = (c - 1) - left_size root.left = c_balanced(left_size, value) root.right = c_balanced(right_size, value) return root
def solution(): root = Node(30) root.left = Node(20) root.left.left = Node(10) assert is_balanced(root) == -1 root.right = Node(40) root.left.right = Node(25) root.left.left.left = Node(0) assert is_balanced(root) == -1 root.right.left = Node(35) root.right.right = Node(45) assert is_balanced(root) == 4
def solution2(): root = Node(30) root.left = Node(20) root.left.left = Node(50) assert is_bst2(root, [-sys.maxint-1]) == False root.left.left = Node(10) assert is_bst2(root, [-sys.maxint-1]) == True root.right = Node(40) root.right.right = Node(0) root.left.right = Node(25) root.left.left.left = Node(0) assert is_bst2(root, [-sys.maxint-1]) == False root.right.right = Node(45) root.right.left = Node(35) assert is_bst2(root, [-sys.maxint-1]) == True
def main(): tree1 = add_value(2) tree2 = Node(2) assert tree1 == tree2 add_value(3, tree1) tree2.right = Node(3) assert tree1 == tree2 add_value(0, tree1) tree2.left = Node(0) assert tree1 == tree2 tree1 = from_list([3, 2, 5, 7, 1]) tree2 = Node(3) tree2.left = Node(2) tree2.right = Node(5) tree2.right.right = Node(7) tree2.left.left = Node(1) assert tree1 == tree2 assert is_symmetric(from_list([5, 3, 18, 1, 4, 12, 21])) assert not is_symmetric(from_list([3, 2, 5, 7, 4]))
class NaiveBST(BinaryTree): """ An unbalanced Binary Search Tree Implementation. No augumented data. """ def __init__(self): super().__init__() self.root = None def _search(self, key): p = self.root while p is not None: if p.key == key: return p.data elif p.key < key: p = p.right else: p = p.left raise KeyError("Key {} not found".format(key)) return p def search(self, key): p = self._search(key) return p.data def search_functional(self, key): def accessAlgorithm(searchTarget): # the access algorithms choice for the next operation depends on # - the key to search (global information) # - the current key (local) # - augumenting attributes of the node # availible Operations # - move to parent/left/right # - rotate # and write augumenting information on entering (exiting?) node def moveLeft(p): return p.left def moveRight(p): return p.right def moveUp(p): return p.parent def rotate(p): p.rotate() return p def alg(p): # you can read p.* but not p.*.* # you can also write p.info etc. but p.key is fixed and # the pointers can only be modified with rotations # you must execute one of the above operations or return if p is None: raise KeyError("Key {} not found".format(searchTarget)) elif p.key == searchTarget: return p elif p.key < searchTarget: p = moveRight(p) return alg(p) elif p.key > searchTarget: p = moveLeft(p) return alg(p) return alg alg = accessAlgorithm(key) p = self.root # pointer is always initialized to root node p = alg(p) # run algorithm return p.data def insert(self, key, data=None): """ Insert or update data for given key. Returns True for insert (key is new) and False for update (key already present). """ # TODO quick and dirty implementation if self.root is None: self.root = Node(key, data, tree=self) return True p = self.root parent = None isLeftChild = False while p is not None: if key == p.key: p.data = data return False elif key < p.key: parent = p p = p.left isLeftChild = True elif key > p.key: parent = p p = p.right isLeftChild = False p = Node(key, data, parent) if isLeftChild: parent.left = p else: parent.right = p return True def delete(self, key): pass def __repr__(self): return self.root.__repr__() def preorder(self): return self.root.preorder()
def run(h): # aaa bra = Operator("bra", "c b a k j i") ket = Operator("ket", "id jd kd ad bd cd") #bra = Operator("bra", "b a j i") #bra = Operator("bra", "j b i a") #bra = Operator("bra", "a3 a2 a1 i3 i2 i1") #ket = Operator("ket", "i1d i2d i6d a1d a5d a6d") #bra = Operator("bra", "a3 a2 a1 i3 i2 i1") #ket = Operator("ket", "i1d i2d i6d a1d a5d a6d") #ket = Operator("ket", "i1d i2d i3d a1d a2d a6d") # aab #bra = Operator("bra", "cb b a kb j i") #ket = Operator("ket", "id jd kbd ad bd cbd") # abb #bra = Operator("bra", "cb bb a kb jb i") #ket = Operator("ket", "id jbd kbd ad bbd cbd") # bbb #bra = Operator("bra", "cb bb ab kb jb ib") #ket = Operator("ket", "ibd jbd kbd abd bbd cbd") #bra = Operator("bra", "cb b a kb j i") #ket = Operator("ket", "id jd kbd ad bd cbd") for t in ts: fs = OperatorString(bra * h * t * ket) root_node = Node(fs) wicks(root_node) # Pretty Print #lines, _, _, _ = get_utf8_tree(root_node) #for line in lines: # print(line) #print(root_node.right.right.right.right.left.data) #print(fs) #root_node.print() full = collect_fully_contracted(root_node) #print(full) #sys.exit() new_eqs = [] new_weights = {} if len(full) == 0: continue for i, eq in enumerate(full): evs = [x.evaluate() for x in eq.deltas] if 0 in evs: continue mv = h.eval_deltas(eq.deltas) mt = t.eval_deltas(eq.deltas) #equiv = new_eqs_weights[key] #print(equiv) #print(eq.deltas) #print(eq.sign * eq.weight, mv) new_eqs.append((eq.sign * eq.weight, mv, mt)) new_weights[(mv, mt)] = eq.sign * eq.weight terms = list(zip(*new_eqs)) #print(terms) if len(terms) > 0: uniques = find_equiv(list(terms[1:])) print("==================================") print(h) print('----------------') for cnt, term in uniques: print(int(math.sqrt(cnt)) * new_weights[term], term) print("==================================\n")
def run(h, bra, ket, python_out): # aaa #bra = Operator("bra", "cb b a kb j i") #ket = Operator("ket", "id jd kbd ad bd cbd") # aab #bra = Operator("bra", "cb b a kb j i") #ket = Operator("ket", "id jd kbd ad bd cbd") # abb #bra = Operator("bra", "cb bb a kb jb i") #ket = Operator("ket", "id jbd kbd ad bbd cbd") # bbb #bra = Operator("bra", "cb bb ab kb jb ib") #ket = Operator("ket", "ibd jbd kbd abd bbd cbd") #bra = Operator("bra", "cb b a kb j i") #ket = Operator("ket", "id jd kbd ad bd cbd") fs = OperatorString(bra * h * ket) root_node = Node(fs) wicks(root_node) # Pretty Print #lines, _, _, _ = get_utf8_tree(root_node) #for line in lines: # print(line) #print(root_node.right.right.right.right.left.data) full = collect_fully_contracted(root_node) #print(full) #sys.exit() new_eqs = [] new_weights = {} if len(full) == 0: return for i, eq in enumerate(full): evs = [x.evaluate() for x in eq.deltas] if 0 in evs: continue mv = h.eval_deltas(eq.deltas) #equiv = new_eqs_weights[key] #print(equiv) #print(eq.deltas) #print(eq.sign * eq.weight, mv) new_eqs.append((eq.sign * eq.weight, mv)) new_weights[mv] = eq.sign * eq.weight terms = list(zip(*new_eqs)) if len(terms) > 0: uniques = find_equiv(list(terms[1])) print("==================================") print(h) print('----------------') for cnt, term in uniques: print(int(math.sqrt(cnt)) * new_weights[term], term) w = int(math.sqrt(cnt)) * new_weights[term] if w < 0: str_out = "-" + term.to_python()[1] else: str_out = term.to_python()[1] python_out.append(str_out) print("==================================\n")
""" from collections import defaultdict, deque from bintree import Node def vertical_order(root): queue = deque() queue.appendleft((root, 0)) distances = defaultdict(list) while queue: node, hd = queue.pop() distances[hd].append(node.key) if node.left: queue.appendleft((node.left, hd-1)) if node.right: queue.appendleft((node.right, hd+1)) for hd in sorted(distances): print ", ".join([str(k) for k in distances[hd]]) if __name__ == "__main__": root = Node(12) root.left = Node(10) root.right = Node(20) root.right.left = Node(25) root.right.right = Node(40) root.right.left.right = Node(3) print "Vertical ordering" vertical_order(root)
def solution2(): root = Node(30) root.left = Node(20) root.left.left = Node(50) assert is_bst2(root, [-sys.maxint - 1]) == False root.left.left = Node(10) assert is_bst2(root, [-sys.maxint - 1]) == True root.right = Node(40) root.right.right = Node(0) root.left.right = Node(25) root.left.left.left = Node(0) assert is_bst2(root, [-sys.maxint - 1]) == False root.right.right = Node(45) root.right.left = Node(35) assert is_bst2(root, [-sys.maxint - 1]) == True
if __name__ == "__main__": ops = gen_fermi_onebody() #ops = gen_fermi_twobody() #ops = gen_fermi_threebody() bra = Operator("bra", "c b a k j i") ket = Operator("ket", "id jd kd ad bd dd") #sys.exit() for op in ops: print('--------', op, '-----------') fs = OperatorString(bra.string + op.string + ket.string) root_node = Node(fs) wicks(root_node) full = collect_fully_contracted(root_node) for eq in full: evs = [kd.evaluate() for kd in eq[1]] if 0 in evs: continue m = op.eval_deltas(eq[1]) print(eq[0], m) print('--------------------------------')