Perfect Balancing, linear time [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I rebuild a binary searching tree into perfect balanced in linear time?
I think i'll do rotations to find median, but I'm not sure if its good way.
Thanks for any ideas.

At the very least, you can do it in two steps:
Extract a sorted array from the tree using in-order traversal.
Construct a near-perfect binary tree. For example, by just capping the height with h=log2n where n is the number of elements. You will get only a part of the perfect tree if n is not equal to 2k-1 for some integer k, but the height will still be minimal possible.
Here is an explanatory image for constructing the tree of values 1, 2, 3, ... 10:
8
4 10
2 6 9
1 3 5 7
Alternatively, on step 2, you can put the middle element of the array as root, divide what remains into two equally sized parts, and proceed recursively. An example:
5
2 8
1 3 7 10
4 6 9
Each of the steps can be performed in linear time.

Related

What is the requirement for bubble sort to complete in 1 pass? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am working on a problem where you're given n distinct numbers, and you want to find the number of permutations such that it takes bubble sort at most 1 pass to complete.
e.g., if n=3, then the following permutations would only require 1 pass
1 2 3
1 3 2
3 1 2
2 1 3
But
3 2 1
2 3 1
would require more than 1 pass. Apparently the answer is 2^{n - 1}, but I am not sure how to prove this for the general n case.
My question is, what are the general constraints for a sequence to allow for it to be sorted with 1 pass of bubble sort?
It is difficult for me to come up with a generic formula to generate the permutations for larger n.

alternative peak and valley algorithm confusion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Wondering if anyone have worked on similar problem before? And my question is, why {8, 6} are peaks? I think 8 is peak, but since 6 is smaller than 8, it should not be a peak? Thanks.
In an array of integers, a "peak" is an element which is greater than or equal to the adjacent integers and a "valley" us an element which is less than or equal to the adjacent integers. For example, in the array {5,8,6,2,3,4,6} {8,6} are peaks and {5,2} are valleys. Given an array of integers, sort the array into an alternating sequence of peaks and valleys.
Example,
Input: {5,3,1,2,3}
Output: {5,1,3,2,3}
thanks in advance,
Lin
The 6 referred to is the 2nd 6 at the end of the sequence. This fits the description well (if not made very clear) and is backed up by 5 being a valley.
An alternating sequence of peaks and valleys is a sequence such that either all of the elements in odd-numbered positions are peaks and those in even-numbered positions are valleys, or vice-versa. The output sequence in the example demonstrates the elements of the input sequence sorted in such a way.

Which algorithm is fastest in walking in binary tree to any given node at a any level of tree [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for example:-there are computers connected in a binary tree structure. There are 20 rooms consider each room as a level in the binary tree starting from room 0 (level 0) in which the main computer is placed and all its children nodes (computers) are in room 1 (level 1) and so on. Each computer is numbered according to its place in the room like computer1 , computer 2, computer3 and computer4 (maximum number of computers in room2 is equals to the 2 raised to the power 2)in room 1 (level 1).Now starting from the root if I want to get to the computer756 in room 12 what is the fastest algorithm or method.
considering the above image as an example level 4 has total 16 nodes (1 to 16 say)that is 2 raised to the power 4. If the tree is huge (say tree with 50 levels) which is the fastest algorithm to access the node numbered 1,099,511,628,800 at level 50
If I understand your question correctly, given N and L you want to find a path from the root to the Nth node in the Lth level. I'll assume that the nodes in each level are numbered left-to-right, and that N and L are zero-based.
This is simple if you use the binary representation of N: Represent N as a binary number of L bits. Now go through these bits from most significant to least significant. A 0 means that you need to go to the left child, and a 1 means that you need to go the the right child.
For example, to find node #3 in level #3: The node number in binary is 011. So from the root, you go left, right, right.

Design a sort algorithm with running time O(n lg d) running time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assume that we know in advance that each record in an unsorted array is at distance at most d << n from its position in the sorted array. We would like to take advantage of this property. Assume that all n keys are distinct. For example: Let the list be 3 8 18 2 7 20 24 15 22 30 40. It is not hard to see that for this unsorted list each record is at distance at most 3 from its position in the sorted array.
Design a sort that has O(n lg d) running time.
It is assignment question. Some hints will be useful.
Here's my tip for doing it (I'd post a full solution, but as you say, this is from an assignment):
You already know that an element is within 2d of the correct index. How might you be able to scan through the array, but only looking through at most 2d elements at once?
More specifically, suppose you just figured out the ith element by checking everything from index i - d to i + d. How might you use what you already know to figure out the i+1th element in O(log d) time?

logical problem for finding the probability [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
hi to all
I want to know how to find the solution for this problem. consider in a Jar there are 10 papers numbered 1 to 10. we have to take 2 papers from that. what is the probability that 2 numbers are consecutive numbers.
It's a probability to choose one of 9 consecutive pairs from C(10, 2) possible pairs. Thus its 9 / (10*9 / 2) = 1 /5
The probability of getting 2-8 is 8/10. Once we have a given number in that range, the probability of getting one of the two adjacent numbers is 2/9. 8/10 * 2/9 = 16/90.
The probability of getting either a 1 or a 10 is 2/10. If we have one of those two endpoint numbers, the probability of getting the one adjacent number is 1/9. 2/10 * 1/9 = 2/90.
Adding these together, we have a total probability of 18/90 or 1/5.
Your first choice has ten possible numbers. 8 of those 10 will have two consecutive numbers, while the other two only have 1. Your second choice will be out of nine numbers. So
(8/10 * 2/9) + (2/10 * 1/9) = 9/45 = 1/5

Resources