def subtree(s, tree): """ Returns whether the given post order traversal index sequence represents a subtree or not :type s: list :type tree: Node :return: result :rtype: bool """ first_node_index = s[0] last_node_index = s[-1] # Neglecting explicitly the '_' characters if '_' in s: return False # Checking whether the first node is a leaf is_non_leaf = Node.has_children(tree, first_node_index) if is_non_leaf == -1: return False # Checking the left most descendant of the root of the subtree, is equal to the first node lmd = Node.lmd_subtree(tree, last_node_index) if not lmd: return False else: return not is_non_leaf and lmd == first_node_index
def appendNode(self, data): if self.left == None: self.left = Node(data) return True elif self.right == None: self.right = Node(data) return True elif self.left.appendNode(data): return True elif self.right.appendNode(data): return True
class Node(simple_tree.Node): def appendNode(self, data): if self.left == None: self.left = Node(data) return True elif self.right == None: self.right = Node(data) return True elif self.left.appendNode(data): return True elif self.right.appendNode(data): return True
from simple_tree import Node import compare A = ( Node("a") .addkid(Node("b") .addkid(Node("c") .addkid(Node("d") .addkid(Node("m1")) .addkid(Node("m2"))))) .addkid(Node("e") .addkid(Node("x") .addkid(Node("m3"))) .addkid(Node("y") .addkid(Node("m4")))) ) B1 = ( Node("a") .addkid(Node("b") .addkid(Node("d") .addkid(Node("m3")) .addkid(Node("m4")))) .addkid(Node("e1") .addkid(Node("x1") .addkid(Node("m1"))) .addkid(Node("y1") .addkid(Node("m2")))) )
def test(): t1 = Node("a").addkid(Node('b').addkid(Node('e')).addkid( Node('f'))).addkid(Node('c').addkid(Node('g'))).addkid(Node('d')) t2 = Node('a').addkid(Node('c').addkid(Node('g'))).addkid( Node('d').addkid(Node('x').addkid(Node('y')).addkid(Node('z')))) enhanced_distance(['m', '+', '+', '+', 'd', '+', 'd', 'm', '+', 'x'], [ 0, 0.05555555555555555, 0.05555555555555555, 0.05555555555555555, 0.2, 0.4, 3.0, 0, 0.5, 0 ])