Ex 3 - Please Do It On NetBeans [Java] Ex: A social networking website stores in
ID: 3830240 • Letter: E
Question
Ex 3 - Please Do It On NetBeans [Java]
Ex: A social networking website stores information about its users in a binary search tree. In addition to the components that provide access to the left and right branches of the tree, each node in the tree contains a unique user number (the key value), the user's name, and a list of the names of each of the user's friends.
1. Write the data definitions needed for this binary search tree.
2. Write a method addNewUser which takes a user number, a user name, a list of friends, and adds a new user with the given information to the binary search tree. Make sure that the tree that is produced is a binary search tree. You may assume that the user number does not already exist in the given tree.
3. Write a method friendCount which takes a user number and returns the number of friends the person with the given user number has. You may assume the user number exists in the tree. Your function should be written efficiently, such that it performs as few comparisons in the tree as is necessary.
4. Write the main method to test your program
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
/**
*
* @author Sam
*/
public class SocialNetwork {
static Member head; // Root node of the tree
public SocialNetwork() { //initialize empty tree
head = null;
}
public static void main(String[] args) throws IOException {
String userName;
long userNumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Enter user name:(exit to quit) "); //request and read username
userName = br.readLine();
if (userName.equals("exit")) //check exit condition
break;
System.out.println("Enter user number: "); //request and read userNumber
userNumber = Long.parseLong(br.readLine());
Member newMember = new Member(userNumber, userName); //create a new binary tree node
long friendsId;
System.out.println("Enter friends user number:(0 to stop) "); //request and add friends of the member
friendsId = Long.parseLong(br.readLine());
while (friendsId != 0) {
newMember.addFriend(friendsId);
System.out.println("Enter friends user number:(0 to stop) ");
friendsId = Long.parseLong(br.readLine());
}
if (head == null) //if the tree is empty, make new node the head node
head = newMember;
else { //else go down the tree to find a appropiate position
Member tmp = head;
Member parent = null;
while(tmp != null) { //propagate till we get empty position to insert the nnew member node
parent = tmp;
if (tmp.compareTo(newMember) < 0) // go to left sub tree
tmp = tmp.left;
else
tmp = tmp.right; //go to right sub tree
}
if (parent.compareTo(newMember) < 0) //insert as left child
parent.left = newMember;
else
parent.right = newMember; //insert as right child
}
} //insertion done
// now we will search a member
System.out.println("Enter user number to display number of friends:(0 to stop) "); //request members user number
userNumber = Long.parseLong(br.readLine());
while (userNumber != 0) {
Member tmp = head;
while(tmp!=null) { //search the user number down the tree
if (tmp.userNumber == userNumber) / if found print number of friends
System.out.println("Number of friends: " + tmp.friendCount());
if (tmp.compareTo((int) userNumber) < 0) //search left sub tree
tmp = tmp.left;
else
tmp = tmp.right; //search right subtree
}
System.out.println("Enter user number to display number of friends:(0 to stop) "); //accept user number for new search
userNumber = Long.parseLong(br.readLine());
}
}
}
class Member implements Comparable<Member> { //This is similar to a Binary tree node
long userNumber; //this is node data representing user number
String userName; //this is node data representing userName
LinkedList<Long> members; //this is node dara consisting of friends userNumber
Member left; //Left subtree of the node
Member right;//Right subtree of the node
public Member(long userNumber, String userName) { //constructor to initialize username and user number
this.userNumber = userNumber;
this.userName = userName;
members = new LinkedList<>();
}
void addFriend(long x){ //this method add friends to the frieds list
members.add(x);
}
Iterator<Long> getFriends(){ //returns an iterator over friends list
return members.descendingIterator();
}
int friendCount() { //count number of friends this member have
return members.size();
}
@Override
public int compareTo(Member o) { //compare with other member to determine leftchild or right child
return (int)(userNumber - o.userNumber);
}
public int compareTo(int otherUserNumber) { //compare with other userNumber to determine leftchild or right child
return (int)(userNumber - otherUserNumber);
}
}
I tried my best to keep the code as simple as possible. If you have doubt, please feel free to comment below. I shall be glad to help you with the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.