编写社交网络的数据结构,要求网络中每个人都有姓名、年龄、职业、好友列表的基本信息。并完成如下编程,用c语言代码实现,不能用第三方库,并截图给出代码运行结果。
(a)、根据姓的拼音首字母在以26个英文字母顺序所在的位置实现二叉排序树的构建和查找,可以不考虑同拼音的情况。
(b)、编写好友推荐算法,算法如下:A和B是好友,B和C是好友,但A和C不是好友,这时候分别向A和C推荐对方加好友。
(c)、找出网络中所有的完全子图,在这个完全子图里面每两个用户都互为好友。
编写社交网络的数据结构
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注
下面是用 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
请注意,本代码仅为示例,可能不够完善或高效,请根据具体需求自行修改。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用