Hoffman tree Vs, Binary balanced tree - algorithm

Given n Real Positive numbers A1,A2....An build a binary tree T such as:
Every number is a leaf.
Weight of T will be minimal as possible. Weight of tree equals to sum of each leaf times its hight.
This question was given to me during a test in Algorithms and Data Structures. My answer in brief was to build a binary tree such that each leaf is A1 to An. Weight of T will be sum of logn*Ai.
I did not get points for this answer. The answer that was awarded full points was to sort the numbers by frequencies and build a Hoffman Tree.
My question is why my answer was ignored?
If A1 to An are all very small numbers, for example ranging from 0 to 1, then the hight of each leaf will become the dominent factor in calculating the weight of the tree.
Help would be apprecieted.

In the original array A there may be some elements with many more occurences than the others. You want to construct the tree in a way, that the most frequent elements are higher in the tree than the somewhat rare ones.
Consider the example on this page - "A quick tutorial on generating a huffman tree".
The generated huffman tree has weight 228, which is optimal.
The best perfectly balanced tree you could get for the same set has weight 241 (5 and 6 with depth 2, other elements with depth 3), the worst one 294 (switching 5 and 6 with 1 and 2).
Your solution would find something between those, rather than the optimum.

Related

minimum height insertion orders for a Binary Search Tree

So in class one of my exercises was to find the insertion orders that result in a binary search tree with a minimum height and maximum height. The numbers being inserted were [1,2,3,4]. The resulting answer was this:
Figure 3.9:
However what I fail to understand is why the insertion orders 1324,1342,4213,4231 are not included as an insertion order resulting in a minimum height, as technically don't these result in a BST with a minimum height of 2 as well?
Thank you in advance!
Interesting that the text doesn't mention those four cases. They don't have the worst case height, but they aren't minimal either. There are two features that characterize a tree:
the maximum depth from the root to any node
the average depth from the root to any node
A tree like 1432 has maximum depth 3, and average depth (0+1+2+3)/4 = 1.50
A tree like 3124 has maximum depth 2, and average depth (0+1+1+2)/4 = 1.00
A tree like 1324 has maximum depth 2, but average depth (0+1+2+2)/4 = 1.25
The best possible tree has the smallest average as well as the smallest maximum depth. To put it another way, the best possible tree has every level (except for the last) completely filled.
For example, even though the two trees below have the same number of nodes, and the same maximum depth, the tree on the left is not a minimum height tree because it's missing a node at the third level (which means that the average depth will be greater than the tree on the right).

Dynamic Programming on Binary Tree: Maximize data transmitted with limited edge capacity

Given a network of a binary tree of nodes with edge capacities c_e. There are data at the leaf nodes and each has data size s_v. L_e is the set of all leaves in the subtree below edge e. Our aim is to find subset S of leaves such that the number of data size transmitted to the root r is maximized, but for all the edges that the data passes through the capacity constraint must hold. It is assumed that c_e and s_v are non negative integers and let m be their maximum. Using dynamic programming on trees it should run in O(nm^2) time.
I have working on this for hours but haven't really come up woth a working solution. Any hints would be appreciated.
edit:
The data must be transmitted as a whole or not at all. for example if a leaf has 10 the algorithm can only take 10 or 0 at all.
for example,
v4=1, v5=3, v6=2, v7=2.
e1=(v1,r), e2=(v2,v1) and e3=(v3,v1) and so on.
assume that the capacity for e4, e5,e6 and e7 satisfied. But c1=5, c2=3 and c3=4
if we focused on finding the maximum of each subtree, we will end up taking v5 and v6+v7 which is not optimal. how to make dynamic programming rule that can tackle this problem and find the correct optimal solution?
Similar to the dynamic programming solution for subset sum...
For each node, calculate the set of attainable sums for subsets of the leaves, and for each attainable sum, remember the last contributing child and the previous sum. You can use this information to reconstruct the set that produces the sum.
While doing a postorder traversal of the tree, you can calculate this set for each node using only the information on its children.
When you get to the root, pick the maximum attainable sum and reconstruct the leaves that produce it.

construct unique binary search tree

I'm working on an algorithm problem. Given n, generate all structurally unique binary search trees that store values 1...n. The solution was to enumerate each number i in the sequence, and use the number as the root, the subsequence 1…(i-1) on its left side would lay on the left branch of the root, and similarly the right subsequence (i+1)…n lay on the right branch of the root. Then construct the subtree from the subsequence recursively. This approach ensures that the BST constructed are all unique, since they have unique roots.
Now my question is: what if the trees are not limited to binary search trees, if it can be any binary tree. How would the solution be? I'd still want to go over all the cases with root i, where i = 1, ... n. The left subtree doesn't have to be in the range of 1...(i-1), right subtree doesn't have to be in the range of (i+1)...n. But then how to arrange them then? Create an arbitrary subset of (i-1) elements and apply??
Suppose you were given the following problem: given n disks, arrange them in unique binary-tree shapes. Then, following your correct reasoning in the question, you could say the following: I'll number the disks 1, 2, 3, .., n; then I'll (recursively) build the trees whose root is at disk #1, then disk #2, etc.
So the tree rooted-digraphs you (correctly) found really have nothing to do with the content in the nodes, let alone the question of whether the contents satisfied the BST invariant. Given your question here,
If the question is how many rooted digraphs exist, then it's the same as before.
If the question is how many combinations of rooted digraphs + node contents there are, then you just enumerate the rooted digraphs as you've done, and, for each one, enumerate the permutations of 1, 2, ... n.
If this is the case, and you don't need to enumerate, but rather approximate the number of such trees, then note that this is n! multiplied by the Catalan Numbers.
Use your algorithm for BSTs. This generates the unique shapes of trees. The shapes are unique, because for each root n there are n-1 elements in the left subtree, the rest are on the right.
Then, for each shape, there are n! orderings of elements. This gives the results for arbitrary trees.

What is the largest chromatic number of a binary tree?

This is a Discrete Math/Combinatorics Question from my homework, but I don't really understand the question.
Find largest chromatic number of a full binary tree given the following depths:
(Check all that apply)
2, 3, 7, 12, 200
I understand that the chromatic number refers to the minimum color that you can color a graph or tree with the adjacent nodes or vertices being different colors.
So knowing this fact, I'm sure that the chromatic number for all full binary trees should be 2 since you can use two different color nodes to complete the tree. But they want me to find the largest chromatic number, which confuses me.
Am I missing something?
All trees are bipartite graphs and therefore 2-colorable. One way to see this is that a graph is bipartite iff it has no odd cycles, and since trees have no cycles at all, they must be bipartite. Therefore, two colors suffice regardless of the tree size.
Hope this helps!

Density of a Binary Tree

How do I find the density of a given binary tree? I came across this interview question and not sure as to what they mean by density! Any help would be appreciated.
A dense binary tree is close to perfect (it has close to 2^(h + 1) - 1 nodes). A sparse tree is closer to a linked list (it has close to h nodes). h is the height of the tree where a single root node has height 0.
A simple measure of density could be:
(n - h)/(2^(h + 1) - h - 1)
I just made that formula up, so I don't know if it would suit your needs for an interview answer, but it'll give you 0 for a degenerate tree and 1 for a perfect tree. It will give you numbers close to 1 for dense trees, and numbers close to 0 for sparse ones.
Wikipedia has a lot of information on binary trees.
In a binary trees, the number of nodes at each level falls within a range of values.
At level 0, there is 1 node, the root; at level 1 there can be 1 or 2 nodes.
At any level k, the number of nodes is in the range from 1 to 2k.
The number of nodes per level contributes to the density of the tree. Intuitively, density is a measure of the size of a tree (number of nodes) relative to the height of the tree.

Resources