Formula for calculating number of combinations of nodes in a perfect binary tree [closed] - data-structures

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 26 days ago.
Improve this question
I have a perfect binary tree with each set of leaf nodes holding Y/N (yes/no) as shown below:
R
/ \
/ \
/ \
/ \
Y N
/ \ / \
Y N Y N
/ \ / \ / \ / \
Y N Y N Y N Y N
The root node holding the character "R" has no value, it just represents 'root node'. Now if we list the combinations of all the leaf nodes in the top-down approach only, we get following 8 combinations
EDITED:RYYYRYYNRYNYRYNNRNYYRNYNRNNYRNNN
The formula for calculating number of nodes in a perfect binary tree is 2h+1-1. In this case the height of the tree is 3 and so the number of nodes will be 23+1-1 = 15 nodes.
As listed above we get 8 paths using combinations of all nodes traversing from root to each leaf node.
Now I want formula for calculating number of combinations (only in top-down direction) from either the number of nodes or height of a "perfect binary tree".
Thanks in advance,
Surya Praveen

These are the powers of two in a binary tree: 2^n

Ralf Ulrich, thank you very much!! I searched formula for calculating number of leaf nodes of a perfect binary tree. It is either "2h" (h -> height of tree) or "(n + 1)/2" (n -> total nodes in tree). So, for my example for height "3" it will be 23 = 8 or for 15 nodes it will be (15 + 1)/2 = 16/2 = 8.
By the way I also edited my question and added few corrections.

Related

Non-leaf node in B+ tree [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 7 years ago.
Improve this question
Assume a B+ tree with order m. Why is it that the non-leaf node must have at least ceil(m / 2) childs? Or what will happen if this property is not satisfied.
If you don't enforce the half-full rule, you can get (effectively) unbalanced tree, e.g.
[root]
/ \_________________________
/ \
[branch] [branch]
/ \ ____/ | \___
/ \ / | \
[1-record leaf] [1-record leaf] [100-record leaf] [100-record leaf] [100-record leaf]
It's going to take much less time to find the first or second record in this tree than to find any of the other 300 records.
If you let leaves have as few as one record and branches have as few as one key (and therefore two children), I believe the disparity in lookup time can be as bad as log(m) (where m is the order of the tree). Looking up a record along a path of full nodes takes Θ(log(N) * log(m)) time (where N = total number of records, so log(N) is proportional to the height of the tree), while looking up a record along a path of minimally-sized nodes only takes Θ(log(N)) time (because you do exactly one comparison at each branch and none at the leaf).
By enforcing the half-full rule, you ensure that all lookup times are Θ(log(N) * log(m)) and within a constant factor of each other.

Perfect Balancing, linear time [closed]

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.

Maximum Xor subset in a binary 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 9 years ago.
Improve this question
Given a tree find the maximal subset XOR of any path from root to leaf. (i.e. the subset XOR may or may not include all elements from root to leaf. So, two siblings or cousins can't belong to the same subset).
My suggestion is that first consider this problem: maximum xor subset, as defined below:
Given a set of integers S = { a1, a2, a3, ... a|S| }, we define a function X on S as follows:
X(S) = a1 ^ a2 ^ a3 ^ ... ^ a|S|. (^ stands for bitwise 'XOR' or 'exclusive or')
Given a set of N integers, compute the maximum of the X-function over all the subsets of the given starting set.
Suppose the above problem can be solved in T(N), then at least your problem can be solved in O(N * T(h)). (N is number of leafs in your binary tree, and h is the height of your tree.) Probably you can make it even better, after you understand the maximum xor subset problem.
According to my research, Gaussian elimination can be used to tackle the maximum xor subset problem. You may refer to https://math.stackexchange.com/questions/48682/maximization-with-xor-operator.
Good luck!

Minimum numbers of attacks needed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We are given 2 Dimensional grid of cells.Each cell may or may not contain a monster.
We are given a list of cells that contain monsters.
In a single attack we can kill all the monsters standing in a row or in a column. We
need to tell the minimum number of attacks that will be require to destroy all the monsters.
Constraints:
1 ≤ N ≤ 1000
1 ≤ X, Y ≤ 10^9
Example:
Input:
3
0 0
1 0
0 1
Output:
2
How to approach this problem..??
This can be modelled as a graph problem.
Create a graph node for each row and column where there's a monster.
Connect the nodes if a monster is on that row and that column.
This is a bipartite graph, and you want to do minimum vertex cover. König's theorem shows that for bipartite graphs the problem is equivalnt with the maximum matching problem which can be solved in polinomial time:
http://en.wikipedia.org/wiki/Maximum_matching#Maximum_matchings_in_bipartite_graphs
This reminds me the Set Cover Problem:
You have a set U that stores the N monsters: U = {1, 2, ..., N}, a set S of possible attacks. Each attack is as well a set of the indexes of the monster that the attack kills. In your example, S is S = { {1}, {2}, {}, {1}, {2} }. You have to find the smallest set C in S whose union is U.

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.

Resources