Calculating Binary search tree height - data-structures

what is the right way of calculating the height of a BST?
the depth of the tree + 1?
the number of edges?
other?

Related

Maximum height of a Binary tree vs height of a binary tree

Are height and maximum height different metrics for binary trees?
For the tree above, is the height 2 and maximum height 2+1 = 3?
Height of a binary tree is the longest path from root to any leaf nodes.
So, height and maximum height has no difference.
The height (or maximal depth) of your tree is, as you say, 2 because that is the largest depth of any node.
The maximum height is a less well-defined term, so unless you specify how you choose to define it, I will interpret literally, meaning the maximum height that a binary tree of this size can have. Since a binary tree requires that a node have at most two children, we find that we can just assign each node one single child and connect them all under each other in a long chain. This is essentially a one-dimensional linked list. Since your tree has 5 nodes, the height of such a tree would be 5-1 (because we don't count the root node in the definition you are using). So the maximum height, if interpreted this way, would be 4. But if you are using a different definition for maximum height, you will have to specify it.
According to definition, height of a binary tree is Maximum of height of left sub-tree of the root node and height of right sub-tree of root node +1.
MAX(left sub-tree of root height, right sub-tree of root height) + 1.
And this is also the maximum height of the binary tree.
Now, what you are confused about is the Diameter of binary tree.
Diameter is the length of the longest path between any two nodes of the tree. In your example, height of binary tree is 2 whereas diameter of binary tree is 3.

Why A Balanced Binary Search Tree's Subtrees Cant Differ More than 1 In Height

Can anyone mathematically prove me why the following BST definition is always true by using the fact that the height of a BST is always logN where N is the number of nodes.
"a binary tree in which the left and right subtrees of every node differ in height by no more than 1."

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

Minimum number of vertices in a binary tree of height 5

What is the Formula to Find the minimum number of vertices required to make a binary tree (not a complete binary tree) of height 5 ?
A binary tree's height cannot be bigger than the number of nodes or vertices in the tree. So yes, the minimum number of vertices required for a binary tree of height 5 will be 5. Also, there must be n-1 edges between them. You can imagine a single series of connected nodes, and that is basically what you get.
Alternately, a full binary tree is a binary tree in which each internal vertex has exactly two children.This means a binary tree with n internal vertices has 2n + 1 vertices, 2n edges, and n + 1 leaves.
the minimum number of vertices in a binary tree of height n (with no further constraints) is always n.
proof:
if there were less than n nodes, the tree wouldn't be a valid binary tree (I guess I don't need to further explain this point)
if there were more than n nodes, it means at least one of the nodes has a brother (pigeonhole principle), which could be taken off the tree and it would result in a valid, smaller binary tree, so we know that this tree is not minimal, which opposes our assumption of a minimal tree.
-> a binary tree T is minimal -> T has n nodes

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