Is this B+ Tree Valid? - data-structures

In a B+ tree, can a non leaf node exist such that it's key value is deleted? What this means is that the B+ tree has a value in its intermediate non leaf node but not in any of its leaf nodes.
Consider the following structure. I came across this while studying B+ trees. In this structure 13 is not a leaf node. But it is a non leaf node. (In fact it was deleted in the previous instructions. Link for the picture. In this link go to the bottom of the page)
If yes then how come the data is deleted?
Is this a mistake or is there something I am missing?

The image you posted is valid. The only data returned by this tree is what you find in the last row. Because 13 has been removed from the tree, it has been removed from the last row. The fact that 13 exists in a non-leaf node is of no consequence to your results, it is just there to be a comparable value when traversing down the tree. In this case the tree would behave no differently if the 13 was changed to a 16 (based on the above convention).
Douglas Fisher did a comprehensive video series on B+ trees, which can be easier to learn from than reading articles. Part 1 can be found here.
Edit: My response in the comments was too long so I will put it here. Plus this is useful info.
If you are searching for a 12, and reach the 13, you will compare IS 12 < 13 or 13 <= 12, the left is true, so you will traverse down to the left leaf. This would have happened whether or not 13 was 16, because this is also true 12 < 16.
If you are searching for 16 and reach the 13 you will compare IS 16 < 13 or 13 <= 16, the right expression is true, so you will traverse down to the right leaf. Then you will find the value of 16 there.
If you are searching for 13 (which doesn't exist) you will ask IS 13 < 13 or 13 <= 13, the right expression is true, so you will traverse down to the right leaf and find that there is no 13 there and you will kick out at this point that 13 has no value.

Related

Insert/remove the same element in heap sort

Show the heap at each stage when the following numbers are inserted to an initially empty min-heap in the given order: {11, 17, 13, 4, 4, 1 }. Now, show the heap at each stage when we successively perform the deleteMin operation on the heap until it is empty.
Here is the answer/checkpoint I receive:
![1]https://imgur.com/zu47RIF
I have 2 questions please:
I don't understand when we insert element 4 the second time, why do we shift 11 to make it the right child of the old element/firstly inserted element 4? Is it because we want to satisfy a requirement of the complete binary tree, which is each node in the levels from 1 to k - 2 has exactly 2 children (k = levels of the trees, level k is the bottom-most level)?
I don't understand how we deleteMin = 1, 13 becomes the right child of the newly parent 11 (which is the left child of 4). Just a quick note that my instructor gave the class 2 ways to deleteMin. The other way is fine with me - it's just the reversed process of inserting.
Like you said, the heap shape is an "almost complete tree": all levels are complete, except the lowest level which can be incomplete to the right. Therefore, the second 4 is necessarily added to the right of 17 to preserve the heap shape:
4
/ \
11 13
/ \
17 4
After that, 4 switches places with 11 to regain the min-heap property.
Deletions are typically implemented by removing the root and putting the last (i.e., bottom-rightmost) element in its place. This preserves the heap shape. The new root is then allowed to sift down in order to regain the min-heap property. So 13 becomes the new root:
13
/ \
4 4
/ \
17 11
Then 13 switches places with either child node. It looks like they chose the right-hand child in your example.

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.

binary search tree paths

In a binary search tree if you are along a certain search path, what keys are considered to be on the left of the path and which on the right?
For example if my tree is :
25
12 30
10 15 28 32
14 20
And lets say my current search path is 20->12->15->20.
1) Are both 10 and 14 considered to be on the left of this search path or is only 10?
2) Also are all 3 numbers 30, 28 and 32 considered on the right of the search path?
Assuming you meant the path 25-12-15-20, I would say both 10 and 14 are left of this path. That's because in order to find either of them, you have to backtrack up the tree (to either 15 or 12) and take a left branch where the path goes right.
Same deal for the three numbers 30, 28 and 32. Since you have to backtrack up to 25 and then go right instead of left, they can be considered on the right of that path:

can a node be inserted at a non leaf position in a binary search tree?

can a node be inserted in a non leaf position in a binary search tree ?
for eg. if we have the following set of numbers to be arranged as a binary serach tree :-
20.,17,15,19,23,25....
so there is more than 1 way these numbers can be arranged as a bst :-
20
17 23
15 19 25
20
15 23
17 25
19
25 make it a root and place the other nodes accordingly....
I think you could devise some way to do that. But the normal algorithm for inserting into a BST doesn't do it and I don't see any reason why would you want to do that. Also, I don't know about any other publicly known algorithm that does that.
A binary search tree is just a tree with certain properties. You can use different algorithms for inserting nodes into such a tree, as long as these algorithms ensure that the properties required for a BST hold. So, yes, you can add nodes in other places than leafes if you want to.

Left balanced binary trees

I am reading a book on data structures and it says that a left balanced binary tree is a tree in which the leaves only occupy the leftmost positions in the last level.
This seemed a little vague to me. Does this mean that leaves are only on the left side of a root and are distributed throughout the whole level, or leave exists only on the left side of the entire tree. Exactly what constitute left balanced?
I am not sure if my guess even covers any of the answer, so if anyone could help, it would be greatly appreciated :-).
You can think of a left-balanced binary tree as a balanced binary tree where the left sub-tree of each node is filled before the right sub-tree. In more informal terms, this is a tree wherein the nodes at the bottom-most level are all on the left side of the entire tree.
Take this tree for example:
This tree is balanced, but not left-balanced. If node 67 were removed, however, the tree would be left-balanced.
It seems to me that the definition of left balanced binary tree is the same of complete binary tree.
I don't really know the answer, but based on the description in the book it sounds to me like this...
For starters, lets think of it this way. Every "row" in the tree has a number, starting at zero and counting up. The row with the highest number is considered to be the last level.
Remember that leaf nodes are nodes without any children. So the tree meets the condition that every leaf node in the last level must be on the left, like so:
50
/ \
/ \
35 70
/ \ / \
10 34 57 90
/ / /
9 7 78
If we were to add a "98" as the right-child of 90 or a "59" as the right-child of 57, then this tree would no longer be left-balanced.
Edit: After reading Brandon E Taylor's answer, you should definitely not choose this one. After looking it over and reading the description again, not only does his make more sense, but it fits the description much better.
In addition to Brandon E Taylor's answer, a left balanced binary tree is a binary tree that when represented in an array there shouldn't exist any gaps in between the elements representing the tree nodes.
for a tree of size 6 for example, here are some possible array representations with the following criteria
1- let _ denote an empty slot in the array
2- let i be the index of a slot in an array (i is 1-based)
2- for any parent at index i the left child is at index (2*i) and the right child is at index (2*i)+1
1 2 3 4 _ _ <- left balanced
6 3 2 4 1 <- left balanced
4 2 1 _ 6 <- not left balanced
to elaborate further more, let's represent user265312's answer:
50 35 70 10 34 57 90 9 _ 7 _ _ _ 78 _ <- not left balanced
meanwhile Brandon's answer (after removing node 67) doesn't violate the left balancing rule:
50 17 72 12 23 54 76 9 14 19 _ _ _ _ _ <- left balanced

Resources