跳到主要内容

2024-2025学年下学期第二次月考

2024-2025学年下学期第二次月考试卷——鲍钰​

一、简答题(40 分,每题 8 分)​

  1. 设一棵二叉树的前序和中序序列如下:

    前序序列:A B D F C E G H  中序序列:B F D A G E H C

    (1)画出这棵二叉树。(4 分)

    (2)给出这棵二叉树的后序遍历。(4 分)


  2. 按顺序分别将 {42,26,8,70,102,56,2}\{42, 26, 8, 70, 102, 56, 2\} 这几个数插入一颗为空的 AVL 树,请图示并文字描述具体过程。


  3. 请写出如下序列插入排序的过程。

    [26,15,67,92,9,16,72,86,49][26, 15, 67, 92, 9, 16, 72, 86, 49]


  4. 请按要求完成如下二叉查找树(Binary Search Tree)的相关操作。

    a. 请写出其前序和后序遍历的结果。(4 分)

    b. 请利用二叉查找树的删除算法删除关键码 k,并画出结果。(4 分)

    简答题第4题二叉查找树


  5. 现有以下的关键码:Key={18,73,49,31,68,99,27,41,25,6,12}\text{Key}=\{18, 73, 49, 31, 68, 99, 27, 41, 25, 6, 12\},散列表采用用数组顺序存储,大小为 13(地址序列为 0-12),散列函数为 h(key)=key%13h(\text{key})=\text{key}\%13,

    1)请用 linear probing.(线性探测)的方法来解决冲突,并画出所有关键码在 Hash 表中的存储位置效果图。(4 分)

    2)当访问关键码 99 时一共需要多少个步骤,请写出具体的访问过程。(2 分)

    3)当访问关键码 39 时一共需要多少个步骤,请写出具体的访问过程。(2 分)


二、填空题(10 分,每空 2 分)​

  1. 请补充哈希查找的如下程序代码,在空格处填上合适的代码。

    Error_code Hash_table :: retrieve(const Key &target, Record &found) const{
    int probe_count, // Counter to be sure that table is not full.
    increment, // Increment used for quadratic probing.
    probe; // Position currently probed in the hash table.
    probe = hash(target);
    probe_count = 0;
    increment = 1;
    while (______【1】______ // Is the location empty?
    && table[probe].the_key() != target.the_key() // not found
    && probe_count < (hash_size + 1)/2) { // Has overflow occurred?
    probe_count++;
    ______【2】______;
    increment += 2; // Prepare increment for next iteration.
    }
    if (table[probe].the_key() == target.the_key()){
    ______【3】______;
    return success;
    }
    else return not_present;
    }

  2. 请补充快速排序的如下程序代码。

    int Sortable_list :: partition(int low, int high){
    Record pivot;
    int i, // used to scan through the list
    last_small; // position of the last key less than pivot
    swap(low, (low + high)/2);
    ______【4】______;// First entry is now pivot .
    last_small = low;
    for (i = low + 1; i <= high; i++)
    if (entry[i] < pivot) {
    last_small = last_small + 1;
    swap(last_small, i); // Move large entry to right and small to left.
    }
    ______【5】______;// Put the pivot into its proper position.
    return last_small;
    }

三、算法与编程题(50 分,前两题每题 10 分,后两题每题 15 分)​

  1. (10 分)请将二叉查找树中如下查找算法的递归版本改写成非递归版本。

    template <class Record>
    Binary_node<Record> *Search_tree<Record> :: search_for_node(
    Binary_node<Record>* sub_root, const Record &target) const
    /*
    Pre: sub_root is NULL or points to a subtree of a Search tree
    Post: If the key of target is not in the subtree, a result of NULL is returned. Otherwise,
    a pointer to the subtree node containing the target is returned.
    */
    {
    if (sub_root == NULL || sub_root->data == target)
    return sub_root;
    else if (sub_root->data < target)
    return search_for_node(sub_root->right, target);
    else return search_for_node(sub_root->left, target);
    }

  2. (10 分)请设计算法 leaves 实现求解任意二叉树的叶子节点数目。

    int leaves(Binary_node *root);
    //returns the number of leaves in this tree
    //the class Binary_node is like below.

    struct Binary_node {
    // data members:
    int data;
    Binary_node *left;
    Binary_node *right;
    // constructors:
    Binary_node( );
    Binary_node(const int &x);
    };

  3. (15 分)实现 AVL 树中的右旋转 rotate_right 函数。

    template <class Record>
    void AVL_tree<Record> :: rotate_right(Binary_node<Record> * &sub_root);
    /* Pre: sub_root points to a subtree of the AVL tree . This subtree has a nonempty
    left subtree.
    Post: sub_root is reset to point to its former left child, and the former sub_root
    node is the right child of the new sub_root node. */

  4. (15 分)已知 Binary_node 为二叉树节点的定义,请完成函数 countNodes,统计以 root 为根的二叉树中节点值大于 x 的数量。

    struct Binary_node {
    // data members:
    int data;
    Binary_node *left;
    Binary_node *right;
    // constructors:
    Binary_node( );
    Binary_node(const int &a);
    };

    int countNodes(Binary_node *root, int x);