Question

In: Computer Science

(+5) Level of a node in a binary tree is distance from root to that node....

    1. (+5) Level of a node in a binary tree is distance from root to that node. For example, level of root is 0 and levels of left and right children of the root are 1.

    Level      Max number of nodes

    1. 1
    2. 2
    3. 4
    4. 8
    5. 16
    6. 32
    7. 64

    ..      …

    n       ??

    The maximum number of nodes on level n of a binary tree is :

    A          2^(n-1)                         B          2^n

    C          2^(n+1)                       D            2^[(n+1)//2]

    In the above answers, the operator '^' indicates power (or exponent).

    Hint: Check your answer using level 5 and 6 data

    (+30)   4. 5. 9. Worth 4 points all others 3 points

    Using the lists a and vowels as defined:

    a = [ "Euclid", "Archimedes", "Newton”, “Descartes", "Fermat", "Turing", "Euler", "Einstein", "Boole", "Fibonacci", "Nash"]
    vowels = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U','u']
    write a Python program that will

    1. Display the longest name (done #12 below )
    2. Display the shortest name
    3. Display the number of names in the list NOTE: your answer should be 11 but do NOT use the constant 11 in your output; code the size of the list using len function
    4. Display a string that consists of the first letter from each of 11 names in the list a

    Output should be the string “EANDFTEEBFN”

    See 9 in the output and consider using the + operator Must use a loop

    1. Display a string that consists of the last letter from each of 11 names in the list a

    Output should be the string “dsnstgrneih”

    See 10 in the output Must use a loop

    1. Ask the user for a letter. Display the number of times the letter appears in the list See the count function from the prior labs. For example if the user selects the character ‘F’ the output should be 2 Lowercase and uppercase are not equivalent
    2. Sort list a and display the list i.e. the first name in the list should be Archimedes; last name is Turing
    3. Sort the list in reverse order and display the list i.e. the first name should be Turing and the last Archimedes

    For 7 and 8 use sort() and reverse() functions as needed

    1. Display the number of vowels using the vowels list in the list a (in operator may be useful) See the variable vowels in the code
    2. Add your last name to the list and display the list in sorted order (your last name should be in the list

    Solutions

    Expert Solution

    Thanks for the question, here are the code for all the sub parts with comments

    ======================================================================

    a = [ "Euclid", "Archimedes", "Newton", "Descartes", "Fermat", "Turing", "Euler", "Einstein", "Boole", "Fibonacci", "Nash"]
    vowels = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U','u']
    #1.    Display the longest name (done #12 below )
    longest_word=''
    shortest_name=a[0]
    for word in a:
        if len(longest_word)<len(word):
            longest_word=word
        if len(shortest_name)>len(word):
            shortest_name=word

    #1. Longest Name
    print('Longest Name: {}'.format(longest_word))
    #2.    Display the shortest name
    print('Shortest Name: {}'.format(shortest_name))
    #3.    Display the number of names in the list
    print('Number of names: {}'.format(len(a)))
    #4.    Display a string that consists of the first letter from each of 11 names in the list a
    initials=''
    for word in a:
        initials=initials+word[0]
    print('Initials: {}'.format(initials))

    #5.    Display a string that consists of the last letter from each of 11 names in the list a
    last_letters=''
    for word in a:
        last_letters=last_letters+word[-1]
    print('Last Letters: {}'.format(last_letters))

    #6.    Ask the user for a letter. Display the number of times the letter appears in the list
    letter = input('Enter a letter: ')
    count_occurences=0
    for word in a:
        count_occurences+=word.count(letter)
    print('Letter: {} occurs {} times'.format(letter,count_occurences))

    #7.    Sort list a and display the list
    a.sort()
    print('Sorted list',a)

    #8.    Sort the list in reverse order and display the list
    a.sort()
    a.reverse()
    print('Reverse Sorted', a)

    #9.    Display the number of vowels using the vowels list in the list a

    vowels_count=0
    for word in a:
        for vowel in vowels:
            vowels_count+=word.count(vowel)
    print('There are {} vowels in the list'.format(vowels_count))


    Related Solutions

    You are given a reference to the root node of a binary search tree, that implements...
    You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
    You are given a reference to the root node of a binary search tree, that implements...
    You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
    1. Draw a binary search tree as a single root node holding a string as the...
    1. Draw a binary search tree as a single root node holding a string as the data element. Each string inserted into a node in the tree will be for a character in a game. Then, draw a new tree each time you insert a new node into the tree holding a string Insert 4 nodes total, including the root. This means the new nodes will need to be inserted at the correct child to maintain the BST property.
    (IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
    (IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
    Consider the following splay tree: Show the paths from root to node 12, 10, 9, 5,...
    Consider the following splay tree: Show the paths from root to node 12, 10, 9, 5, and 1 after search node 3. (Sample answer: for the above splay tree, the path from root to node 9 can be expressed as 10, 4, 6, 8, 9.) The path from root to node 12:Question Blank.The path from root to node 10:Question Blank.The path from root to node 9:Question Blank.The path from root to node 5:Question Blank.The path from root to node 1:Question...
    Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
    Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
    Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
    Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
    Consider the following struct that represents a node within a binary tree: struct Node { int...
    Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
    Implement a function to remove a leaf node from a binary search tree. Using the following...
    Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
    A binary tree is a rooted tree in which each node has at most two children....
    A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children is exactly one less than the number of leaves.
    ADVERTISEMENT
    ADVERTISEMENT
    ADVERTISEMENT