fishopps
asked on
How to calculate the total sales in the given title??
The title is like this: "A" is the sales manager and he has 2 downline, which is "B" and "C". "B" has 2 downline,which is "D" &"E"...
So..the question is ..I want to know the total sales result of "A" downline, which is B,C,D and E...but can't used for loop because it will be very messy if there are lots of downline...
So..the question is ..I want to know the total sales result of "A" downline, which is B,C,D and E...but can't used for loop because it will be very messy if there are lots of downline...
Is this a homework question
Please try to put some efforts on coding.... and if you face some problems/ errors, then ask us.
Mayank.
Mayank.
What do these guys sell?
>> What do these guys sell?
Does it really matter?
Does it really matter?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think so you can do a pretty neat job,by using a data structure like a linked list or tree.
If you can't use recursion, then you need to keep a stack to keep track of what is "up" from where you are:
node * stack[ 10000 ];
every time you go "down", push your current place on the stack, then add in the node you've gone down to, then try to go down further. ifyou can't go any further down, pop the stack and inspect the next node on that level.
node * stack[ 10000 ];
every time you go "down", push your current place on the stack, then add in the node you've gone down to, then try to go down further. ifyou can't go any further down, pop the stack and inspect the next node on that level.
ASKER
sorry, This is not homework question!
This is actually a tough question, which need expertise to think about it...coz usually a programmer will use for loop to do it...but I dont want a for loop coz this it is tree...
This is actually a tough question, which need expertise to think about it...coz usually a programmer will use for loop to do it...but I dont want a for loop coz this it is tree...
ASKER
ok..now if the question changed...there is a lot of level in the tree..and you do not know how many level there are...
so, wherever I point to the person..I want to know all of his/her downline's sales result..
remember no for loop!
so, wherever I point to the person..I want to know all of his/her downline's sales result..
remember no for loop!
ASKER
Thanks a lot grg99 and locil...I need sometime to do the testing...
ASKER
kocil...you suggest to use for loop right?
But if there are lots of subordinates then I have to do lots of for loop???!!
But if there are lots of subordinates then I have to do lots of for loop???!!
Yes man, you need recursive only for down to next level.
For direct subordinates, which are in the same level, use loop.
Please notice that I assumed the downline array stores the direct subordinates and terminated by NULL.
For direct subordinates, which are in the same level, use loop.
Please notice that I assumed the downline array stores the direct subordinates and terminated by NULL.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hope the above code is of some help to you
i have assumed each person has 2 people or nobody under him
i have assumed each person has 2 people or nobody under him
ASKER
Thanks vamshi pavan the code helps a lot,I will need sometime to verify it and is there any other way to do the search??
coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...
And please do me a favor--add 2 more functions which is delete and modify employee info...
anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!
coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...
And please do me a favor--add 2 more functions which is delete and modify employee info...
anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!
ASKER
Thanks vamshi pavan the code helps a lot,I will need sometime to verify it and is there any other way to do the search??
coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...
And please do me a favor--add 2 more functions which is delete and modify employee info...
anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!
coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...
And please do me a favor--add 2 more functions which is delete and modify employee info...
anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!
ASKER
Sorry for asking so much...I will raise the point if I get a really satisfy answer.
ASKER
*For the people who do not know what I am asking for:
I want to know the most efficient way to do data search/add/delete and modify in a really huge size of database, which is in the form of tree.
for example: I want to know total sales of all the people below John and you do not know which level/node he is currently located.
obviously we can use for loop, start from first node to search for John until last node and then display the totsales of all the member below him. But what if the amount of database is unlimited...
I want to know the most efficient way to do data search/add/delete and modify in a really huge size of database, which is in the form of tree.
for example: I want to know total sales of all the people below John and you do not know which level/node he is currently located.
obviously we can use for loop, start from first node to search for John until last node and then display the totsales of all the member below him. But what if the amount of database is unlimited...
If you use a binary tree(like my above code) each employee has 2 or nobody under him.
If you use a doubly linked list (it is much easier than tree) each employee will have 1 or nobody under him.
If you use a doubly linked list (it is much easier than tree) each employee will have 1 or nobody under him.
sorry fishopps,
i won't be able to help you regarding those 2 functions
i won't be able to help you regarding those 2 functions
If you're using a binary tree to hold the data, and the sum at a node will be equal to the total of the sums of the left and right subtrees of this node, and the value at that node node):
The structure is something like:
typedef struct node
{
struct node * left ;
int data ; // holds the value to be summed
struct node * right ;
} tree ;
int total ( tree * head ) // 'head' is a pointer pointing to the node at which the sum is to be calculated
{
tree * stack[100] ;
int top = -1, sum = 0 ;
if ( head == NULL )
return 0 ; // end if
stack[++top] = head ;
do
{
if ( head != NULL )
{
head = head -> left ;
if ( head != NULL )
stack[++top] = head ; // end nested if
} // end if
if ( head == NULL )
{
head = stack[top--] ;
sum += head -> data ;
head = head -> right ;
if ( head != NULL )
stack[++top] = head ; // end nested if
} // end if
} while ( top != -1 ) ; // end do-while
return sum ;
} // end of total ()
Hope that helps! Please do ask if you face any problems.
Mayank.
The structure is something like:
typedef struct node
{
struct node * left ;
int data ; // holds the value to be summed
struct node * right ;
} tree ;
int total ( tree * head ) // 'head' is a pointer pointing to the node at which the sum is to be calculated
{
tree * stack[100] ;
int top = -1, sum = 0 ;
if ( head == NULL )
return 0 ; // end if
stack[++top] = head ;
do
{
if ( head != NULL )
{
head = head -> left ;
if ( head != NULL )
stack[++top] = head ; // end nested if
} // end if
if ( head == NULL )
{
head = stack[top--] ;
sum += head -> data ;
head = head -> right ;
if ( head != NULL )
stack[++top] = head ; // end nested if
} // end if
} while ( top != -1 ) ; // end do-while
return sum ;
} // end of total ()
Hope that helps! Please do ask if you face any problems.
Mayank.
How about rating it now? If you are having some more problems, then please ask..
Mayank.
Mayank.
ASKER
Please give me some time...
How much more time, buddy??
Nothing has happened on this question in more than 6 months. It's time for cleanup!
My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between grg99, kocil, and vamshi_pavan (partial answers).
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between grg99, kocil, and vamshi_pavan (partial answers).
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
Actually, fishopps said: >> I want to know total sales of all the people below John and you do not know which level/node he is currently located.
And vamshi_pavan said: >> sorry fishopps, i won't be able to help you regarding those 2 functions
If you look at mayankeagle's code, it pretty much accomplishes the task of searching through the tree at any node and summing up the values beneath. But anyways, I guess I am posting this comment a little late.
Mayank.
And vamshi_pavan said: >> sorry fishopps, i won't be able to help you regarding those 2 functions
If you look at mayankeagle's code, it pretty much accomplishes the task of searching through the tree at any node and summing up the values beneath. But anyways, I guess I am posting this comment a little late.
Mayank.