Question

WRITE A JAVA CODE

Create a communication information system for a company with using Red-Black Tree Method. In this system the users are allowed to search an employee from employee's registration number(id) and retreive the department number(id) and that employee's internal phone number. Also, if the employee they are looking for is not in the tree, they can add it. (in accordance with the red-black tree properties)

a) Implement the Employee class:

- Registration number (an integer)

- Department ID (an integer)

- Internal Phone Number (an integer) - all other required procedures.

b) Implement a Red-Black Tree. You will store your employee objects in a Red-Black Tree. In your Red-Black Tree implementation you can directly use the header file that we shared with you on the moodle (RedBlackTree.h) or you can write yourself. This tree will have:

- insert function: to insert an employee object into the Red-Black Tree. (in accordance with the red- black tree properties)

- search function: to find a employee in the tree

c) Implement a main function. In the main function:

- Instantiate your Red-Black Tree object.

- Instantiate and insert the following employee objects into the tree:

- Registration number : 1001, Department id : 51, Phone number: 6467 - Registration number : 1002, Department id : 43, Phone number: 2359 - Registration number : 1010, Department id : 34, Phone number: 4342 - Registration number : 1008, Department id : 21, Phone number: 6761 - Registration number : 2001, Department id : 45, Phone number: 2345 - Registration number : 2006, Department id : 23, Phone number: 6862

SAMPLE RUN (after Red-Black Tree is created and filled with employee objects): Enter Employee’s Registration Number : 1001

Department id is : 51. Internal phone number is : 6467

Continue (y/n): y

Enter Employee’s Registration Number : 2008

Employee not found !

Do you want to add this employee to the system ? : y

What is the Department id and Internal phone number? : 63 8469

Added succesfully.

Continue (y/n): y

Enter Employee’s Registration Number : 2008

Department id is : 63. Internal phone number is : 8469

Continue (y/n): n

Bye (Note : Those written in bold are entered by the user.

Write with using JAVA


0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER :

C++ code :

#include<iostream>
using namespace std;
class Employee{
public:
Employee *left,*right,*par; //left-child,right-child,parent-node
int reg,dept,phone; //variables to store the detials of the employee
char color; //to give color to the node

Employee(int x,int y,int z,Employee*l=0,Employee*r=0,Employee*pa=0,char col='r')
{
reg=x;
dept=y;
phone=z;
left=l;
right=r;
par=pa;
color=col;
}
};
class RBT{

public:
Employee *root;
Employee *nil;

RBT() //constructor
{
nil=new Employee(0,0,0);
nil->color='b';
root=nil;
}

void Insert(int x,int y,int z) //Inserting the elements in the tree
{
Employee *p,*prev=nil,*q;

if(root==nil)
{
p=new Employee(x,y,z,nil,nil,nil,'b');
root=p;
}
else{
p=new Employee(x,y,z,nil,nil,nil,'r');
q=root;
while(q!=nil)
{ prev=q;
if(x<q->reg)
q=q->left;
else
q=q->right;
}
p->par=prev;
if(x<prev->reg)
{
prev->left=p;
// p->par=prev;
}
else{
prev->right=p;
//p->par=prev;
}
}
Insert_fixup(p);
}

void Insert_fixup(Employee *t) //fixing up the tree after insertion of new elements by rotating the tree
{
Employee *u;
if(root==t)
{
t->color='b';
return;
}
while(t->par!=nil&&t->par->color=='r')
{
Employee *g=t->par->par;
if(g->left==t->par)
{
if(g->right!=nil)
{
u=g->right;
if(u->color=='r')
{
t->par->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->par->right==t)
{
t=t->par;
left_rotate(t);
}
t->par->color='b';
g->color='r';
right_rotate(g);
}
}
else
{
if(g->left!=nil)
{
u=g->left;
if(u->color=='r')
{
t->par->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->par->left==t)
{
t=t->par;
right_rotate(t);
}
t->par->color='b';
g->color='r';
left_rotate(g);
}
}
root->color='b';
}
}
void left_rotate(Employee *x) //to adjust tree due to new entries by ritating left
{
Employee *y;
y= x->right;
x->right=y->left;
if(y->left!=nil)
y->left->par=x;
y->par= x->par;
if(x->par==nil)
root =y;
else if (x==x->par->left)
x->par->left=y;
else
x->par->right = y;
y->left=x;
x->par=y;
}
void right_rotate(Employee *x) //to adjust tree due to new entries by rotating right
{
Employee *y;
y= x->left;
x->left=y->right;
if(y->right!=nil)
y->right->par=x;
y->par= x->par;
if(x->par==nil)
root =y;
else if (x==x->par->right)
x->par->right=y;
else
x->par->left = y;
y->right=x;
x->par=y;
}
Employee* search(int x) //to serach for an employee
{
int f=0;
Employee*p=root;
while(p!=0)
{
if(x==p->reg)
{
f=1;
break;
}
else if(x<p->reg)
{
p=p->left;
}
else
{
p=p->right;
}
}
if(f==1)
return p;
}
void display(Employee *t) //to display employee
{
if(t!=nil)
{
cout<<"\nDepartment Id of Employee is: "<<t->dept;
cout<<"\nInternal Phone number of Employee: "<<t->phone;
}
}
void menu()
{
char ch;
Insert(1001,51,6467); //filling the tree with details
Insert(1002,43,2359);
Insert(1010,34,4342);
Insert(1008,21,6761);
Insert(2001,45,2345);
Insert(2006,23,6862);
do
{
int q;
cout<<"Enter Employee's Registration Number: ";
cin>>q;
if(search(q)) //search for an employee
{
cout<<"Employee Found!";
display(search(q));
}
else
{
char a;
cout<<"Employee not Found!";
cout<<"\nDo you want to add this employee to the system? ";
cin>>a;
if(a=='y')
{
int d,p;
cout<<"What is the Deprtment Id and Internal Phone number?: ";
cin>>d; //enter department
cin>>p; //enter phone
Insert(q,d,p); //insert this in the tree
cout<<"Added Successfully.";
}
}
cout<<"\nContinue?(y/n): ";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"bye";
}
};
int main()
{
RBT rbt;
rbt.menu();
return(0);
}

Output :

input Enter Employees Registration Number: 1001 Employee Found! Department Id of Employee is: 51 Internal Phone number of Em


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
WRITE A JAVA CODE
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a communication information system for a company with using Red-Black Tree Method.

    PLEASE USE THE HEADER FILE THAT I ADDED TO THE END OF THE QUESTIONWrite this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it....

  • Create a communication information system for a company with using Red-Black Tree Method

    • Write this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it. (in accordance with the red-blacktree properties)a) Implement the Employee class:- Registration number...

  • java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll...

    java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: • An empty string is given for the employee’s name. • An invalid value is given for the employee’s ID number. If you implemented this field as a string, then an empty string would be invalid. If you implemented this field as a numeric variable, then a negative number or...

  • Write a java code : Student ID at University is composed of year of admission and...

    Write a java code : Student ID at University is composed of year of admission and students number. we aim to implement a structure that improves operations of inserting and searching for a student. To enhance these operations, we will build a tree of linked lists (TreeOfLists) to combine advantages of both structures. Each node in this tree contains a linked list of all students who were admitted in that year. Each node in the linked list represents a student(id,...

  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • C++ Program The Ward Bus Manufacturing Company has recently hired you to help them convert their...

    C++ Program The Ward Bus Manufacturing Company has recently hired you to help them convert their manual payroll system to a computer-based system. Write a program to produce a 1-week payroll report for only one employee to serve as a prototype (model) for the administration to review. Input for the system will be the employee’s 4-digit ID number, the employee’s name, hours worked that week, and the employee’s hourly pay rate. Output should consist of the employee’s ID number, the...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • Please code in Java and ignore question 2, thank you! 1. Write a Java method that...

    Please code in Java and ignore question 2, thank you! 1. Write a Java method that takes as its only input parameter a single integer, n, and returns the number of unique binary search trees that can be constructed with integers 1 through n. (Hint: You'll probably want to do this recursively.) (Note: This is not asking for the number of maximally tall BSTs. The BSTs can be any height.) 2. Work through the deletion exercises in 2-4-deletion-supplementary.pdf (attached above)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT