Removing a node with left and right subtrees from BST with duplicates - data-structures

The DSA book describes this case of deletion of a node from the binary search tree:
"4. the value to remove has both a left and right subtree in which case we
promote the largest value in the left subtree."
Let's say, we have the following (I tried to make it look like a tree):
7
6 8
5 6 8
If we remove root (7), it says we should put 6 to its place. Now it would look like that (it just doesn't feel right):
6
6 8
5 8
Now 6 is a left node of 6. But it shouldn't, right (the values on the left should be less)? So, I guess my questions would be: is it okay to have such cases? If such cases are acceptable, is there a name for that? Or should we choose some other nodes to substitute the deleted one?

Is your assumption that all "equal" values be in the right sub-tree? If so, you can always replace the node with the smallest value in it's right sub-tree. If they can be on either side then you don't have a problem.

Related

Given a list of keys, how do we find the almost complete binary search tree of that list?

I saw an answer here with the idea implemented in Python (not very familiar with Python) - I was looking for a more general algorithm.
EDIT:
For clarification:
Say we are given a list of integer keys: 23 44 88 12 74 32 7 39 10
That list was chosen arbitrarily. We are to create an almost complete (or complete) binary search tree from that list. There is supposed to be only one such tree...how do we find it?
A binary search tree is constructed so that all items on a node's left subtree are less than the node, and all nodes on the right subtree are greater than the node.
A complete (or almost complete) binary tree is one in which all levels except possibly the last are completely full, and the bottom level is filled to the left.
So, for example, this is an almost-complete binary search tree:
4
/ \
2 5
/ \
1 3
This is not:
3
/ \
2 4
/ \
1 5
Because the bottom level of the tree is not filled from the left.
If the number of items is one less than a power of two (i.e. 3, 7, 15, etc.), then building the tree is easy. Start by sorting the list. Then, take the middle element as the root. So if you have [1,2,3,4,5,6,7], and the root node is 4.
You do the same thing recursively for the right and left halves of the array.
If the number of items is not one less than a power of two, you have to adjust the starting point (the root node) so that the bottom row is left-filled. Note that you might have to apply that adjustment recursively, as well, whenever your subtree length is not one less than a power of two.
Since this is a homework assignment, I'll leave that for you to figure out.

Which element is the 'middle' in a B-Tree of even order?

If I have an B-Tree of order 4 with the following data in it...
and I need to add 2 to the tree; do I...
add the 2 to the node (making it invalid, as it now has 4 keys), then split the node, taking the value 2 as the middle value and propagating it up
OR
do I not add the 2, take 3 as the middle value, propagate 3 up, then add 2 into the correct node?
Excuse the poor diagram.
You perform the first option. For a B-tree of any order you always add the node then perform splits that propagate upwards. For a great interactive demonstration of a variety of basic (insert, delete, search) operations on data structures, there is a useful algorithm visualization page I go to located here. Find the B-tree page and you will find that it performs option 1.
How to find which element to push upward:
1)Push the element in proper position of Btree and check if overflow occurs.
If then follow steps 2 and 3 given below.
2)find CEILING((order of Btree+1)/2).
3)Move that index element upward giving two pointers to left and right subtree.
Note:First insert the element then follow steps 2 and 3 if overflow occurs.
Here in this example first insert 2.
The partial leaf of the tree becomes |1| 2| 3| 5|.
overflow occurs because only 3 keys can there be in any node.
Find ceiling ((4+1)/2)= ceiling(5/2)= 3 (index no)
3rd index value 3 is the middle element. so propagate it up. 3's left pointer points to 1|2 and right points to 5.

Where would you add '4' to the above binary search tree?

Where would you add '4' to the above binary search tree? And why?
A) A
B) B
C) C
D) Any of the above
My TA said it was just A but I'm thinking why can't it be all of the above
It is only A. Starting from root if your number is less than 5, go left branch. If your number is greater than 5 go right branch. Same process for every node.
Answer: A
Options B and C both violate that BST property; i.e. the new key '4', whose value is smaller than '5', would end up in the right subtree of '5'. (The right subtree should have keys which are greater.)
A binary search tree works by following the left child if the value you are searching for is less than the current node and right if it's greater until you find a node with desired value or the desired child is an empty tree (null).
So to test A, B or C:
if 4 is greater than 5 and smaller than 8 and 6, B is the correct answer.
If 4 is greater than 5 and 8 but smaller than 42, C is the correct answer.
If 4 is smaller than 5 but greater than 3, A is the correct answer.
In some silly field of mathematics or perhaps a parallel universe all these 3 might be correct at the same time, but besides that only one of these are correct with standard number theory.
Or from a search perspective (look at your tree from the root while reading this):
4 is smaller than 5 so go left.
4 is greater than 3 so go right
right node empty, insert at A
Now where would 2 be inserted?

quickest way to find out if an element is in the left or right subtree

If a binary tree is constructed as follows
the root is 1
the left child of an element n is 2*n
the right child of an element n is (2*n)+1
if I get a number n, what's the quickest way to find out if it's in the left or right subtree of the root? Is there some easy-to-determine mathematical property of the left subtree?
note: this is NOT a homework question, though it is part of a bigger algorithmic problem I'm trying to solve.
Consider the numbers in binary. Each child will be the parent number with a 0 or 1 appended to it depending on whether it is left or right.
This means that everything to the left of the root will start 10 in binary and anything to the right will start 11 in binary.
This means that you should be able to work out which side it is on just using some shift operations and then some comparisons.
I should note that I don't know if this is the most efficient method but it is a easy to determine mathematical property of the left subtree.
As noted by others the consequence of the 0 or 1 appendation means that each digit encodes the path through the subtree. The first 1 represents the root of the tree and from then on a 0 will mean taking the left branch at that point and a 1 will mean taking the right branch.
Thus binary 1001101 would mean left, left, right, right, left, right.
An obvious consequence of this is that the number of binary digits will determine exactly how deep in the tree that number is. so 1 is the top (1st) level. 10 would be at the second level (one choice made). my example 1001101 would be at the 7th level (six choices made). I should note that I'm unfamiliar with Binary tree terminology so not sure if the root would usually be considered the first or zeroth level which is why I am being explicit about number of choices made too.
One last observation in case it hasn't already been observed is that the numbers will also be assigned counting from top to bottom, left to right. So the first level is 1. The next is 2 on the left, 3 on the right. The level below that will go 4, 5, 6, 7 and then the row below that 8, 9, 10, 11, 12, 13, 14, 15 and so on. This isn't any more useful mathematically but if you are trying to visualise it may well help.
Following from Chris' observation, there's a very simple rule: Let x be the node you are looking for. Let S be the binary representation of x. Then the digits in S after the first from most significant tell you the path from the root: 0 means go left, 1 means go right.
Example: x = 2710 = 110112, so we need to go right, left, right, right to get there (the leading 1 is ignored).
The reason why this is true is that if you go right, you multiply by 2 (binary left shift by 1) and add a 1, so you are essentially appending a 1. Conversely, if you go left, you append a 0.

Binary search tree deletion operation

I have a book that explains the over all binary search tree in a very bad way i have so far been able to close study my book and get an idea of the binary search tree however i find the explanation for the Binary search tree's operation Delete
I do understand the two first simple operations:
Deleting a leaf (node with no children): Deleting a leaf is easy, as
we can simply remove it from the tree.
Deleting a node with one child: Remove the node and replace it with
its child.
However the one with two children is really difficult for me to understand, i have already read on wiki and other sites to try and find a solution but i find the explanations to be kinda encrypted.
I was hoping that someone here could give me a more details or explain it in to me another way ?
If you understand the first two rules, then deleting a node with two children is not tough to understand.
A very simple way to think of it is, go to the in-order successor (or predecessor) of the node to be deleted. Then apply the first two rules and the previous rule again.
While programming, having a fully functional successor (predecessor) function makes coding deletion a lot simpler.
For this tree :
To delete 8 :
Go to 9 (7)
Replace 9 with 10
Replace 8 with 9 (7)
To delete 12 :
Go to 14 (10)
(Replace 9 with 10)
Replace 12 with 14 (10)
Can we say in short:
To delete a node N with 2 children in a binary tree (like the aforementioned ones), either replace this N with the largest node of the left sub-tree or with the smallest node of the right sub-tree
When the node has two children you have to:
Find the minimum.
Replace the key of the node to be deleted by the minimum element.
look at this picture:
we want to delete element 4
4 has 2 children.
find min right sub-tree.
5 found.
So, 4 is replaced by 5, and 4 is deleted.
Hope that is what you are looking for!!

Resources