A console-based social network implementation that models users as nodes and friendships as undirected edges in a graph structure. The system provides core social networking functionality including user management, friendship management, mutual friend discovery, and friend suggestions.
AVL Tree Implementation
- Self-balancing binary search tree for efficient string-keyed data storage
- Used for both user directory and friend relationship sets
- Provides O(log n) time complexity for insertions, searches, and deletions
User Structure
name: String identifier for the userfriends: AVL tree storing friend names (keys only, values unused)friend_count: Integer tracking number of friends for optimization
g_users: Global AVL tree mapping usernames to User objectsg_user_count: Integer tracking total number of users (max 50)
ADD_USER Command
- Creates new user with specified name
- Validates: non-empty name, user limit (50), duplicate prevention
- Initializes empty friend set and zero friend count
Constraints
- Maximum 50 users
- Unique usernames required
- Case-sensitive user identification
ADD_FRIEND Command
- Creates bidirectional friendship between two users
- Validates: both users exist, no self-friending, duplicate prevention
- Maintains symmetry by adding relationship to both users' friend sets
VIEW_FRIENDS Command
- Lists all direct friends of specified user
- Displays friend names in lexicographic order
- Handles empty friend sets gracefully
MUTUAL Command
- Finds intersection of two users' friend sets
- Implements optimization by iterating smaller friend set
- Returns mutual friends in lexicographic order
SUGGEST Command
- Recommends friends-of-friends not already connected
- Uses common neighbor count as recommendation score
- Excludes self and existing friends from suggestions
- Displays suggestions with number of common connections
- Insertion: Standard AVL insertion with rotation balancing
- Search: Binary search with string comparison
- Traversal: In-order traversal for sorted output
- Memory Management: Recursive cleanup with value-specific deallocation
- Retrieve both users' friend sets
- Select smaller set for iteration
- Check each friend against larger set using AVL contains
- Output matches in sorted order
- For each friend of target user
- For each friend-of-friend (excluding self and existing friends)
- Count common connections using candidate map
- Output suggestions sorted lexicographically with connection counts
- Interactive prompt-based interface
- Case-insensitive command parsing
- Clear error messaging and usage instructions
- Real-time feedback for operations
ADD_USER <name>ADD_FRIEND <name1> <name2>VIEW_FRIENDS <name>MUTUAL <name1> <name2>SUGGEST <name>EXIT
- User operations: O(log n)
- Friendship operations: O(log n)
- Mutual friends: O(m log n) where m is size of smaller friend set
- Friend suggestions: O(d² log n) where d is average friend count
- O(n + e) where n is users and e is edges
- Each user: O(1) for structure + O(f) for friend storage
- Global: O(n) for user directory
- Empty username prevention
- User existence verification
- Self-friending prevention
- Duplicate friendship detection
- Maximum user limit enforcement
- Clear, descriptive error messages
- Usage instructions for malformed commands
- Informative status messages for successful operations
- Dynamic memory allocation for all structures
- String duplication for name storage
- Recursive tree deallocation on program exit
- Recursive AVL tree destruction
- User structure deallocation with friend tree cleanup
- Proper memory freeing for all dynamically allocated resources
- Efficient Data Structures: AVL trees provide optimal search and insertion performance
- Scalable Design: Algorithm optimizations for large friend sets
- Clean Separation: Modular architecture with distinct functional components
- Robust Error Handling: Comprehensive input validation and error reporting
- Memory Safe: Proper cleanup procedures prevent memory leaks
- Fixed User Limit: Maximum 50 users hardcoded
- In-Memory Only: No persistence between program executions
- Single-Threaded: No concurrent access handling
- Case Sensitivity: Usernames are case-sensitive
- No Network: Local console-only implementation
AVL Tree Operations
avl_insert(): Insert with balancingavl_find(): Search for keyavl_contains(): Check existenceavl_foreach(): In-order traversalavl_free(): Recursive cleanup
User Management
add_user(): Create new user with validationget_user(): Retrieve user by namefree_user_value(): Cleanup user resources
Friendship Operations
add_friendship(): Create bidirectional friendshipview_friends(): Display user's friendsview_mutual(): Find common friendssuggest_friends(): Generate friend recommendations
- Command Input: User enters command via CLI
- Parsing: Command tokenized and validated
- Execution: Corresponding function called with parameters
- Data Access: AVL tree operations performed
- Output: Results displayed to user
- Cleanup: Memory freed on program exit
This design provides a solid foundation for a social network system with efficient algorithms for core social networking operations while maintaining clean, maintainable code structure.