How many leaves are in a given heap and its level - data-structures

I have a question and it goes like this:
There is a Max heap binary tree.
Let's assume that in the Heap there are (2^2017)-2017 nodes at the bottom-most level.
A)How many levels are there in the heap?
B)What are the number of the Leaves in the heap?
Thanks

A full binary heap that has 2x nodes at the bottom-most level has (x+1) levels. Consider this heap:
1
2 3
4 5 6 7
It has 4 (22) nodes at the bottom level, and 3 levels.
If the bottom level is full, then the number of leaf nodes is the same as the number of nodes on the bottom level.
In your case, the bottom level has (22017)-2017 leaf nodes. We know that the maximum number of nodes possible on this level is 22017 (because maximum is always a power of 2). And we also know, from the example above, that there are 2018 levels in the tree.
The number of leaf nodes, however, is not (22017)-2017. Consider this heap:
1
2 3
4 5 6 7
8 9
It has two leaf nodes at the bottom level, and three leaf nodes at the level above the bottom level (5, 6, and 7).
I think you'll find that in your case, the number of leaf nodes is (22017)-2017 + 2017/2. The 2017/2 is the number of leaf nodes on the level above.

Related

How to count the maximum number of times any node has been visited while traveling through a tree several times?

We travel through a given tree (not binary) several times. How do we calculate the most number of times any node in the tree has been visited?
For example: in the tree:
1
/ \
2 3
/ \
4 5
Suppose we are told to travel 2 times, from 2 to 3, then 5 to 3. The travel paths will be (2->1->3 and 5->3). The maximum number of times a node has been visited is 2 (the node is 3). All travels are independent from each other. A given travel starts from a given node A and ends at B.
How to efficiently travel (if we even need to) in order to calculate that, considering that we have over 50,000 nodes and 75,000 paths to cover (like 2 to 3 and 3 to 4 in the example)?
Based on what you are saying, the answer is the amount of children that node has...
Also in your example, going off what you have said, both 1 and 3 are visited the most.
In your example each node is only going to get visited once. The only way you could get multiple visits to one node would be with a tree like:
1 3
\ /
2
Edit:: the most efficient way of traversing is if you have a perfect binary tre
4
2 6
1 3 5 7
Where the max depth is number of ((log base 2 of (number of nodes + 1)) + 1) rounded down
Why not store the travel count of each node separately?
Maintain a HashMap<Node, long> keeping track of how many times each node has been visited.
Then maintain a TreeMap<long, List<Node>> that is keyed on count and contains the list of node whose count it is representing.
This way, the TreeMap's first would contain all the nodes that have the highest count, because there can definitely be more than one node with that highest visit count.
All you now need to do is add bookkeeping code for properly updating the two maps whenever a node is visited as part of a tree traversal.
There's an XY problem here.
Your question states you want to store number of node visits. What you really want though is an efficient traversal strategy.
You have options here. Since the edges are bi-directional the best strategy IMO would be a bi-directional search.
But the search strategy itself is a toss up.
Consider a slightly more elaborate tree as
1 -> 2,3,4; 2 -> 5,6,7; 3->8,9; 4->10,11; 10 -> 12,13. If you have an efficient path from 5 to 4 It doesn't mean you can just start from there to find an efficient path from 5 to 13 because you don;t 13 comes under 4 unless you've already found an efficient path from 4 to 13.
So I would suggest memoizing your traversals in a dictionary of the form <Node Pair>: [Traversal list]
Where you start at a target node and perform a Breadth first search. and each time you visit a node, examine your memoization structure if there exists a an entry for <curnode,targetnode> in the dictionary. If there exists an entry, you are done. If not proceed to the current node's sibling or child.
CAVEAT: THIS IS UNDER THE ASSUMPTION THAT ALL NODES HAVE ONLY 1 PARENT AND CYCLES DON'T HAPPEN
I think people are miss understanding the question. He/she wants to ask which node is visited max number of times given x number of travel. So from his tree edges are {(2-1)(1-3)(3-4),(3-5)} now for example we are traveling in following paths{(1,5), (2,4), (1,3), (1,2), (1,3), (4,5)}. So this example 3 visited 5 times, 1 visited 4 times and so on.
Since it is a tree there is only one path exist from one node to another. Find paths for all combination of nodes in DP way and store it.
Then count for each visit. I know there is more efficient way for counting but cannot think of it right now.

Iterations to delete node and its children in a binary tree

Example Tree image
Given a binary tree and pointer to a node (which is present) in tree, assume we have parent pointers. I have to find the number of iterations to delete the adjacent nodes and subsequent adjacent nodes of that deleted nodes. Here deleting means setting some flag, node -> burn. I wont delete the node.
Example :
1
/ \
2 3
/ \ / \
4 5 6 7
/ /
8 9
/
10
and given node in tree is 4,
The burned nodes in each iteration
In iteration 1:
For node 4: 4,8,2 (8 is the child node, 2 is parent of 4 , these are the adjacent nodes of 4).
In iteration 2:
For node 8: 10 will be burned. (4 is already burned)
For node 2: 5 and 1 will be burned.
This continues... , hence i have to find the number of iterations required to burn all nodes.
Not efficient but I think it will work:
Maintain a queue for the next nodes to be considered for deletion. And before deleting a node's adjacent nodes check whether they already have burn flag set.
Also, every time when you remove an item from queue:
check if all the nodes are burned or not (for this you can maintain a
Hash for faster access)
increment iteration count(which will be your result)

How to build binary tree knowing only which nodes are connected?

I have to build a binary tree but i don't know which node is parent, left child, or right child. I only know which nodes are connected. Example: for input like this:
6 4
5 7
9 7
1 5
10 4
3 4
2 6
7 8
5 6
(from 1 there is always one path) the tree should looks like that:
One the input i have also given number of nodes. Any ideas, tips?
From the list of the edges, one can easily create a tree.
You can find which are the leaf nodes, just search which nodes are appearing only in one edge.
But, it is not possible to know which of the leaf nodes is the head of the tree.
In a tree structure it is possible to choose any leaf, choose it as the head, and reorder the tree, and it'll be a valid tree.
There is also the issue of tree isomorphism, you can swap the right and left sub-trees to get a valid tree.
To summarize, from this list you can get 6 heads and in each 4 possible swaps, so in total 24 different valid trees.

binary search tree diagram for numbers 1 to 10

How would i draw a binary search diagram for numbers 1 to 10?
Would it be split into two sub-trees or would it be 1 straight line with the nodes to the right of the parent node?
The diagram is to start with 1 as the parent node.
The value in the middle should be the root, this is to get the lowest height in your tree.
Then from there just write two pointers on each node one for the lower and one for the greater and so on, until you get to your leafs nodes.
1,2,3,4,5,6,7,8,9,10
5
3 8
2 4 7 9
1 6 10

IOI 2003 : how to calculate the node that has the minimum balance in a tree?

here is the Balancing Act problem that demands to find the node that has the minimum balance in a tree. Balance is defined as :
Deleting any node
from the tree yields a forest : a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T
For the sample tree like :
2 6 1 2 1 4 4 5 3 7 3 1
Explanation is :
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The
larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node
1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees
has two nodes, so the balance of node 1 is two.
What kind of algorithm can you offer to this problem?
Thanks
I am going to assume that you have had a looong look at this problem: reading the solution does not help, you only get better at solving these problems by solving them yourself.
So one thing to observe is, the input is a tree. That means that each edge joins 2 smaller trees together. Removing an edge yields 2 disconnected trees (a forest of 2 trees).
So, if you calculate the size of the tree on one side of the edge, and then on the other, you should be able to look at a node's edges and ask "What is the size of the tree on the other side of this edge?"
You can calculate the sizes of trees using dynamic programming - your recurrence state is "What edge am I on? What side of the edge am I on?" and it calculates the size of the tree "hung" at that node. That is the crux of the problem.
Having that data, it is sufficient to iterate through all the nodes, look at their edges and ask "What is the size of the tree on the other side of this edge?" From there, you just pick the minimum.
Hope that helps.
You basically want to check 3 things for every node:
The size of its left subtree.
The size of its right subtree.
The size of the rest of the tree. (size of tree - left - right)
You can use this algorithm and expand it to any kind of tree (different number of subnodes).
Go over the tree in an in-order sequence.
Do this recursively:
Every time you just before you back up from a node to the "father" node, you need to add 1+size of node's total sub trees, to the "father" node.
Then store a value, let's call it maxTree, in the node that holds the maximum between all its subtrees, and the (sum of all subtrees)-(size of tree).
This way you can calculate all the subtree sizes in O(N).
While traversing the tree, you can hold a variable that hold the minimum value found so far.

Resources