📜  测试用例生成 |第 6 组(随机未加权二叉树)

📅  最后修改于: 2022-05-13 01:56:05.692000             🧑  作者: Mango

测试用例生成 |第 6 组(随机未加权二叉树)

生成随机未加权二叉树

  • 由于这是一棵树,因此测试数据生成计划不会形成循环。
  • 边数比顶点数少一。
  • 对于每个RUN ,首先打印节点数,例如N ,接下来的N - 1行是(a, b)形式,其中ab 的父级。
  • 每个节点最多包含 2 个子节点。

方法:可以使用队列解决问题。这个想法是使用 BFS 遍历树。请按照以下步骤解决问题:

  • 初始化一个映射,比如mp来检查一个节点是否已经包含在树中。
  • 初始化一个队列来存储树的每一层的节点。
  • 1视为根节点并将其插入队列。
  • 当树中的节点总数不等于N时迭代队列。在每次迭代中,使用 rand()函数和映射将树的每个级别的不同节点插入队列,还将节点和节点的父节点插入数组
  • 最后,打印N的值和数组。
CPP
// C++ Program to generate test cases for
// an unweighted tree
  
#include 
using namespace std;
  
  
// Function to generate the binary tree using BFS
vector > generateBinaryTree(int n)
{
      
      
    // Stores number of children
    // a node can have
    vector options = { 0, 1, 2 };
      
      
    // Check if a node is already
    // included in the tree or not
    map mp;
  
  
    // Stores node of tree at 
    // each level of the tree
    queue q;
  
  
    // Insert root node
    q.push(1);
  
  
    // Stores the generated tree
    vector > v;
  
  
    // Store count of nodes
    // already included
    int count = 1;
  
  
    // Marking the inclusion
    // of node 1
    mp[1] = 1;
  
  
    // Traverse tree using BFS
    while (!q.empty() or count < n) {
          
          
        // Stores from element
        // of queue
        int front;
          
        if(!q.empty()) {
              
              
            // Update front
            front = q.front();
              
              
            // Pop front element 
            // of queue
            q.pop();
        }
          
          
          
  
  
        // Find count of child nodes
        // of current node
        int numberOfChilds
          = options[rand() % (options.size())];
  
  
        // If all the nodes are 
        // already included
        if (count >= n)
            continue;
              
  
        // Connect child node to
        // the parent node
        while (numberOfChilds--) {
              
              
            // Stores value in node which
            // is not already included
            int child = rand() % n + 1;
  
  
            // Find the child until found a node
            // that is not yet included
            while (mp[child]) {
                child++;
                if (child > n) {
                    child = 1;
                }
            }
  
              
            // Update count
            count++;
  
  
            // Mark the included node
            mp[child] = 1;
  
  
            // Insert it to the generated tree
            // as {parent, child}
            v.push_back({ front, child });
  
  
            // Push the child into the queue
            q.push(child);
  
  
            // If all the nodes are included
            // break
            if (count == n)
                break;
        }
    }
  
  
    // Shuffle the v vector randomly
    random_shuffle(v.begin(), v.end());
  
    return v;
}
  
  
// Function to print the generated tree
void printTree(int n, vector > v)
{
    int s = v.size();
  
  
    // Number of nodes
    cout << n << "\n";
  
  
    // Print n-1 edges as {parent, child}
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << "\n";
    }
}
  
  
// Driver Code
int main()
{
      
      
    // Random seeding
    srand(time(NULL));
  
  
  
    // Number of node between 3 to 8
    // this range can be easily changed
    int n = rand() % 6 + 3;
  
  
    // Function Call
    vector > v 
              = generateBinaryTree(n);
  
    // Print the generated tree
    printTree(n, v);
}


输出:
5
5 4
1 2
1 3
3 5