logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Nov 13, 2019
      ·  Edited: Nov 13, 2019

    Senior2019-Set 4 - Recursive Functions and CNS 4

    in ACSL Contest Prep

    Short Questions:


    Programming: ACST_Trees

    5 comments
    0
    Steven
    Nov 17, 2019

    1. E

    2. A

    3. B

    0
    awesomeface259
    Nov 17, 2019

    2. E

    3. A

    12. B

    0
    chris13888
    Nov 17, 2019

    a = input() 
     
    class Node(): 
        def __init__(self, value, depth=0): 
            self.value = value 
            self.left = self.right = None 
            self.depth = depth 
        def insert(self, value): 
            if value <= self.value: 
                if self.left == None: 
                    self.left = Node(value, self.depth+1) 
                    # print(f'{value} is the left child of {self.value}') 
                else: 
                    self.left.insert(value) 
            if value > self.value: 
                if self.right == None: 
                    self.right = Node(value, self.depth+1) 
                    # print(f'{value} is the right child of {self.value}') 
                else: 
                    self.right.insert(value) 
        def children(self): 
            children = 0 
            if self.left != None: children += 1 
            if self.right != None: children += 1 
            return children 
        def match(self, other): 
            if self.left == None and self.right == None: 
                return True 
            if other.left == None and self.left != None: 
                return False 
            if other.right == None and self.right != None: 
                return False 
            if self.left == None: 
                return self.right.match(other.right) 
            if self.right == None: 
                return self.left.match(other.left) 
            return self.left.match(other.left) and self.right.match(other.right) 
        def traverse(self): 
            # print(self.value, self.left, self.right) 
            yield self 
            if self.left != None: 
                yield from self.left.traverse() 
            if self.right != None: 
                yield from self.right.traverse() 
     
    A = Node(a[0]) 
    for c in a[1:]: 
        A.insert(c) 
     
    leaf = external = external_path = internal_path = depth = 0 
    for node in A.traverse(): 
        if node.children() == 0: 
            leaf += 1 
            external += 2 
            external_path += 2*(node.depth+1) 
        if node.children() == 1: 
            external += 1 
            external_path += node.depth+1 
        internal_path += node.depth 
        depth = max(node.depth, depth) 
     
    print(depth, leaf, external, internal_path, external_path)
    
    

    0
    kchen4000
    Nov 17, 2019

    unnamed
    .jpg
    Download JPG • 761KB

    0
    kchen4000
    Nov 17, 2019

    2)e 3)a 12)b

    0
    5 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners