minimum height insertion orders for a Binary Search Tree - algorithm

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).

Related

Maximum & minimum height of a binary tree

According to my textbook when N nodes are stored in a binary tree H(max) = N
According to outside sources when N nodes are stored in a binary tree H(max) = N - 1
Similarly
According to my textbook when N nodes are stored in a binary tree H(min) = [log2N+1]
According to outside sources when N nodes are stored in a binary tree H(min) = [log2(N+1)-1]
Which one is right and which is wrong? Are they supposed to be used in different situations. In this case what would be the maximum height of a tree with 32 nodes?
I have been looking through my resources to understand this concept, and for some reason all my sources have different answers. I can calculate height when a binary tree is pictorially represented because that would involve number of nodes in each subtree. What about when only the number of nodes are given?
Obviously it has to do with the definition of height. If the height is defined by the number of nodes traversed from top to bottom then the max height is N. If it's defined as the number of hops between nodes, then it's N-1. Same goes for minimum height. That said, what counts is it's respectively O(N) and O(log N).

Special Binary Tree, a tricky questions?

We have a binary tree with n nodes. this tree is not necessarily balanced. for any node such x of this tree, we calculate the size (i.e: number of nodes) of left and right subtree of this node and set the label of this node as the minimum of these two values (values of right size and left size subtree). if any subtree has zero nodes, this size is equal to 0. Which of the following is True:
I) sum of labels belongs to order O(n log n).
II) there is a tree that sum of it's label belongs to order O(n). (i.e: is it possible to get a tree with sum of it's label be O(n)?)
III) there is a tree that sum of it's label belongs to order O(n^2).
My TA says two of these is true. My problem is with these sentences, anyone could describe it for me?
My guess is that it's 1 & 2.
1.) Consider the height of the tree. The height and the number of nodes n have the relationship n = (2^h)-1. From this relation, we can derive that h =logn. Now, Let's move to the number of nodes in each level of the binary tree. The maximum number of nodes that a level could have is (n/2) which is the last level (in a full binary tree, the last level will have n/2 nodes). So, the worst case of calculating the minimum is (number of levels)*(number of nodes in each level) => n/2logn => O(nlogn).
2.) It's possible to get an 0(n) solution by changing the height of the tree. For example of if considering a subset of the all nodes such that the height of the tree is zero/one, then it's possible to get an O(n) solution - for a tree with a total level of two, there can be a maximum of three nodes (root, left, right), and therefore, there's no minimum calculation involved in this. In this case, we don't have the additional logn in running time, and we end up with O(n).

Hoffman tree Vs, Binary balanced tree

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.

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is:
h = log n,
where
h is height
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.
He then goes on to say that the height of a perfectly balanced binary search tree, would be:
h = log n
I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.
Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?
sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.
proof sketch:
a perfectly balanced binary tree has
an actual branching factor of 2 at each inner node.
equal root path lengths for each leaf node.
about the leaf nodes in a perfectly balanced binary tree:
as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).
so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).
It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.
In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.
n = 2^(h+1) -1
this is the all the nodes of the tree

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