def operate_tn(op, l, r): if op == ":": #':' operator adds the length of branch (and create node if no node exists) if isinstance(l, TreeNode): l.length = float(r) return l else: nod = TreeNode(name=[l], length=float( r)) #added [] 18/10/13...to give all tip names as node.name return nod elif op == ",": #',' operator creat a subtree from 2 nodes if not isinstance(l, TreeNode): l = TreeNode(name=[ l ]) #added [] 18/10/13...to give all tip names as node.name if not isinstance(r, TreeNode): r = TreeNode(name=[ r ]) #added [] 18/10/13...to give all tip names as node.name nod = TreeNode( name=l.name + r.name ) #added [] 18/10/13...to give all tip names as node.name. need refactoring. l.parent = nod r.parent = nod nod.left, nod.right = l, r return nod
def put(self, key, val): if not self.root: self.root = TreeNode(key, val) else: currentNode = self._findSpot(key) new = TreeNode(key, val) new.parent = currentNode if key < currentNode.key: currentNode.leftChild = new elif key > currentNode.key: currentNode.rightChild = new else: currentNode.replaceNodeData(key, val, currentNode.leftChild, currentNode.rightChild) self.size += 1
def build_tree(self, unparsed_xml, root): # gets <tag>, <tag "data"/>, or </tag> tag = re.search(r'(\<)(\/)?([A-Za-z]+)((.*)\/?)(\>)?', unparsed_xml) if (tag): tag = tag.group() new_node = TreeNode() # no children, get attrs then process rest of siblings if tag.endswith('/>'): self.get_node_attrs(tag, new_node, root) root.children.append(new_node) unparsed_siblings = re.search(r'(?<=%s)(<?.*)' % tag, unparsed_xml, re.DOTALL).group() if (unparsed_siblings != ""): self.build_tree(unparsed_siblings, root) # has children, get attr and recursively get children elif tag.endswith('>'): close_tag = self.get_close_tag(tag) self.get_node_attrs(tag, new_node, root) num_close_tags = self.get_num_close_tags(tag, unparsed_xml) close_tag_start_index = [ m.start() for m in re.finditer(close_tag, unparsed_xml) ][num_close_tags - 1] close_tag_end_index = [ m.end() for m in re.finditer(close_tag, unparsed_xml) ][num_close_tags - 1] children = re.search(r'(?<={})(.+)'.format(tag), unparsed_xml[:close_tag_start_index], re.DOTALL).group() unparsed_siblings = unparsed_xml[close_tag_end_index:] if self.get_tag_name(tag) == 'root': self.root = root self.root.behavior = 'ROOT' self.build_tree(children, root) else: new_node.parent = root root.children.append(new_node) self.build_tree(children, new_node) if unparsed_siblings != "": self.build_tree(unparsed_siblings, root)
childside = root.left if a_onleft else root.right return ancestor_helper(childside, a, b) def covers(root, b): if root == None: return False if root == b: return True return covers(root.left, b) or covers(root.right, b) if __name__ == "__main__": n1 = TreeNode(1) n2 = TreeNode(2) n3 = TreeNode(3) n4 = TreeNode(4) n5 = TreeNode(5) n6 = TreeNode(6) ## n2.left = n1 n2.right = n3 n1.parent = n2 n3.parent = n2 ## n5.left = n4 n5.right = n6 n4.parent = n5 n6.parent = n5 ## print(common_ancestor(n2, n1, n4)) print(common_ancestor(n2, n1, n3).value)
return get_leftmost_child(n.right) else: q = n x = n.parent while x and x.left != q: q = x x = x.parent return x def get_leftmost_child(n): while n.left != None: n = n.left return n if __name__ == "__main__": n0 = TreeNode(0) n1 = TreeNode(1) n2 = TreeNode(2) n3 = TreeNode(3) n_1 = TreeNode(-1) n2.left = n1 n1.parent = n2 n2.right = n3 n3.parent = n2 n0.right = n2 n2.parent = n0 n0.left = n_1 n_1.parent = n0 print(inorder_succ(n2).value)