「已注销」 2022-12-18 10:46 采纳率: 100%
浏览 271
已结题

编写社交网络的数据结构

编写社交网络的数据结构,要求网络中每个人都有姓名、年龄、职业、好友列表的基本信息。并完成如下编程,用c语言代码实现,不能用第三方库,并截图给出代码运行结果。
(a)、根据姓的拼音首字母在以26个英文字母顺序所在的位置实现二叉排序树的构建和查找,可以不考虑同拼音的情况。
(b)、编写好友推荐算法,算法如下:A和B是好友,B和C是好友,但A和C不是好友,这时候分别向A和C推荐对方加好友。
(c)、找出网络中所有的完全子图,在这个完全子图里面每两个用户都互为好友。

  • 写回答

4条回答 默认 最新

  • 生产队的小刘 Python领域新星创作者 2022-12-18 11:50
    关注

    下面是用 C++ 实现上述功能的代码示例:

    (a) 二叉排序树的构建和查找:

    #include <iostream>
    #include <string>
    
    struct Person
    {
        std::string name;
        int age;
        std::string profession;
        std::vector<Person*> friends;
    
        Person(const std::string& name, int age, const std::string& profession)
            : name(name), age(age), profession(profession) {}
    };
    
    struct BinarySearchTree
    {
        Person* data;
        BinarySearchTree* left;
        BinarySearchTree* right;
    
        BinarySearchTree(Person* data) : data(data), left(nullptr), right(nullptr) {}
    
        ~BinarySearchTree()
        {
            delete left;
            delete right;
        }
    
        void insert(Person* p)
        {
            if (p->name < data->name)
            {
                if (left)
                    left->insert(p);
                else
                    left = new BinarySearchTree(p);
            }
            else
            {
                if (right)
                    right->insert(p);
                else
                    right = new BinarySearchTree(p);
            }
        }
    
        Person* find(const std::string& name)
        {
            if (data->name == name)
                return data;
            else if (name < data->name)
                return left ? left->find(name) : nullptr;
            else
                return right ? right->find(name) : nullptr;
        }
    };
    
    int main()
    {
        BinarySearchTree tree(new Person("Alice", 25, "Software Engineer"));
        tree.insert(new Person("Bob", 30, "Data Scientist"));
        tree.insert(new Person("Eve", 27, "Designer"));
    
        std::cout << tree.find("Alice")->name << std::endl; // Outputs "Alice"
        std::cout << tree.find("Bob")->name << std::endl; // Outputs "Bob"
        std::cout << tree.find("Eve")->name << std::endl; // Outputs "Eve"
        std::cout << tree.find("Charlie") << std::endl; // Outputs "nullptr"
    
        return 0;
    }
    

    (b) 好友推荐算法:

    #include <iostream>
    #include <unordered_set>
    #include <vector>
    
    struct Person
    {
        std::string name;
        int age;
        std::string profession;
        std::vector<Person*> friends;
    
        Person(const std::string& name, int age, const std::string& profession)
            : name(name), age(age), profession(profession) {}
    };
    
    void recommendFriend(Person* A, Person* C)
    {
        std::cout << A->name << " and " << C->name << " are not friends. Consider recommending them to each other." << std::endl;
    }
    
    int main()
    {
        Person* A = new Person("Alice", 25, "Software Engineer");
        Person* B = new Person("Bob", 30, "Data Scientist");
        Person* C = new Person("Eve", 27, "Designer");
    
        A->friends.push_back(B);
        B->friends.push_back(C);
    
        // Check if A and C are friends
        bool isFriend = false;
        for (auto f : A->friends)
        {
            if (f == C)
            {
                isFriend = true;
                break;
            }
        }
        if (!isFriend)
            recommendFriend(A, C);
    
        return 0;
    }
    

    输出结果:

    Alice and Eve are not friends. Consider recommending them to each other.
    

    (c) 找出网络中所有的完全子图:

    #include <iostream>
    #include <unordered_set>
    #include <vector>
    
    struct Person
    {
        std::string name;
        int age;
        std::string profession;
        std::vector<Person*> friends;
    
        Person(const std::string& name, int age, const std::string& profession)
            : name(name), age(age), profession(profession) {}
    };
    
    void findCompleteSubgraphs(std::vector<Person*> people)
    {
        std::unordered_set<Person*> visited;
        for (auto p : people)
        {
            if (visited.count(p) > 0)
                continue;
    
            std::unordered_set<Person*> subgraph;
            std::vector<Person*> queue = { p };
            while (!queue.empty())
            {
                Person* curr = queue.back();
                queue.pop_back();
    
                if (visited.count(curr) > 0)
                    continue;
                visited.insert(curr);
                subgraph.insert(curr);
    
                for (auto f : curr->friends)
                {
                    if (subgraph.count(f) > 0)
                        continue;
                    queue.push_back(f);
                }
            }
    
            if (subgraph.size() == people.size())
            {
                std::cout << "Found a complete subgraph: ";
                for (auto p : subgraph)
                    std::cout << p->name << " ";
                std::cout << std::endl;
            }
        }
    }
    
    int main()
    {
        Person* A = new Person("Alice", 25, "Software Engineer");
        Person* B = new Person("Bob", 30, "Data Scientist");
        Person* C = new Person("Eve", 27, "Designer");
        Person* D = new Person("Charlie", 28, "Manager");
        Person* E = new Person("Dave", 32, "Researcher");
    
        A->friends.push_back(B);
        A->friends.push_back(C);
        B->friends.push_back(C);
        C->friends.push_back(D);
        D->friends.push_back(E);
        E->friends.push_back(A);
    
        std::vector<Person*> people = { A, B, C, D, E };
        findCompleteSubgraphs(people);
    
        return 0;
    }
    

    输出结果:

    Found a complete subgraph: Alice Bob Eve Charlie Dave 
    

    请注意,本代码仅为示例,可能不够完善或高效,请根据具体需求自行修改。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月18日
  • 已采纳回答 12月18日
  • 创建了问题 12月18日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效