For the height `h' of a binary tree, for which no further attributes are given than the number `n' of nodes, holds:
ceil( ld n) <= h <= n
Where `ld' is the binary logarithm and `ceil' is rounding up to the next integer.
Maximum height of Binary Search Tree of nodes is n-1
n(n-1)/2
n
Minimum is h nodes (Maximum is 2h+1 - 1 nodes, if tree consisting of only one node is considered to have height of 0. if you consider a tree with one node to be a height of one, then the minimum nodes is (2^(h-1)) 1 nodes. Minimum number of nodes in a binary tree of height is 2h+1. For example, if the height of the binary tree is 3, minimum number of nodes is 2*3+1=7.
binary search
If it is an unbalanced binary tree, O( ln( n ) / ln( 2 ) ) is best-case. Worst case is O( n ). If it is balanced, worst case is O( ln( n ) / ln( 2 ) ).
A degenerate binary tree is one where most or all of the nodes contain only one sub node. It is unbalanced and, in the worst case, performance degrades to that of a linked list. If your add node function does not handle rebalancing, then you can easily construct a degenerate tree by feeding it data that is already sorted.
Merge sort is O(n log n) for both best case and average case scenarios.
In the worst case a binary search tree is linear and has a height equal to the number of nodes. so h=O(h).
Minimum is h nodes (Maximum is 2h+1 - 1 nodes, if tree consisting of only one node is considered to have height of 0. if you consider a tree with one node to be a height of one, then the minimum nodes is (2^(h-1)) 1 nodes. Minimum number of nodes in a binary tree of height is 2h+1. For example, if the height of the binary tree is 3, minimum number of nodes is 2*3+1=7.
binary search
If it is an unbalanced binary tree, O( ln( n ) / ln( 2 ) ) is best-case. Worst case is O( n ). If it is balanced, worst case is O( ln( n ) / ln( 2 ) ).
The best case for a binary search is finding the target item on the first look into the data structure, so O(1). The worst case for a binary search is searching for an item which is not in the data. In this case, each time the algorithm did not find the target, it would eliminate half the list to search through, so O(log n).
A degenerate binary tree is one where most or all of the nodes contain only one sub node. It is unbalanced and, in the worst case, performance degrades to that of a linked list. If your add node function does not handle rebalancing, then you can easily construct a degenerate tree by feeding it data that is already sorted.
Merge sort is O(n log n) for both best case and average case scenarios.
Really the best way to traverse any binary tree is recursion. In this case we are going to be some node defined as having 3 values, a pointer to a left node, a pointer to a right node, and a value. then in psudocode we can do it as: int height(node n, int depth){ int leftDepth; int rightDepth; if(n.left != NULL) leftDepth = height(n.left, depth+1) else leftDepth = depth; if(n.right != NULL) rightDepth = height(n.right, depth+1) else rightDepth = depth; if(leftDepth > rightDepth) return leftDepth; return rightDepth; } Essentially what you are doing is calling the algorithm on both the left and right nodes which in turn will call it on their left and right nodes, down to where all the nodes are null. Then what is returned is the greater depth of the two; because it will traverse before returning a depth, and only traverses if there is a deeper node, it will return the depth of the deepest node, or the height of the binary tree.
In this representation, each node contains two pointers, one pointing to its parent (null in the case of root node) and the other pointing to its child node (null in the case of leaf nodes).
Average case complexity for Binary search O(log N). (Big O log n)Habibur Rahman (https://www.facebook.com/mmhabib89)BUBT University Bangladeshhttp://www.bubt.edu.bd/
worst case- often hyphenated- the Oil disaster was a Worst-Case scenario in the Gulf area!
If the array is unsorted, the complexity is O(n) for the worst case. Otherwise O(log n) using binary search.