You are on page 1of 3

Homework - Week 12

Name:RABIUS SANY
Student Id :2230130218

Please watch these videos first, and finish your homework after class. You can edit
WORD documents directly, or take photos after writing them down, and send them to my
email. My email: xupeng.01@163.com
If you can't do some problems in your homework, you can leave it blank.
请大家先认真学习视频,再完成下列作业。作业可以直接编辑 Word 文档,也可
以写下后拍照,将文档或照片发我的电子邮箱。我的邮箱:xupeng.01@163.com
如果作业中有些题目不会,可以空着不做。

Task 1: Talk about the definition of Binary Trees(Binary Max-Heap). (讲一下二叉树的


定义。)
Answer:

A binary tree is a type of tree data structure in which each node can have at most two
children, referred to as the left child and the right child. Binary trees are commonly
used in computer science and programming for organizing and representing
hierarchical relationships between elements.
A binary max-heap is a specific type of binary tree that satisfies the max-heap property.
The max-heap property states that for any node in the tree, the value stored in that
node is greater than or equal to the values stored in its children. In other words, the
maximum value in the heap is always located at the root node.
In a binary max-heap, the tree is complete, meaning that all levels of the tree are filled,
except possibly for the last level, which is filled from left to right. This property allows
efficient storage and retrieval of the maximum element.
The binary max-heap is often represented using an array, where the root of the tree is
located at index 0. For any node at index i, its left child is located at index 2i + 1, and its
right child is located at index 2i + 2.
Binary max-heaps are commonly used in various algorithms and data structures. They
are particularly useful in priority queues, where the element with the highest priority
needs to be accessed efficiently. The max-heap property ensures that the maximum
element can be quickly obtained by accessing the root of the tree, which has the
maximum value.
Operations on a binary max-heap include inserting a new element, deleting the
maximum element, and retrieving the maximum element. These operations maintain
the heap property by rearranging the elements as necessary. The most common
operations, such as inserting and deleting, involve adjusting the tree structure and
swapping elements to maintain the max-heap property.

In summary, a binary max-heap is a binary tree where each node is greater than or
equal to its children, and the tree is complete. It provides an efficient way to store and
retrieve the maximum element, making it a valuable data structure in various
algorithms and applications.
Task 2:Talk about the advantage of complete binary trees. (讲一下完全二叉树的优点。)
Answer:

Complete binary trees offer several advantages compared to other types of binary trees:

Efficient representation: Complete binary trees can be efficiently represented using an


array or list. The elements of the tree can be stored in sequential order, making it easy
to access and manipulate them. This representation eliminates the need for explicit
pointers or references, saving memory and simplifying the implementation.

Fast indexing: With the array-based representation, complete binary trees enable fast
indexing of elements. Given the index of a node, you can directly access its position in
the array without any traversal or searching. This property is useful in many
algorithms and data structures that rely on efficient indexing, such as heaps and
priority queues.

Easy implementation: Complete binary trees have a regular and predictable structure,
which simplifies their implementation. Adding or removing elements from the tree can
be done efficiently by maintaining the completeness property. Unlike other types of
binary trees, you don't need to rebalance or reorganize the tree after modifications,
reducing the complexity of the operations.

Optimal space utilization: Complete binary trees utilize space efficiently. Except for the
last level, all other levels of the tree are completely filled. This property ensures that the
tree occupies the minimum possible space for a given number of nodes. Other types of
binary trees, like balanced binary search trees, may require additional space to
maintain balance factors or other structural properties.

Efficient heap operations: Complete binary trees are often used to implement binary
heaps, a popular data structure for efficient priority queues and heapsort algorithms.
The completeness property ensures that the tree maintains the desired heap properties,
such as the maximum or minimum element at the root, and efficient operations like
inserting and extracting the extremal elements.

Easy level-order traversal: Complete binary trees support efficient level-order traversal.
Level-order traversal visits the nodes of a tree level by level, from left to right. In a
complete binary tree, this traversal can be implemented straightforwardly using the
array representation and simple arithmetic calculations, without the need for recursive
calls or explicit stack/queue data structures.

These advantages make complete binary trees a valuable choice in various applications
where fast indexing, efficient space utilization, and ease of implementation are
important considerations.

Task 3:Compared with using array/list to implement the operation of priority queue, what
are the advantages of using binary heap?(与使用数组/列表实现优先级队列操作相比,使
用二进制堆(即二叉树)有哪些优点?)
Answer:
Compared to using an array or list, a binary heap offers several advantages when
implementing a priority queue:

Efficient operations: Binary heaps provide efficient implementations for the main
operations of a priority queue, namely insertion and removal of the highest-priority
element. These operations have a time complexity of O(log n), where n is the number of
elements in the heap. In contrast, using an array or list would require searching or
sorting, resulting in a time complexity of O(n) for insertion or removal.

Constant-time access to the highest-priority element: A binary heap allows constant-


time access to the highest-priority element. The root of the heap always holds the
highest-priority element, making it readily accessible without traversing the entire data
structure. This is not the case with an array or list, which would require sorting or
maintaining the elements in a specific order to achieve constant-time access.

Space efficiency: Binary heaps are more space-efficient than arrays or lists for
implementing a priority queue. They typically use an array to store the elements, and
the heap structure itself can be represented in place without additional pointers or
references. In contrast, using an array or list might require additional storage for
maintaining the priority order or other auxiliary data structures.

Simplicity and ease of implementation: Binary heaps have a straightforward structure


and clear rules for maintaining the heap property, which simplifies their
implementation. The operations of insertion, removal, and heapifying can be easily
implemented using simple arithmetic calculations. In contrast, using an array or list
might require more complex algorithms or bookkeeping to maintain the priority order
and ensure efficient operations.

Heapsort capability: Binary heaps can be used to efficiently sort a collection of elements
in ascending or descending order. This capability stems from their ability to efficiently
extract the highest (or lowest) priority element. Using an array or list, sorting the
elements would typically require more complex sorting algorithms like quicksort or
mergesort.

Overall, the advantages of using a binary heap over an array or list for implementing a
priority queue include efficient operations, constant-time access to the highest-priority
element, space efficiency, simplicity of implementation, and the potential for sorting
elements efficiently.

You might also like