The question is what I have read in the previous slides of the lecture . I have been mentioned that the Height of tree = Maximum Level + 1 .
While in a next slide I found that Maximum Depth = height of a tree .
Now can anybody proves that because i have got a tree that isn't satisfying both the statements
If you see in the above Picture the Maximum Level is exactly the same as the depth and height . Then how is it satisfying the statement .
Height Of a tree = Maximum Level + 1
I just add Another image to clarify what i am asking about this is another slide i am having for finding the maximum height .
.png
I have learned a formula that is
level=depth+1
making more clear i would say that
Height of a node x is number of edges in longest path from x to a leaf node.
while the Depth of x is the number of edges in path from root to x.
I would say we can compare depth and the level as given in formula above but not level and height.
Related
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).
Assume: 'd' is the finite depth of a tree ; 'b' is a branching factor ; 'g' is the shallowest goal node.
From what I know, the worst-case is when the goal node is at the very last right-bottomed node in a tree.
Thus, supposedly the total number of nodes generated is O(bg), right?
However, my instructor told me it was wrong since the worst-case is when all the tree are explored except the subtree rooted at the goal node.
He mentioned something about O(bd) - O(b(g-d)) .... I'm not entirely sure.
I don't really get what he means, so can somebody tell me which answer is correct?
I recommend drawing a tree, marking the nodes that are explored, and counting how many there are.
Your reasoning is correct if you use breadth first search because you will only have reached a depth of g for each branch (O(b**g) nodes explored in total).
Your instructor's reasoning is correct if you use depth first search because you reach a depth of d for all parts of the tree except the one with the goal (O(b**d - b**(d-g)) nodes explored).
The goal is the green circle.
The blue nodes are explored.
The red nodes are not explored.
To count the number explored we count the total in the tree, and take away the red ones.
Depth = 2 = d
Goal at depth = 1 = g
Branching factor = b = 3
Note that I have called the total number of nodes in the tree O(b**d). Strictly speaking, the total is b**d + b**(d-1) + b**(d-2) + ... + 1, but this is O(b**d).
The fan-out of a node in a tree is defined to be the number of children the node has. The fan-out of a tree is defined to be the maximum fan-out of any node in the tree. Assume tree T has n nodes and a fan-out f > 1. What is the minimum possible height of T?
I have no idea how to begin this problem. I solved the first part which is to find the maximum number of nodes that can be T in terms of height h and fan-out f > 1. I got (f^(h+1) -1)/(f-1). I'm thinking you can use this to solve for the question above. Can someone please point me in the correct direction?
Thank you!
I would approach this problem by turning it around and trying to find the maximum number of nodes you can pack in a tree with a given height and fan-out T_max(h,f). This way, every other tree T(h,f) is guaranteed to have as much, or less nodes than T_max(h,f). Therefore, if you find such T_max(h,f), that
total_nodes( T_max(h,f) ) > n > total_nodes( T_max(h-1,f))
h will be guaranteed to be the minimum height of a tree with n nodes and f fan-out.
In order to find such a tree, you need to maximize the number of nodes in every layer of a tree. In other words, every node of such a tree needs to have fan-out of f, no less. Therefore you start inserting nodes in a tree, one level at a time. After a layer is full, you start adding another layer. After n nodes are inserted in such a tree, you stop and check the height of the tree. This will be the minimal height you are looking for.
Or, you can do a calculation instead:
nodes_in_level(1) = 1
nodes_in_level(2) = f
nodes_in_level(3) = f * f
...
nodes_in_level(x) = f ^ (x - 1)
This is a standard geometric progression. The maximum nodes of a given tree with height x and fan-out f is therefore the sum of such geometric progression and it shouldn't be too much trouble to figure out the smallest x, for which the number of nodes is greater than n.
In samsung technical test Question was asked whether this statement is true or not:
Maximum number of nodes in a binary tree is 2^(h+1)-1 where h is the height of tree.
I think it's false because according to Schaum's Series TMH height is defined as the maximum number of nodes till the leaf is reached.
Is it correct or not?
The statement is correct. A zero-height binary tree has at most one node, a tree of height one has at most 3 nodes and so on.
There are two conventions to define the height of binary 1) Maximum Number of nodes from root to leaf 2) Maximum Number of edges from root to leaf
Please refer following article
http://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/
True, Maximum number is reached when each node has two children - so we have power of 2. Just draw the tree with all leafs at each level and you will see the formula.
according to this wikipedia article that statement is true
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.