Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help. Write codes in C++. Part 1 Create an Organizational Structure . Part 2 Traversing Trees. Thank you very much 3 Problems 3.1 Creating

Please help. Write codes in C++. Part 1 Create an Organizational Structure . Part 2 Traversing Trees. Thank you very much

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

3 Problems 3.1 Creating an Organizational Structure In this exercise, we will implement a basic version of the organizational tree. Let's get started: 1. First, create the file Tree.h and Tree.cpp and include the required headers. Be sure to include the proper default constructor and destructor. #include #include 2. For simplicity, we will assume that any person can have, at most, two subordinates. We will see that this is not difficult to extend to resemble real-life situations. This kind of tree is also known as a binary tree. Here we write a basic structure for that: struct Node std::string position; node *first, *second; }; As we can see, any node will have two links to other nodes - both of their subordinates. By doing this, we can show the recursive structure of the data. We are only storing the position at the moment, but we can easily extend this to include a name at that position or even a whole struct comprising all the information about the person in that position. 3. We do not want end users to deal with this kind of raw data structure. So, wrap this in a nice class interface called OrgTree: class OrgTree public: Node *root; //Constructor //Destructor }; 4. Now, let's add a function to create the root, starting with the highest commanding officer of the company. This function should create an OryTree object and instantiate the root node of type Node. Then return the OrgTree object. 5. OrgTree createorgStructure (const std::string& pos); This is a function just to create the tree. Now, let's see how we can extend the tree. 6. Now, we want to add a subordinate of an employee. The function should take two parameters, the name of the already existing employee in the tree and the name of the new employee to be added as a subordinate. But before that, let's write another function that will help us find a particular node based on a value to make our insertion function easier. Given the root and the value implement a binary recursive call to the find function to search the tree for the value. See the pseudo code below Node* find (Node* root, const std::string& value) { if root is null return NULL if position is equal to value return root find (first, value) if firstFound is not NULL return firstFound return find (second, value) While we are traversing the tree in search of an element, either the element will be the node we are at, or it will be in either of the right or left subtrees. Hence, we need to check the root node first. If it is not the desired node, we will try to find it in the left subtree. Finally, if we have not succeeded in doing that, we will look at the right subtree. 7. Now, we can implement the insertion function. We will make use of the find function in order to reuse the code: bool addSubordinate (const std::string& manager, const std::string& subordinate) As we can see, the function returns a Boolean, indicating whether we can insert the node successfully or not. 8. Create main.cpp. Now, use this code to create a tree in the main function: int main() OrgTree* a Tree = new OrgTree(); auto tree = aTree->createOrgStructure ("CEO"); if (tree.addSubordinate("CEO", "Deputy Director")) std::cout q; q.push(start); while (!q. empty()) int size = q.size(); for (int i = 0; i position ", if (current->first) q.push(current->first); if (current->second) q.push(current->second); }//End For. cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visualizing Health And Healthcare Data Creating Clear And Compelling Visualizations To See How Youre Doing

Authors: Katherine Rowell ,Lindsay Betzendahl ,Cambria Brown

1st Edition

1119680883, 978-1119680888

More Books

Students also viewed these Databases questions

Question

1. What is meant by Latitudes? 2. What is cartography ?

Answered: 1 week ago

Question

What is order of reaction? Explain with example?

Answered: 1 week ago