Can there exist a balanced binary tree that is not a balanced binary search tree? What is the time complexity? - algorithm

Can there exist a balanced binary tree that is not a balanced binary search tree? If so, what would the time complexity be to search a node in such a tree.
My understanding is this:
Binary Tree: any node has two maximum leaf nodes. Searching in a binary tree, using DFS or BFS is O|V+E|
Binary Search Tree: A BST is a tree of ordered nodes. Searching in a binary search tree, using DFS is O|log n|
Balanced Tree(assuming height-balanced): Maximum number of levels below the root is kept to the minimum.
Does being balanced have any effect to the time complexity of search?
So, essentially, can I create a binary tree that is height-balanced but not ordered. Would the search time of this tree be O|V+E| or will it be better?

Searching an unordered binary tree requires visiting every node, so it's O(N) whether it's balanced
50
__/ \__
/ \
25 26
/ \ / \
49 46 48 47
or not
50
__/ \__
/ \
25 26
/ \
49 46
/ \
5 6
There's really no point in balancing an unordered tree.

Related

What will be the root after deleting it from a binary search tree?

I am looking at this question:
Is the solution going to be node 23 because we would first apply a left-right rotation and then a deletion?
There are different algorithms for deleting a node from a binary search tree.
One is as follows:
If the node is a leaf, then just remove it and stop
If the node has one child, then replace the node by its child and stop
If the node has has two children:
find the node's predecessor (its in its left subtree)
Copy that predecessor's value in the original node
Delete the predecessor node by applying one of the first two steps above.
The given tree has a root with two children, so we first find its predecessor, which is the node with value 23. Then that node (its value) is moved to the root node, and in its stead comes the node with value 19:
50 23
/ \ / \
17 72 => 17 72
/ \ / \ / \ / \
12 23 54 76 12 19 54 76
/ \ / / \
9 14 19 9 14
The resulting tree is still balanced, so no rotations happen here. Moreover, the type of rotations to apply may depend on which type of self-balancing binary search tree is implemented: AVL, Splay tree, Red-black tree are examples of such trees which will rotate in different conditions and at different places in the tree.

Do Red-Black tree and AVL tree have same balance condition?

For example:
An unbalanced tree:
4
/ \
1 11
/
7
/ \
5 9
After balance, it will become AVL tree:
11
/ \
4 7
/ / \
1 5 9
However, if the unbalanced tree is R-B tree like this:
(\\ means read pointers. \ means black pointers.)
4
/ \\
1 11
/
7
// \\
5 9
Is this a legal R-B tree? Or I should make it balance just like what I did to the tree?
The balance condition of AVL trees is different from the balance condition of Red-Black trees. An AVL tree is, in a sense, more balanced than a Red-Black tree.
In an AVL tree, for every node v, the difference between the height of v's right sub-tree and the height of v's left sub-tree must be at most 1. This is a very strict balance property compared to the one imposed by the Red-Black tree. In a Red-Black tree, no simple path from the root to a leaf is allowed to be more than twice as long as any other. This property results from the five color conditions a binary search tree must satisfy in order to be considered a valid Red-Black tree.
Your example Red-Black tree is indeed not balanced in the AVL sense because the difference between the height of the root's left sub-tree and the height of its right sub-tree is 2. Nevertheless, the tree is balanced in the Red-Black sense as it satisfies the five Red-Black color conditions.
The AVL balance condition implies that the height of an AVL tree is bounded by roughly 1.44log(n) while there's nothing preventing the height of a Red-Black tree from being greater: the Red-Black tree balance condition only implies a bound of 2log(n) on the height.
The fact that AVL trees tend to be shorter than Red-Black trees seems to suggest they must perform better. This is not necessarily the case: an AVL tree is indeed generally faster to search because it's more balanced than a Red-Black tree. But the reason an AVL tree is so well balanced is that keeping it balanced is computationally harder than keeping a Red-Black tree balanced. In particular, the AVL rotations make insertions and deletions in an AVL tree slower in comparison to these operations in the corresponding Red-Black tree.

Difference between complete binary search tree and AVL tree?

Is there any difference between the complete binary search tree and an AVL tree? Give an example.
Searched on google but found this. not much helpful
Every complete binary tree is an AVL tree, but not necessarily the other way around.
A complete binary tree is one where every layer except possibly the last is completely filled in. An AVL tree is one where every node's children are AVL trees whose heights differ by at most one. The maximally skewed AVL trees are the Fibonacci trees, which generally aren't complete trees. Here's an example of a tree that's an AVL tree and not a complete binary tree:
.
/ \
. .
/ \ / \
. . . .
/ / / \
. . . .
/
.
AVL tree and Binary search tree are both same but AVL tree has a constraint that the difference between the height of left sub tree and right sub tree should either be 0, 1 or -1.
If any binary search tree meet these conditions it will be called as AVL tree.
Binary search tree + HEIGHT CONDITION is an AVL tree.
Refer: Introduction to Algorithms by Cormen
https://books.google.co.in/books..

Find a binary tree given only in-order traversal

I'm given an in-order traversal and need to find a binary tree. I referred my sites and most of them said it is not possible. However, i think a non-unique binary tree is possible. Can i find a binary tree using just given in-order traversal? If not, can i find a corresponding pre-order traversal from the given in-order traversal?
I tried to convert the in-order into pre-order by selecting the central node of in-order as the root but I'm not sure if its correct. Please guide me.
Thank you.
Given just the in-order traversal of the nodes, and no more information in the question, you can find a binary tree, but as you said, there won't be a unique solution (especially if the tree doesn't have to be balanced). As a result, you can again find a pre-order traversal.
As an example, if your in-order traversal is [1,2,3,4,5,6,7,8], then even if the tree is balanced, there are multiple possibilities for the root node (namely, 4 or 5). If the tree doesn't have to be balanced, you could potentially pick any of these as the root node.
Here's an example of a non-balanced tree you could build after arbitrarily choosing 4 as the root node:
4
/ \
3 6
/ / \
2 5 7
/ \
1 8
Pre-order traversal for this tree would yield 4,3,2,1,6,5,7,8. Again, if the only requirements are that you just find a binary tree, this is just as valid as setting 1 as the root node and making everything else a right node:
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
The pre-order traversal for this tree would be 1,2,3,4,5,6,7,8. Since these trees both generate the same in-order traversal, but different pre-order traversals, there isn't guaranteed to be a single, unique tree or even a single, unique pre-order traversal for a given in-order traversal.
For more of one node there is no a unique binary search tree that contains the inorder traversal.
But if you want a tree, then just perform a random shuffle of inorder sequence and then insert the resulting randomized sequence in a binary search tree

Given the level order traversal of two complete binary trees, how to check whether one tree is mirror of other?

How to check whether two complete binary trees are mirror of each other where only the level order traversal of the trees are given ?
A Complete binary tree is a binary tree which all the nodes except the leaf nodes have 2 child nodes.
I don't think this is possible. Consider these two trees:
0 0
/ \ / \
1 2 1 2
/ \ / \ / \
3 4 3 4 5 6
/ \
5 6
These are complete binary trees (according to your definition), and even though they're different trees they have the same level-order traversals: 0123456.
Now, look at their mirrors:
0 0
/ \ / \
2 1 2 1
/ \ / \ / \
4 3 6 5 4 3
/ \
6 5
Notice that the left tree has level-order traversal 0214365, while the right tree has level-order traversal 0216543. In other words, the original trees have the same level order traversals, but their mirrors have different traversals.
Now, think about what happens if you have your algorithm and you feed in 0123456 (the level-order traversal of either of the trees) and 0214365 (the level-order traversal of one of the mirrors). What can the algorithm say? If it says that they're mirrors, it will be wrong if you fed in the second of the input trees. If it says that they're not mirrors, it will be wrong if you fed in the first of the input trees. Therefore, there's no way for the algorithm to always produce the right answer.
Hope this helps!
According to general definitions, in a complete binary tree, every level except possibly the last, is completely filled, and all nodes are as far left as possible. So a complete binary tree may have a node with just one child (for eg, one root node with one left child is a complete binary tree). A tree where all nodes except the leaves have 2 child nodes is called a full binary tree.
For complete binary trees, the problem would be trivial. Starting from the top-down, for the ith level you need to compare 2^i elements (root being the 0th level) of the given level-order traversals A and B. For any given i, the set of 2^i elements from A should be equal to the reverse of these elements from B. However, the last level may not be completely filled and you'll need to account for that.
For full binary trees, where the only constraint given is that every node has 2 or no children, it won't be possible unless you have constructed the tree itself. And you cannot construct a tree by using only level-order traversal. templatetypedef provides a good example.

Resources