
With respect to this tree,
 What is the correct inorder traversal?
 U S T X C P Y R B A I G J F N H V T E D L
 U S T X C P Y R B A D E I G J F N H V T L
 What is the correct postorder traversal?
 U T S X P R Y C B D I J G N V T H F E L A
 U T S X P R Y C B I J G N V T H F E D L A
I evaluated both pairs. But some are saying 11 and 21 are correct, while others say 12 and 22 are correct. I'm confused. Which ones are actually correct?
modified 20May21 12:22pm.





12 and 22, maybe. I'll explain the reasoning, and why there is a "maybe".
Let's do the postorder one first, that's easier. OK so both answer start with "U T S X P R Y C B", so let's say we've just gone up out of B, and are looking at A right now (not "visiting" it yet though), so we recurse into L, which recurses into D, into E, into F, into G, into I, and then we come back up, so after B comes I, not D.
Or to put it in a different way: in a postorder traversal, a node is visited only after all its descendants, so there is no way D could be in the middle of this sequence, it has to be near the end. Only L and A could ever go after it.
The inorder question has two answers that start with "U S T X C P Y R B A", so let's say we're at A again, recursing into L, D, D's left child (null), then D itself, so it's the second answer. But here's why I added "maybe": "U S T X C P Y R B A" is not even the right sequence to start with, because it puts B after C but C is the right child of B so B should been visited before C. Both answers are wrong. But the first answer is worse: it gets more of the sequence wrong.





Thanks for your reply.
Below is my takeaway from your post.
Postorder traversal
Or to put it in a different way: in a postorder traversal, a node is visited only after all its descendants
Your statement quoted above was enough to clarify me about the postorder traversal. I believe, that makes the following series as the correct answer: U T S X P R Y C B I J G N V T H F E D L A
Inorder traversal
According to your explanation, while traversal, if any node has a null left child, that node has to be visited right away as the immediate root node before moving down further. If I'm right, then, I think the following series is the correct answer: B U S T X C P Y R A D E I G J F N H V T L





priyamtheone wrote: B U S T X C P Y R A D E I G J F N H V T L Yes this looks correct to me





I'm working on a UI which has a bunch of titles grouped under multiple categories(let's say list of movies). Let's say I have Category 1, Category 2, Category 3 etc., with each category having 20 movies under it. The problem is that some movies are repeated across categories and if they appear in the top 5 in multiple categories, then it looks bad in the UI, since it appears duplicated. The source of this categorization is out of my control and I'm just receiving this data from another service. My requirement is to look for duplicates across categories and if a movie already appeared in a higher ranked(based on order of display) category, penalise it and rank it down in the category. e.g., Movie1 appeared at Rank2 in Category1. Movie1 also appears at Rank4 in Category2. I want to move Movie1 to a much lower rank(say Rank15 or Rank20) in Category2 since it is already appearing at a higher rank in a previous category.
Problem:

Category1:
1. Movie1
2. Movie2
3. Movie3
...
Category2:
1. Movie1
2. Movie4
3. Movie5
...
Category3:
1. Movie4
2. Movie6
3. Movie7
...
In the case above, I want to rank down Movie1 in Category2 to rank 10 or below since it already appears in Category1 at the same rank or above. Similarly, I want to rank down Movie4 in Category3 to be ranked down since it appears in a higher category at a higher rank.
I was thinking of assigning weights(in descending order) to categories and then calculating the score of each movie based on the rank+weight and then use this to rank down the repeating movies. I'm not an expert in algorithms, ranking etc, but I feel that this would be a common use case and there might already be solutions to such ranking problems. Can anyone guide me here?





Since no one has answered yet I write my thoughts to your issue ...
Generally an algorithm is a rule which defines how to work something.
When I read your text I'm unable to detect such a rule.
For example you write "bring it to a lower rank (say 15 or 20)". Which of it ? 15 or 20 ? Or something in between ?
You have to define something which allows a kind of calculation ... and sorry ... I don't find it here.
Better will be :
 do it like you do it allready
or :
 if Movie1 appears in Category1 on the highest position) it must not appear in any other Category





I have database table object which is:
[Table("TreeViewDb")]
public class TreeViewDb
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
}
And I have a view model whcih is :
<pre>public class TreeView
{
public TreeView()
{
Children = new List<TreeView>();
}
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public List<TreeView> Children { get; set; }
}
Now I need to save TreeView to the database. During save children or children under children to the nth Level. But my below method only goes to level 3. How can I go to nth Level to save child and parent objects with recursive way?
public bool SaveOrUpdateTreeView(TreeView viewModel)
{
var parentModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = viewModel.ParentId,
Name = viewModel.Name
};
var parentId = _dataRepository.SaveOrUpdateTreeView(parentModel);
foreach (var child in viewModel.Children)
{
var childModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = parentId,
Name = viewModel.Name
};
var childId = _dataRepository.SaveOrUpdateTreeView(childModel);
foreach (var grandChild in child.Children)
{
var grandChildModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = childId,
Name = viewModel.Name
};
_dataRepository.SaveOrUpdateTreeView(grandChildModel);
}
return true;
}
return true;
}





Use recursion. Better (easier) yet, serialize the whole tree (object graph) to XML and save the result as a string in the "database".
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food





A[0..n − 1] is an array of n distinct numbers. A pair of array elements (A[i], A[j]) is
called an inversion if A[i] > A[j] for i < j.
1.1 Design a brute force algorithm to count the number of inversions in an array, analyze the
number of executions of its basic operation, and determine the efficiency class.
1.2 Design a recursive divideandconquer algorithm of Θ(n log n) to count the number of
inversions in an array, set up a recurrence to analyze the number of executions of its basic
operation of the best case, and determine the efficiency class. Use the Master Theorem
to verify the efficiency class in your analysis result.
Anyone help me in finding the solution of this question.





It would help if you would post where you are stuck. It's unlikely anyone is going to do it all for you.





Show your work and explain where you are stuck.
The 1.1 should be rather easy.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein





In a card game involving 24 computer players, does an algorithm exist for determining which of the player(s) has the highest card (e.g., 2 < 3 <...< K < A)? With two players, it's simply:
if (player1 > player2)
player1 wins
else if (player2 > player1)
player2 wins
else
tie But that does not scale well at all for 3 or 4 players. When I tried something for 3 players, it was ugly and nearly unmanageable. 4 players was not even attempted.
Thanks.
DC
"One man's wage rise is another man's price increase."  Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it."  Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him."  James D. Miles





It sounds for me you need something like a sorting algorhythm ...
Now you can sort the Players according to their points ...





You don't need a full sort. It would work, but it's overkill.
Essentially you have an array (or list, or collection of some sort) of card values, indexed by player.
The standard lineartime algorithm for finding the max or min of an array and remembering where you saw it...
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





Very roughly in pseudocode for traditional languages (no particular language)
nPlayers = 7;
int cardForPlayer[nPlayers] = (cards for each of the players);
int bestPlayerNo = 1;
int bestScore = cardForPlayer[0];
for (int playerIndex = 1; playerIndex++; playerIndex < nPlayers)
if (cardForPlayer[playerIndex] > bestScore)
{
bestScore = cardForPlayer[playerIndex];
bestPlayerNo = playerIndex + 1;
}
For a simple functional style language (no particular language)
cardForPlayer = List p1card, p2card, ..., p7card
bestScore = max cardForPlayer
bestPlayerNo = (FindIndex cardForPlayer bestScore) + 1





Give OP's seniority and rep, I didn't condescend to spelling out the algorithm.
I've lost track of the number of times and different languages I've implemented it...
There is a useful variant where you start with a sentinel value ("minus infinity" or whatever) and compare every one including the first.
This handles the case where there is no item that qualifies for selection.
In this example, it would be easy to skip a player if he has no card. What if nobody had any cards?
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





You have a "list" of players.
The list is always processed in order; so the "first" element in the list should be the the player that plays first, etc.
After the last player, you cycle back to the first. A "pass" just bumps the player index.
I wrote a GO game on that basis; for 2 to 7 players; humans and bots. 7 was an arbitrary limit (The Warring States).
The "top" player is the one with the .Max something; unless it's tied.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food





Suppose your operations on arrays were constrained as follows:
 .get(i) accesses the element at index position 𝑖 in readonly fashion.
 .length() returns the length of the array.
 .flip(i) flips the array up to index position 𝑖. Thus, the {1,2,3,4}.flip(2) operation changes the array to {3,2,1,4}.
Only these three operations are available to you. Also assume that the three operations are in 𝑂(1).
Develop an algorithm that sorts a given array in 𝑂(𝑛 × 𝑙𝑜𝑔 𝑛).
Suppose you have an array of length 𝑖 + 1. You want to correctly sort the element at position 𝑖. The array is already sorted up to 𝑖  1. The correct position of the element at position 𝑖 is index position 𝑗. Find a sequence of flip operations that inserts the element in the correct location.
Problem: I have no idea how to begin
modified 14Dec20 13:06pm.





If you can figure out the last paragraph, you'll have a way to move an element into the correct position when it's out of place. That element might only be part way down the array, in which case you can move it up by only doing flips as far as that element, leaving the elements after it alone.
Start with an actual example, say 1235674. You want to move 4 up into the position currently occupied by 5. Clearly, 4 must be included in the first flip, else it'll never move. So flip(6) is the first operation, which yields 4765321. Now the ball is in your court.





They don't say to flip one way or another. Looks more like a rotate. Poor spec.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food






Read on the lines of bubble sort. That should help solving this





Could help me solve this problem pls?
The job is to calculate the degree of discontent of passengers on a bus.
Passengers get on the bus, all at the same point and say at which point they want to stop (that point is the mileage from the point where they get on the bus to where they want to disembark), but the bus can only make k stops, that is, it is a value limited number of stops. The discontent will be calculated as follows (xy) * 2 where x is the place that each passenger chose to stay and y where the bus stopped. The K will be defined after calculating the discontent of each passenger





1. Nobody here is going to do your homework for you.
2. That's a maths problem, not a programming problem.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
 Homer





