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

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.

Related

Which tree is balance and AVL balanced?

From my understanding, a complete tree is a balanced tree that may not be AVL balanced.
A complete tree is one where all levels are completely filled except maybe the deepest, at depth n, the height of the tree needs to be as far left as possible.
Am I therefore right to say that both these trees are Complete and AVL balanced?
Example A:
4
/ \
2 5
/ /
1 3
Example B:
4
/ \
2 5
/ \
1 3
Example A is neither complete, nor AVL.
It is not complete because at the bottom level not all values are as far left as possible (there is a gap). If you would iterate the bottom level and include null for where the parent node has no corresponding child, it would be 2,null,3,null,... while there should be no value occurring after such null.
It is not AVL, because it violates the order: no nodes in the right subtree of 4 should be less than 4.

why this binary search tree can't be a red black tree?

I had a question that asked to explain why this balanced tree can't be a red black tree and also wanted to transform it into a red black tree using only one rotation:
Why can't this binary search tree be a red black tree?
In a red-black tree, every path through a leaf has the same number of black nodes, and at least every second node on the path will be black, since a red node can't have a red child. Therefore, the longest path to a leaf in the tree (the one with the most red nodes) is at most twice as long as the shortest path to a leaf.
17 -> 11 -> 3 -> 5-> 7
has more twice as many nodes as
17 -> 19
so this cannot be a red-black tree.
If you rotate right at the root, then the tree will be more balanced and could be colored as a red-black tree.
Property 5 of red-black trees is violated.
Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
Given that the root is black, the path to the nils at the end of 7 are imbalanced with respect to the path to the nils at the end of 19. 19 has 1 black node to traverse; 7 has about 3.
This tree can't be considered a red-black tree since it's not properly balanced.
As other's have explained if
17 & 19 were both black, then the path 17,11,3,5,7 breaks the black height. If every other node was red-black-red etc, then 17 (black), 11 (red), 3 (black), 5 (red), 7 (black) would have 3 black nodes where as the path 17,19 would have 2 black nodes.
But suppose 7 as a node did not exist. Then the remaining tree could be a red-black tree:
And suppose 7 was inserted. Then with some color flipping and a right rotation about 17 you get

Deleting a node in a binary tree having children

If we have to delete a node from a binary tree, how should we handle the children of the node being deleted. E.g. in this tree:
100
/
10
/ \
5 20
where 100 is the root node, 10 is left child of 100, 5 is left child of 10 and 20 is right child of 10. So after deleting 10, what will happen to 5 and 20?
It is your choice if what should you do after deleting.You would probably want to move one of the child upward on some so certain criteria. Either of of Child will take place of parent.
delete node in Binary Search Tree.
delete node in AVL.
Heapify in Heapsort.
And more.
So it is always a choice you have to take to solve for objective.

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

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.

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