How can I find the n-th item in a sorted array, but that array is represented using two AVL trees? - data-structures

I have two AVL trees filled with numbers from a set. The information stored in every node is both the value of the node itself and the size of the corresponding subtree. The question is how can I (in an optimal time complexity) find the nth value of the sorted array containing all the items in the AVL trees (basically, the nth smallest number).
My approach would be to do some kind of binary search regarding the sizes of the subtrees, but the fact that there are two makes it a little bit more difficult. Would it be inefficient to firstly combine the two AVL trees? What would be your approach?

Related

How many binary trees possible with N nodes using Catalan Number concept

So I was understanding Catalan Number Concept and try to implement it in code using topDown DP approach, so the recurrence relation is from what I have learned,
f(N) signifies total count of binary search trees possible by N number of nodes
f(N) = f(i-1).f(N-i)
where,
i = 1.....N (i signifies root node and traverse through all the nodes),
N is total number of nodes present in the binary tree,
f(i-1) signifies number of left subtrees possible when i is a root node,
f(N-i) signifies number of right subtrees possible when i is a root node.
there's a mathematical formulae by which we can find f(N) value,
2NcN/(N+1)
.
And, total count of binary trees possible by N number of nodes,
(factorial of N) * f(N)
My Doubt:
what's the difference between binary trees and binary search trees? I feel like both are defines same meaning but there's a difference that I'm not aware of so help me.
What's the difference between binary trees and binary search trees?
There is no difference in the shape of such trees, but binary search trees put a constraint on the values that its nodes have, which doesn't exist for (just) binary trees:
The value of a node must not be less than any of the values in its left subtree
The value of a node must not be greater than any of the values in its right subtree
Another way to express this constraint is:
The in-order traversal of the values in the tree must be non-decreasing.
Practically this means that when you have been given:
A shape of a binary tree with 𝑛 nodes
A set of 𝑛 unique values that are present in the tree
...then there is only one possibly way to associate those values with the tree's nodes for it to be a binary search tree. If it does not have to be a binary search tree, then there are 𝑛! ways to make that association.
Conclusions:
Given 𝑛, then 𝐢𝑛 (the 𝑛th Catalan number) represents:
the number of binary trees with 𝑛 nodes without values (i.e. number of shapes of binary trees)
the number of binary search trees with 𝑛 distinct values
The number of binary trees with 𝑛 distinct values is 𝑛!𝐢𝑛

In binary trees, are sibling nodes necessarily ordered?

Just been learning about binary trees in school, and two rules of binary trees is that
every node has at most 2 child nodes
there exists linear ordering defined for the children of each node (ordered pair)
Now, all types of binary trees (full, complete, etc.) are binary trees so they must satisfy these 2 conditions.
However, I saw on GeeksForGeeks this example:
How is 'linear ordering', ordered pair, defined here?
It seems that for the sibling nodes in this picture, some left ones are larger than the right one, some right nodes are larger than the left one.
If asked to check if a given tree is a binary tree, how do I make sure of the second property, that the children of each node have to be ordered?
Thanks
This is one of the complicated ways to introduce a binary tree.
two rules of binary trees is that
every node has at most 2 child nodes
there exists linear ordering defined for the children of each node (ordered pair)
Simple ways of introducing binary trees I could think of are "at most two children and no cycles" or "at most two children and unique path between any pair of vertices".
But fine. You bring up the point of linear order. Lets discuss that.
Here
A linear ordering on a finite collection of objects may be described
as follows: each object has exactly one immediate predecessor object
and one immediate successor object with two exceptions: A first object
has no predecessor and a last object has no successor.
If you have learnt about traversal so far, with the above definition, I would take binary tree traversals as linear order - preorder, postorder, inorder, level order. This applies to all types of binary trees (full, complete, etc.) which includes the complete binary tree you posted as an image.

How self balancing tree or small height tree structure helps for efficient lists and abstract data structure

I'm reading about AVL tree and there I redirected to self balancing tree there I read that
In computer science, a self-balancing (or height-balanced) binary
search tree is any node-based binary search tree that automatically
keeps its height (maximal number of levels below the root) small in
the face of arbitrary item insertions and deletions.1
These structures provide efficient implementations for mutable ordered
lists, and can be used for other abstract data structures such as
associative arrays, priority queues and sets.
I'm confused
What is the relation of height to list? array?
how small height tree provide efficient implementations for mutable ordered lists? array? queues?
Let suppose the node of list or index of array are height of list or
array, how it could be small?
Here is a visual on balance
2. Remember that trees offer a unique path to every node. Because AVL trees are binary search trees we "know" which direction to go when given a value, because the tree is sorted. AVL trees (for every node, the heights of the left and right subtrees differ by at most 1) allow for quick searches and insertions, usually log N, because it can rotate and manipulate itself in constant time. We can implement an ordered list, queue, etc. values into a tree structure to leverage its characteristics. The key is the tree remaining balanced (stays horizontal not vertical, which it does by following the binary search tree trait).
Can you elaborate on 1 and 3?

How many traversals need to be known to construct a BST

I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree from any one traversal (pre,post or in-order), or a combination of any two of them. For example, at this page, it says that given the pre,post or level order traversal, along with the in-order traversal, one can construct the BST. But here and there, they show us to construct a BST from pre-order alone. Also, here they show us how to construct the BST from given pre and post-order traversals. In some other site, I found a solution for constructing a BST from the post-order traversal only.
Now I know that given the inorder and pre-order traversals, it is possible to uniquely form a BST. As regards the first link I provided, although they say that we can't construct the BST from pre-order and post-order, can't I just sort the post-order array to get its inorder traversal, and then use that and the pre-order array to form the BST? Will that be same as the solution in the 4th link, or different? And given pre-order only, I can sort that to get the in-order, then use that and the pre-order to get the BST. Again, does that have to be different from the solution at links 2 and 3?
Specifically, what is sufficient to uniquely generate the BST? If uniquement is not required, then I can simply sort it to get the in-order traversal, and build one of the N possible BSTs from it recursively.
To construct a BST you need only one (not in-order) traversal.
In general, to build a binary tree you are going to need two traversals, in order and pre-order for example. However, for the special case of BST - the in-order traversal is always the sorted array containing the elements, so you can always reconstruct it and use an algorithm to reconstruct a generic tree from pre-order and in-order traversals.
So, the information that the tree is a BST, along with the elements in it (even unordered) are equivalent to an in-order traversal.
Bonus: why is one traversal not enough for a general tree, (without the information it is a BST)?
Answer: Let's assume we have n distinct elements. There are n! possible lists to these n elements, however - the possible number of trees is much larger (2 * n! possible trees for the n elements are all decayed trees, such that node.right = null in every node, thus the tree is actually a list to the right. There are n! such trees, and another n! trees where always node.left = null ) Thus, from pigeon hole principle - there is at least one list that generates 2 trees, thus we cannot reconstruct the tree from a single traversal.
(QED)
If the values for the nodes of the BST are given then only one traversal is enough because the rest of the data is provided by the values of the nodes. But if the values are unknown then, as per my understanding, constructing a unique BST from a single traversal is not possible. However, I am open to suggestions.

nth smallest element of a BST

a BST(binary search tree) T is given.
how to find the nth smallest element of T ?
A binary search tree is effectively sorted, so you just need to go through the tree in-order and get to the nth spot. If the tree is fully balanced, you can calculate the spot to get to.
If the binary search tree is not fully balanced, then you need to do a recursive search to find the nth smallest element. This can be hugely accelerated by having each node store the number of subnodes pointed to by each of its branch pointers, effectively turning the search into a binary one. However, this add overhead on tree updates as each insertion or deletion now requires a leaf-to-root traversal to update the node counts.
Alternatively, you can keep the tree balanced, and use the above answer.

Resources