首页 >
> 详细

Question 1 (20 points):

a. Given the following (unbalanced) binary search tree:

We are executing the following two operations on the tree above (not one after

the other):

• Inserting 10

• Deleting 13

For each one of these operations, apply the algorithm described in class, and

draw the resulting tree.

b. Let T be a binary search tree. If we traverse T in post-order, we get the

following sequence:

Postorder(T): 3, 5, 4, 6, 2, 9, 8, 11, 10, 7, 1

Draw T.

Question 2 (25 points):

Consider the following definition of a mono-data subtree:

Let T be a binary tree containing integers as data in its nodes, and let T’ be a

subtree of T. We say that T’ is a mono-data subtree, if all the nodes of T’ have the

same data.

For example, in the following tree, the subtree that is circled in red is a mono-data

subtree, as all its nodes have the same data (the common data is 4).

Note: Each leaf is a mono-data subtree (these are the smallest mono-data

subtrees possible).

In this question, we will implement the following function:

def count_mono_data_subtrees(bin_tree)

The function is given bin_tree, a non-empty LinkedBinaryTree object, it will

return the number of mono-data subtrees in bin_tree.

For example, if called with the tree above, it should return 6, as these are the six monodata

subtrees:

Complete the implementation (given in the next page) for the function

count_mono_data_subtrees.

In the implementation you should define a nested recursive helper function:

def count_mono_data_subtrees_helper(root)

This function is given root, a reference to a LinkedBinaryTree.Node, that

indicates the root of the subtree that this function operates on.

4

def count_mono_data_subtrees(bin_tree):

def count_mono_data_subtrees_helper(root):

…

…

…

____________ = count_mono_data_subtrees_helper(bin_tree.root)

return _________

Implementation requirements:

1. Your implementation has to run in linear time. That is, if there are n nodes in

the tree, your function should run in θ(n) worst-case.

2. Your implementation for the helper function must be recursive.

3. You are not allowed to:

o Define any other helper function.

o Add parameters to the function’s header lines.

o Set default values to any parameter.

o Use global variables.

Hint:

To meet the runtime requirement, you may want count_mono_data_subtrees_helper

to return more than one value (multiple values could be collected as a tuple).

5

Question 3 (25 points):

We say that a sequence of numbers is a palindrome if it is read the same

backward or forward.

For example, the sequence: 6, 15, 6, 3, 47, 3, 6, 15, 6 is a palindrome.

Implement the following function:

def construct_a_longest_palindrome(numbers_bank)

When given numbers_bank, a non-empty list of integers, it will create and return

a list containing a longest possible palindrome made only with numbers from

numbers_bank.

Notes:

1. The longest palindrome might NOT contain all of the numbers in the

sequence.

2. If no multi-number palindromes can be constructed, the function may return

just one number (as a single number, alone, is a palindrome).

3. If there is more than one possible longest palindrome, your function can

return any one of them.

For example, if numbers_bank=[3, 47, 6, 6, 5, 6, 15, 3, 22, 1, 6, 15],

Then the call construct_a_longest_palindrome(numbers_bank) could return:

[6, 15, 6, 3, 47, 3, 6, 15, 6] (Which is a palindrome of length 9, and

there is no palindrome made only with numbers from numbers_bank that is

longer than 9).

Implementation requirements:

1. You may use one ArrayQueue, one ArrayStack, and one ChaniningHashTableMap.

2. Your function has to run in expected (average) linear time. That is, if numbers_bank

is a list with n numbers, your function should run in &(') average case.

3. Besides the queue, stack, hash table, and the list that is created and returned, you may

use only constant additional space. That is, besides the queue, stack, hash table, and

the returned list, you may use variables to store an integer, a double, etc. However, you

may not use an additional data structure (such as another list, stack, queue, etc.) to

store non-constant number of elements.

6

Question 4 (30 points):

Recall the Minimum-Priority-Queue ADT we introduced in class. A minimum priority queue

is a collection of (priority, value) items, that come out in an increasing order of priorities.

A Minimum-Priority-Queue supports the following operations:

• p = PriorityQueue(): Creates an empty priority queue.

• len(p): Returns the number of items in p.

• p.is_empty(): Returns True if p is empty, or False otherwise.

• p.insert(pri, val): Inserts an item with priority pri and value val to p.

• p.min(): Returns the Item (pri, val) with the lowest priority in p,

or raises an Exception, if p is empty.

• p.delete_min() : Removes and returns the Item (pri, val) with the

lowest priority in p, or raises an Exception, if p is empty.

Complete the definition below of the LinkedMinHeap class, implementing the MinimumPriority-Queue

ADT. In this implementation, you should represent the heap using node

objects and references to form a tree structure (a “linked representation” of the tree). That is,

you should construct Node objects with references to their "children" and “parent”.

Note: In class (when we implemented the ArrayMinHeap class) we represented the heap

using an “array representation” of the tree.

class LinkedMinHeap:

class Node:

def __init__(self, item):

self.item = item

self.parent = None

self.left = None

self.right = None

class Item:

def __init__(self, priority, value=None):

self.priority = priority

self.value = value

def __lt__(self, other):

return self.priority < other.priority

def __init__(self):

self.root = None

self.size = 0

def __len__(self):

…

def is_empty(self):

…

def min(self):

…

def insert(self, priority, value=None):

…

def delete_min(self):

…

7

Notes:

1. In the LinkedMinHeap class that you would need to complete (given in the

previous page), we already implemented two nested classes:

• Node – should be used for each node object of the linked binary tree.

• Item – should be used to store the (priority, value) item of the Priority Queue.

2. We also implemented the __init__ method of the LinkedMinHeap class.

Each LinkedMinHeap object would maintain two data members:

• self.root – A reference to the heap’s root node. Initially set to None

(indicating an empty tree)

• self.size – Indicating the number of nodes in the heap. Initially set to 0.

Implementation requirements:

1. You are not allowed to add data members to the LinkedMinHeap object.

That is, you can’t edit the __init__ method, that initializes root and size as the

only data member.

2. Runtime requirements:

• Each one of the insert and delete_min operations should run in θ(log (n))

worst case (where n is the number of elements in the priority queue).

• Each one of the len, is_empty, and min operations should run in θ(1) worst case.

3. You may define additional helper methods.

Hint: You might want to re-watch the beginning of the last lecture (1134 – 5/6 lecture).

We had a short discussion about one of the challenges in representing the heap using the

linked representation of the tree, and we described a way to overcome this challenge.

联系我们

- QQ：99515681
- 邮箱：99515681@qq.com
- 工作时间：8:00-23:00
- 微信：codinghelp2

- Cis 484作业代做、代写sql编程语言作业、代做sql课程设计作业、代写 2020-09-27
- 代写kit206课程作业、代做software留学生作业、代写c++程序语言 2020-09-27
- Comp2100作业代做、代写programming作业、C/C++编程设计 2020-09-27
- Msbd5015作业代写、Python编程语言作业调试、代写python课程 2020-09-27
- Programming作业代写、Java程序设计作业调试、代做algorit 2020-09-27
- Cisc 360作业代做、代写java程序设计作业、Python，C++语言 2020-09-27
- Cs 570留学生作业代做、Java程序语言作业调试、代写java课程设计作 2020-09-27
- 代做isys 1108作业、代做software作业、代写python，C+ 2020-09-27
- Data留学生作业代写、Java，C++程序设计作业调试、Python语言作 2020-09-26
- Csc220作业代做、Data留学生作业代做、代写java课程作业、Java 2020-09-26
- 代写csc220作业、代做java实验作业、Java程序语言作业调试、代做p 2020-09-26
- Bridging Coursework 2020-09-26
- Comp Sci 3004/7064 Operating Systems ... 2020-09-26
- Comp9311 20T2 - Assignment 2 2020-09-26
- Ipal Capstone Project 2020-09-26
- Ipal Programming In R - Week 2 Assign... 2020-09-26
- Csc 503/Seng 474 Data Mining Assignmen... 2020-09-26
- Assignment 1Cmpt 307 2020-09-26
- Csci 2300 Lecture Exercise 5 2020-09-26
- Csci 2300 Lab 4 2020-09-26