Can we sort 7 numbers in 10 comparisons?
Depth of a binary tree with n node is? log(n)+1 or something else
If every node in a binary tree has either 0 or 2 children then the height of the tree is log(n): is it true or false?
Inserting an element into a binary search tree of size n takes time proportional to ------?
A binary tree not balanced can have all children at the right (for example), so the maximum height of a tree with n nodes is n
Same as 2.
If the tree is not balanced the insertion is proportional to the number of nodes that you need to traverse to find the correct position. Potentially n nodes.
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).
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."
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
I'm trying to find out what are the minimum and maximum number of nodes in a 2-3 Tree with n leaves.
I have tried blocking it with inf\sup but I couldnt go further then that the number of nodes in a 2-3 Tree is bigger then the number of nodes in a full-AVL tree.
Thanks in advance
Operating under the definition of a 2-3 tree at wikipedia:
In computer science, a 2–3 tree is a type of data structure, a tree where every node with children (internal node) has either two children (2-node) and one data element or three children (3-nodes) and two data elements. Nodes on the outside of the tree (leaf nodes) have no children and one or two data elements.
It appears to me that the maximum number of nodes in a tree will be when each internal node has 3 children. In order to find the maximum number of nodes in that tree, we must first find the height of the tree.
If there are n leaves in this 3 tree, then the height of the tree is height = log3(n) (log base 3 of n) and so the max number of items would be 3^height.
The smallest tree is one which has the smallest number of elements, which would be a tree with a single node.
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