首先,我们需要实现一个小顶堆的类。可以使用数组来表示堆,数组的第一个元素为根结点,第i个元素的左孩子为2i,右孩子为2i+1。然后,我们可以依次插入给定的数字到堆中,插入完成后,我们可以根据给定的命题进行判断。

下面是完整的C++代码实现:

#include <iostream>
using namespace std;

class MinHeap {
private:
    int* heap;
    int capacity;
    int size;

public:
    MinHeap(int capacity) {
        this->capacity = capacity;
        this->size = 0;
        this->heap = new int[capacity + 1];
    }

    ~MinHeap() {
        delete[] heap;
    }

    void insert(int num) {
        if (size == capacity) {
            return;
        }

        size++;
        int i = size;
        while (i > 1 && num < heap[i/2]) {
            heap[i] = heap[i/2];
            i /= 2;
        }
        heap[i] = num;
    }

    bool isRoot(int num) {
        return heap[1] == num;
    }

    bool areSiblings(int num1, int num2) {
        int index1 = -1, index2 = -1;
        for (int i = 1; i <= size; i++) {
            if (heap[i] == num1) {
                index1 = i;
            }
            if (heap[i] == num2) {
                index2 = i;
            }
        }
        if (index1 == -1 || index2 == -1 || index1 == index2) {
            return false;
        }
        return index1/2 == index2/2;
    }

    bool isParent(int parent, int child) {
        int parentIndex = -1, childIndex = -1;
        for (int i = 1; i <= size; i++) {
            if (heap[i] == parent) {
                parentIndex = i;
            }
            if (heap[i] == child) {
                childIndex = i;
            }
        }
        if (parentIndex == -1 || childIndex == -1) {
            return false;
        }
        return parentIndex == childIndex/2;
    }

    bool isChild(int child, int parent) {
        int childIndex = -1, parentIndex = -1;
        for (int i = 1; i <= size; i++) {
            if (heap[i] == child) {
                childIndex = i;
            }
            if (heap[i] == parent) {
                parentIndex = i;
            }
        }
        if (childIndex == -1 || parentIndex == -1) {
            return false;
        }
        return childIndex/2 == parentIndex;
    }
};

int main() {
    int N, M;
    cin >> N >> M;

    MinHeap heap(N);
    for (int i = 0; i < N; i++) {
        int num;
        cin >> num;
        heap.insert(num);
    }

    for (int i = 0; i < M; i++) {
        string proposition;
        cin >> proposition;

        if (proposition == "is") {
            string type, relation;
            int num1, num2;
            cin >> num1 >> type >> relation >> num2;

            if (type == "root") {
                if (relation == "the") {
                    cout << (heap.isRoot(num1) ? "T" : "F") << endl;
                }
            } else if (type == "and") {
                string relation2;
                int num3;
                cin >> relation2 >> num3;

                if (relation == "are" && relation2 == "siblings") {
                    cout << (heap.areSiblings(num1, num2) ? "T" : "F") << endl;
                }
            } else if (type == "is") {
                string relation2;
                int num3;
                cin >> relation2 >> num3;

                if (relation == "the" && relation2 == "parent" && num3 == "of") {
                    cout << (heap.isParent(num1, num2) ? "T" : "F") << endl;
                } else if (relation == "a" && relation2 == "child" && num3 == "of") {
                    cout << (heap.isChild(num1, num2) ? "T" : "F") << endl;
                }
            }
        }
    }

    return 0;
}

这个程序首先读取插入元素的个数N和命题数M,然后依次读取N个要被插入堆的整数,并插入到堆中。最后,根据给定的命题进行判断,并输出结果

用c++书写这个程序:将一系列给定数字顺序插入一个初始为空的小顶堆H。随后判断一系列相关命题是否为真。命题分下列几种:x is the root:x是根结点;x and y are siblings:x和y是兄弟结点;x is the parent of y:x是y的父结点;x is a child of y:x是y的一个子结点。输入格式:每组测试第1行包含2个正整数N≤ 1000和M≤ 20分别

原文地址: http://www.cveoy.top/t/topic/hTy7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录