Question

Write a function in JAVA that takes an organizational tree (CEO, C-suite execs, their subordinates, etc)...

  • Write a function in JAVA that takes an organizational tree (CEO, C-suite execs, their subordinates, etc) and prints the members in descending order of rank. Please explain your thought behind the function.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.List;

public class OrganisationTree {

   List<Member> members;
   int length = 0;

   public OrganisationTree() {
       members = new ArrayList<>();
   }

   private static void printMembers(OrganisationTree arr, int start, int end) {
       if (start > end)
           return;

       // print left subtree
       printMembers(arr, start * 2 + 1, end);

       // print root
       System.out.println(arr.getMember(start).name + " ");

       // print right subtree
       printMembers(arr, start * 2 + 2, end);
   }

   static class Member {
       String name;

       Member(String name) {
           this.name = name;
       }
   }

   public void addMember(Member m) {
       members.add(m);
   }

   public Member getMember(int i) {
       return members.get(i);
   }

   public int size() {
       return members.size();
   }

   // driver program to test above function
   public static void main(String[] args) {

       OrganisationTree tree = new OrganisationTree();
       Member ceo = new Member("Sam");
       Member exec1 = new Member("Rihan");
       Member exec2 = new Member("Rock");
       Member subordinate1 = new Member("Clinton");
       Member subrodinate2 = new Member("Tom");

       tree.addMember(subordinate1);
       tree.addMember(exec1);
       tree.addMember(subrodinate2);
       tree.addMember(ceo);
       tree.addMember(exec2);

      
       System.out.println("Members in descending order of their ranks : ");
       printMembers(tree, 0, tree.size() - 1);
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a function in JAVA that takes an organizational tree (CEO, C-suite execs, their subordinates, etc)...
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
  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • Write a function in Java Script that accepts a string. The function takes the last digit...

    Write a function in Java Script that accepts a string. The function takes the last digit of the ASCII code and returns a string of digits sorted in ascending order E.G. function("zDbc") returns “2,8,8,9” (z => 122, D=>68, b=>98, c=>99) ....i am not allowed to use any of the built-in functions

  • answer in java Given a Binary Search Tree containing integer values, write a method to print...

    answer in java Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code

  • 7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and...

    7) Recursion Write a recursive function max_tuple(a tree which takes a tree as a parameter and returns the max values of the right and left subtree. You can assume that the parameter is a full binary tree (in a pyramid shape like with more than 3 nodes. Your solution should not have any iteration. You can use a helper function if needed. Your code should be indented correctly. The expected return value of max_tuple(tree) for the tree below should be(...

  • Using the programming language Java or C++, Write a function that takes an integer as input....

    Using the programming language Java or C++, Write a function that takes an integer as input. Return true if this integer is a palindrome integer; false otherwise;

  • help please! due tomorrow! Write code in Java Write a program that takes as input an...

    help please! due tomorrow! Write code in Java Write a program that takes as input an unordered list of integers, creates a Btree of minimum degree t 4 and then outputs the sorted list of integers. A simple inorder traversal of the B tree will output the list of the integers in a increasing order. You can choose your programming language and the platform you run on. The documentation is required for any programming assignment.

  • Using C++, Java, or C# write a generic function/method that takes in 2 arguments of any...

    Using C++, Java, or C# write a generic function/method that takes in 2 arguments of any numeric type, adds those 2 arguments together and returns the result.

  • Write a python function that takes a binary tree as its only parameter. Return the value...

    Write a python function that takes a binary tree as its only parameter. Return the value stored in the shallowest leaf, anywhere in the tree. If there is a tie, then return the "leftmost" of the leaves (that is, the one that you would encounter first in an in-order traversal). You are allowed (and encouraged) to use a helper function. You may assume that the tree nodes have public fields left,right. The code cannot import anything from the Python standard...

  • Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting...

    Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting of triangles of increasing sizes, filled with a character (' oror '$' etc). Your program should consist of three functions: 1. A function print_shifted_triangle (n, m, symbol). It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin. For example, if we call print_shifted_triangle (3, 4, +"), the expected output is: +++ Left margin +++++ 4 spaces 2. A...

  • Code in C++ please! Write a function that takes an array of integers as an input...

    Code in C++ please! Write a function that takes an array of integers as an input parameter (the address of the first value of the array). It returns nothing. It prints out the array as a single line, with commas between each number, and when the array is finished being printed, it prints an endl; so that we flush the buffer and move to a new line. (I’m having you write this function because you’ll be wanting to print out...

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