Join m binary trees to get a single binary tree with minimum possible height - binary-tree

In a collection of binary trees, T1, . . . , Tk. Now I need to join all trees to form a single binary tree that has the minimum height. To join two trees Ti and Tj , the root of one tree must be made a child of some node of the other tree that has less than two children.
If we join the trees one by one to get one single binary tree, what is the deciding factor at each step while joining 2 trees ? Total no of joins will be k-1; but which 2 trees to join at the ith step ?
My approach: Take the 2 trees with minimum heights and join them. Continue this until you get a single tree. But this approach seems to be wrong.

Related

difference between m way tree and m way search tree

I tried finding the difference between m way tree and the m way search tree. Most resources only tells about m way search tree and end up being on B tree or B+ trees.
My doubts are:-
Is it analogous to the binary tree and binary search tree?
I read somewhere that m way trees don't have any particular order and
every node has to be filled fully before moving to the new node.(complete
tree)
Is it analogous to the binary tree and binary search tree?
Yes
m way trees don't have any particular order
This is true
and every node has to be filled fully before moving to the new node.(complete tree)
Something like this describes a step in an algorithm, and has little to do with the data structure itself: nothing is "moving" in a data structure.
Definitions
In short: an m-way tree puts no conditions on the values stored in the nodes, while an m-way search tree does.
Reva Freedman, associate professor at Northern Illinois University has notes on Multiway Trees where four terms are defined in succession, each time indicating which additional requirements apply for the next term:
multi way tree,
m-way tree
m-way search tree
B-tree of order m
Multiway Trees
A multiway tree is a tree that can have more than two children. A
multiway tree of order m (or an m-way tree) is one in which a tree can
have m children.
As with the other trees that have been studied, the nodes in an m-way
tree will be made up of key fields, in this case m-1 key fields, and
pointers to children.
To make the processing of m-way trees easier, some type of order will
be imposed on the keys within each node, resulting in a multiway
search tree of order m ( or an m-way search tree). By definition an
m-way search tree is a m-way tree in which:
Each node has m children and m-1 key fields
The keys in each node are in ascending order.
The keys in the first i children are smaller than the ith key
The keys in the last m-i children are larger than the ith key
M-way search trees give the same advantages to m-way trees that binary
search trees gave to binary trees - they provide fast information
retrieval and update. However, they also have the same problems that
binary search trees had - they can become unbalanced, which means that
the construction of the tree becomes of vital importance.
B-Trees
An extension of a multiway search tree of order m is a B-tree of
order m. This type of tree will be used when the data to be
accessed/stored is located on secondary storage devices because they
allow for large amounts of data to be stored in a node.
A B-tree of order m is a multiway search tree in which:
The root has at least two subtrees unless it is the only node in the tree.
Each nonroot and each nonleaf node have at most m nonempty children and at least m/2 nonempty children.
The number of keys in each nonroot and each nonleaf node is one less than the number of its nonempty children.
All leaves are on the same level.

Number of possible balanced binary trees?

Given number of nodes N (ie a preorder seq of length N) in a balanced binary tree,I am trying to find the number b of possible balanced binary trees that can be formed.
I noticed it is possible that there might be variations in the leaves as well as the higher-level nodes:
A
/ \
B C
/ / \
D E F
/
G
The constraint is generally applied recursively to every subtree. That is, the tree is only balanced if:
1.The left and right subtrees' heights differ by at most one, AND
2.The left subtree is balanced, AND
3.The right subtree is balanced

No of trees can be constructed from given Inorder/Preorder/Postorder traversal

I know one cannot construct a tree without having both Inorder and Preorder/postorder traversals. Because for a given (only Inorder/Preorder/postorder) there could be a possibility of generating more number of trees. Are there any algorithms or mechanism one can compute the number of unique trees from a given (only Inorder/Preorder/postorder traversal).
Eg : a b c d e f g this is my Inorder traversal.
How many unique trees that can be constructed with the given Inorder traversal.
I tried them is google but none of the explanations are clear
Any help would be appreciated...
Well the algorithm is as follows:
Let, P(N) denote the number of trees possible with N nodes. Let the indexes of the nodes be 1,2,3,...
Now, lets pick the root of the tree. Any of the given N nodes can be the root. Say node i has been picked as root. Then, all the elements to the left of i in the inorder sequence must be in the left sub-tree. Similarly, to the right.
So, total possibilities are: P(i-1)*P(N-i)
In the above expression i varies from 1 to N.
Hence we have,
P(N) = P(0)*P(N-1) + P(1)*P(N-2) + P(2)*P(N-3)....
The base cases will be:
P(0) = 1
P(1) = 1
Thus this can be solved by using Dynamic Programming.
Note that a particular traversal is just a way of labeling the nodes in a tree, so that the number of possible binary trees is the same for any two traversals of the same length. The number of binary trees with n nodes is given by the n-1st Catalan number.
The formula
(2n)!/ (n)!(n+1)!
OR
2n * C(n) / (n+1)
gives the number of possible binary trees for any given INORDER/PREORDER/POSTORDER traversal.

sorted result of inorder traversal on binary tree

I have a question about Binary Trees:
There is a binary tree T1 with n members.
When we run inorder traversal on T1 then we get a series from 1 to n (1,2,3,...n).
Now is T1 a BST (Binary Search Tree)?
I know that if T1 is BST an inorder traversal will result a sorted series but is the oposite direction will work also?
In short yes..
Binary Search property is every node on left tree is lesser and every node on right is greater.
Considering that at any sub tree in your case since we are traversing in-order and saw every element before them is lesser and we are ascending when travelling to the right we have the BST...
This sounds too homeworkish so no direct answer. But:
Assume the root has value k.
Now try this: what does is means for a node to be appearing to the left of k in the in order traversal? On the right?
Also, the numbers appearing before k are all smaller than k. What does that help this question here?

Is it always possible to turn one BST into another using tree rotations?

Given a set of values, it's possible for there to be many different possible binary search trees that can be formed from those values. For example, for the values 1, 2, and 3, there are five BSTs we can make from those values:
1 1 2 3 3
\ \ / \ / /
2 3 1 3 1 2
\ / \ /
3 2 2 1
Many data structures that are based on balanced binary search trees use tree rotations as a primitive for reshaping a BST without breaking the required binary search tree invariants. Tree rotations can be used to pull a node up above its parent, as shown here:
rotate
u right v
/ \ -----> / \
v C A u
/ \ <----- / \
A B rotate B C
left
Given a BST containing a set of values, is it always possible to convert that BST into any arbitrary other BST for the same set of values? For example, could we convert between any of the five BSTs above into any of the other BSTs just by using tree rotations?
The answer to your question depends on whether you are allowed to have equal values in the BST that can appear different from one another. For example, if your BST stores key/value pairs, then it is not always possible to turn one BST for those key/value pairs into a different BST for the same key/value pairs.
The reason for this is that the inorder traversal of the nodes in a BST remains the same regardless of how many tree rotations are performed. As a result, it's not possible to convert from one BST to another if the inorder traversal of the nodes would come out differently. As a very simple case, suppose you have a BST holding two copies of the number 1, each of which is annotated with a different value (say, A or B). In that case, there is no way to turn these two trees into one another using tree rotations:
1:a 1:b
\ \
1:b 1:a
You can check this by brute-forcing the (very small!) set of possible trees you can make with the rotations. However, it suffices to note that an inorder traversal of the first tree gives 1:a, 1:b and an inorder traversal of the second tree gives 1:b, 1:a. Consequently, no number of rotations will suffice to convert between the trees.
On the other hand, if all the values are different, then it is always possible to convert between two BSTs by applying the right number of tree rotations. I'll prove this using an inductive argument on the number of nodes.
As a simple base case, if there are no nodes in the tree, there is only one possible BST holding those nodes: the empty tree. Therefore, it's always possible to convert between two trees with zero nodes in them, since the start and end tree must always be the same.
For the inductive step, let's assume that for any two BSTs of 0, 1, 2, .., n nodes with the same values, that it's always possible to convert from one BST to another using rotations. We'll prove that given any two BSTs made from the same n + 1 values, it's always possible to convert the first tree to the second.
To do this, we'll start off by making a key observation. Given any node in a BST, it is always possible to apply tree rotations to pull that node up to the root of the tree. To do this, we can apply this algorithm:
while (target node is not the root) {
if (node is a left child) {
apply a right rotation to the node and its parent;
} else {
apply a left rotation to the node and its parent;
}
}
The reason that this works is that every time a node is rotated with its parent, its height increases by one. As a result, after applying sufficiently many rotations of the above forms, we can get the root up to the top of the tree.
This now gives us a very straightforward recursive algorithm we can use to reshape any one BST into another BST using rotations. The idea is as follows. First, look at the root node of the second tree. Find that node in the first tree (this is pretty easy, since it's a BST!), then use the above algorithm to pull it up to the root of the tree. At this point, we have turned the first tree into a tree with the following properties:
The first tree's root node is the root node of the second tree.
The first tree's right subtree contains the same nodes as the second tree's right subtree, but possibly with a different shape.
The first tree's left subtree contains the same nodes as the second tree's left subtree, but possibly with a different shape.
Consequently, we could then recursively apply this same algorithm to make the left subtree have the same shape as the left subtree of the second tree and to make the right subtree have the same shape as the right subtree of the second tree. Since these left and right subtrees must have strictly no more than n nodes each, by our inductive hypothesis we know that it's always possible to do this, and so the algorithm will work as intended.
To summarize, the algorithm works as follows:
If the two trees are empty, we are done.
Find the root node of the second tree in the first tree.
Apply rotations to bring that node up to the root.
Recursively reshape the left subtree of the first tree to have the same shape as the left subtree of the second tree.
Recursively reshape the right subtree of the first tree to have the same shape as the right subtree of the second tree.
To analyze the runtime of this algorithm, note that applying steps 1 - 3 requires at most O(h) steps, where h is the height of the first tree. Every node will be brought up to the root of some subtree exactly once, so we do this a total of O(n) times. Since the height of an n-node tree is never greater than O(n), this means that the algorithm takes at most O(n2) time to complete. It's possible that it will do a lot better (for example, if the two trees already have the same shape, then this runs in time O(n)), but this gives a nice worst-case bound.
Hope this helps!
For binary search trees this can actually be done in O(n).
Any tree can be "straightened out", ie put into a form in which all nodes are either the root or a left child.
This form is unique (reading down from root gives the ordering of the elements)
A tree is straightened out as follows:
For any right child, perform a left rotation about itself. This decreases the number of right children by 1, so the tree is straightened out in O(n) rotations.
If A can be straightened out into S in O(n) rotations, and B into S in O(n) rotations, then since rotations are reversible one can turn A -> S -> B in O(n) rotations.

Resources