6. Constructing a binary search tree -- based on the un-balanced BST algorithms covered in class -- from inserting the values 5, 2, 9, 6, 3, 8, 1 in that order. In what order are the values visited in a preorder traversal of the resulting tree? A. 9,8,6,5,3,2,1 B. 5,3,2,1,9,8,6 C. 3,5,2,9,1,8,6 D. 5,2,1,3,9,6,8
时间: 2023-07-02 14:05:50 浏览: 66
The resulting binary search tree from inserting the values 5, 2, 9, 6, 3, 8, 1 in that order is:
```
5
/ \
2 9
/ \ /
1 3 6
\
8
```
The order of values visited in a preorder traversal of the resulting tree is:
```
5, 2, 1, 3, 9, 6, 8
```
So, the correct answer is D.
相关问题
fastq-dump.2.8.0 err: item not found while constructing within virtual database module - the path 'SRR24827192.fastq.gz' cannot be opened as database or table
这个错误通常意味着 `fastq-dump` 无法找到指定的文件或目录。可能有以下几种原因导致该错误:
1. 文件路径错误:请确保你提供的文件路径是正确的,并且文件存在于该路径下。你可以使用绝对路径或相对路径来指定文件。
2. 文件权限不足:请确保你对该文件具有读取权限。你可以尝试使用 `ls -l` 命令来查看文件的权限,并使用 `chmod` 命令来更改文件的权限。
3. 文件损坏或缺失:如果文件被损坏或缺失,`fastq-dump` 将无法打开它。请确保文件没有被意外地删除或损坏。
如果你确定文件路径和权限都是正确的,但仍然遇到这个错误,请提供更多详细信息,例如完整的命令和错误输出,以便我可以更好地帮助你解决问题。
write a C++ program for constructing a heap (max heap) with integers and printing it in the rotated form.
Sure, here's the code:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void heapify(vector<int>& arr, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void buildHeap(vector<int>& arr) {
int n = arr.size();
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
}
void printHeap(vector<int>& arr) {
int n = arr.size();
int row = 1, col = 0;
while (col < n) {
for (int i = col; i < min(n, col + row); i++)
cout << arr[i] << " ";
cout << endl;
col += row;
row *= 2;
}
}
int main() {
vector<int> arr = { 4, 10, 3, 5, 1 };
buildHeap(arr);
printHeap(arr);
}
```
This program first constructs a max heap from a given vector of integers using the `buildHeap` function. It then prints the heap in a rotated format using the `printHeap` function.
The `heapify` function is used to maintain the heap property while constructing the heap. It takes an array `arr`, its size `n`, and the index of the current node `i` as input. It compares the value at the current node with its left and right child nodes, and swaps the current node with the largest child node if necessary. It then recursively calls itself on the largest child node to maintain the heap property.
The `buildHeap` function iterates over the second half of the array in reverse order (since these nodes are the ones with children) and calls `heapify` on each node to construct the heap.
The `printHeap` function prints the heap in a rotated format. It first initializes the row and column numbers, and then iterates over each row of the heap, printing the nodes in that row and updating the column and row numbers accordingly.
Note that this program assumes a complete binary tree representation of the heap. If the input vector is not complete, the program may produce unexpected results.