Special Binary Tree, a tricky questions? - algorithm

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

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

What will be complexity in this Balanced BST?

I have been asked this question in an interview and I'm curious to know what will be the correct explanation for it?
Consider the following height balanced BST where Balance factor = Height of left subtree - Height of right subtree & the accepted balance factors are 0, 1, -1, 2, and -2.
What will be the time taken to search an element in such a kind of height-balanced BST? Explain.
What I said was, even if it has a height factor of 2 rather than 1 in standard Balance BST definitions, still the operations should be logN complexity order, (where N is the number of elements in the tree) because when the N will be large then will be not making much difference if the height factor is 2 or 1.
If anyone can tell me what would have been the correct answer here will be helpful :)
We can solve this mathematically as :
Defining Worst Case
Now, in any Binary Tree, time complexity of searching is O(h) where h is height of the Binary Tree.
Now, for worst case, we want to find Maximum Height.
In case of simple Binary Search Tree with no Balancing Factor
Condition on Nodes, this maximum height can be n or n+1 (depending
on convention whether height of single node tree will be 1 or 0)
where n is number of nodes.
Thus, we can say that given number of nodes, worst case is maximum height.
Interestingly, we can also say that given height of a tree, the worst case is minimum nodes. As even for such minimum number of nodes, we might have to traverse down the tree of height h, which we also have to do for maximum number of nodes.
Thus, the intuition should be clear that Given Height of Tree, the worst case is minimum number of nodes.
Applying this Concept on Binary Search Tree
Let us try to construct Binary Search Tree of Height H such that number of nodes in the tree is minimum.
Here we will exploit the fact that Binary Tree is a Recursive Data
Structure (A Binary Tree can be defined in terms of Binary Tree)
We will use the notation NH to denote Minimum Number of Nodes in a Binary Search Tree of height H
We will create a Root Node
To Left (or Right) of Root, add a subtree of height H-1 (exploiting Recursive Property). So that number of nodes in entire tree is minimum, the number of node in Left (or Right) subtree should also be minimum. Thus NH is a function of
NH-1
Do we need to add anything to Right (or Left)?
No. Because there is no restriction of Balancing Factor on BST. Thus, our tree will look like
Thus, to construct Binary Search Tree of Height H such that number of nodes in the tree is minimum, we can take Binary Search Tree of Height H-1 such that number of nodes is
minimum, and can add 1 root node.
Thus, we can form Recurrence Relation as
NH = NH-1 + 1
with base condition as
N0=1
To create BST of height 0, we need to add one node. Throughout the answer we will use this convention
Now, this Recurrence Relation is quite simple to solve by Substitution and thus
NH = H+1
NH > H
Now, let n be the number of nodes in the BST of height H
Then,
n ≥ NH
n ≥ H
H ≤ n
Therefore,
H=O(n)
Or
O(H) = O(n)
Thus, Worst Case Time Complexity for Searching will be O(n)
Applying this Concept on AVL Tree
We can apply similar concept on AVL Tree. After reading later part of solution, one can find recurrence relation as :
NH = NH-1 + NH-2 + 1
with Base Condition :
N0 = 1
N1 = 2
And, inequality condition on solving recurrence will be
NH ≥ ((1+√5)/2)H
Then, let n be the number of nodes. Thus,
n ≥ NH
On simplifying, one can conclude that
H ≤ 1.44log2(n)
Applying this Concept on GIVEN Tree
Let us try to construct Given Tree of Height H such that number of nodes in the tree is minimum.
We will use the notation NH to denote Minimum Number of Nodes in Given Tree of height H
We will create a Root Node
To Left (or Right) of Root, add a subtree of height H-1 (exploiting Recursive Property). So that number of nodes in entire tree is minimum, the number of node in Left (or Right) subtree should also be minimum. Thus NH is a function of
NH-1
Do we need to add anything to Right (or Left)?
Yes! Because there is restriction of Balancing Factor on Nodes.
We need to add subtree on Right (or Left). What should be it's height?
H?
No, then height of entire tree will become H+1
H-1?
Permitted! since Balancing Factor of Root will be 0
H-2?
Permitted! since Balancing Factor of Root will be 1
H-3?
Permitted! since Balancing Factor of Root will be 2
H-4?
Not Permitted! since Balancing Factor of Root will become 3
We want minimum number of nodes, so out of H-1, H-2 and H-3, we will choose H-3. So that number of nodes in entire tree is minimum, the number of node in Right (or Left) subtree should also be minimum. Thus NH is also a function of
NH-3
Thus, to construct Given Tree of Height H such that number of nodes in the tree is minimum, we can have LEFT subtree as
Given Tree of Height H-1 such that number of nodes is minimum and can have RIGHT subtree as Given Tree of Height H-3 such that number of nodes in it is also minimum, and can add one Root Node. Our tree will look like
Thus, we can form Recurrence Relation as
NH = NH-1 + NH-3 + 1
with base condition as
N0=1
N1=2
N2=3
Now, this Recurrence Relation is Difficult to Solve. But courtesy to this answer, we can conclude that
NH > (√2)H
Now, let n be the number of nodes in the Given Tree Then,
n ≥ NH
n ≥ (√2)H
log√2(n) ≥ H
H ≤ log√2(n)
H ≤ 2log2(n)
Therefore, H=O(log(n))
Or O(H) = O(log(n))
Thus, Worst Case Time Complexity for Searching in this Given Tree will be O(log(n))
Hence, Proved Mathematically!

Given a number n, how many balanced binary trees (not binary search trees) are there?

The definition of balanced in this question is
The number of nodes in its left subtree and the number of nodes in its
right subtree are almost equal, which means their difference is not
greater than one
if given a n as the number of nodes in total, how many are there such trees?
Also what if we replace the number of nodes with height? Given a height, how many height balanced trees are there?
Well the difference will be made only by the last level, hence you can just find how many nodes should be left for that one, and just consider all possible combinations. Having n nodes you know that the height should be floor(log(n)) hence the same tree at depth k = floor(log(n)) - 1 is fully balanced, hence you know that is needs (m = sum(i=0..k)2^i) nodes, hence n-m nodes are left for the last level. Some definition of a balanced binary tree force "all the nodes to be left aligned", in this case it is obvious that there can be only one possibility, without this constraint you have combinations of 2^floor(log(n)) chooses n-m, because you have to pick which of the 2^floor(log(n)) possible slots you will assign with nodes, forcing a total of n-m nodes to be assigned.
For the height story you consider a sum of combinations of 2^floor(log(n)) chooses i as i goes from 1 to 2^floor(log(n)). You consider all possibilities of having either 1 node at the last level, then 2 and so on, until you don't make it a fully balanced binary tree, hence having all 2^floor(log(n)) slots assigned.

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

Split a tree into equal parts by deleting an edge

I am looking for an algorithm to split a tree with N nodes (where the maximum degree of each node is 3) by removing one edge from it, so that the two trees that come as the result have as close as possible to N/2. How do I find the edge that is "the most centered"?
The tree comes as an input from a previous stage of the algorithm and is input as a graph - so it's not balanced nor is it clear which node is the root.
My idea is to find the longest path in the tree and then select the edge in the middle of the longest path. Does it work?
Optimally, I am looking for a solution that can ensure that neither of the trees has more than 2N / 3 nodes.
Thanks for your answers.
I don't believe that your initial algorithm works for the reason I mentioned in the comments. However, I think that you can solve this in O(n) time and space using a modified DFS.
Begin by walking the graph to count how many total nodes there are; call this n. Now, choose an arbitrary node and root the tree at it. We will now recursively explore the tree starting from the root and will compute for each subtree how many nodes are in each subtree. This can be done using a simple recursion:
If the current node is null, return 0.
Otherwise:
For each child, compute the number of nodes in the subtree rooted at that child.
Return 1 + the total number of nodes in all child subtrees
At this point, we know for each edge what split we will get by removing that edge, since if the subtree below that edge has k nodes in it, the spilt will be (k, n - k). You can thus find the best cut to make by iterating across all nodes and looking for the one that balances (k, n - k) most evenly.
Counting the nodes takes O(n) time, and running the recursion visits each node and edge at most O(1) times, so that takes O(n) time as well. Finding the best cut takes an additional O(n) time, for a net runtime of O(n). Since we need to store the subtree node counts, we need O(n) memory as well.
Hope this helps!
If you see my answer to Divide-And-Conquer Algorithm for Trees, you can see I'll find a node that partitions tree into 2 nearly equal size trees (bottom up algorithm), now you just need to choose one of the edges of this node to do what you want.
Your current approach is not working assume you have a complete binary tree, now add a path of length 3*log n to one of leafs (name it bad leaf), your longest path will be within one of a other leafs to the end of path connected to this bad leaf, and your middle edge will be within this path (in fact after you passed bad leaf) and if you partition base on this edge you have a part of O(log n) and another part of size O(n) .

Resources