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:
Related
I am trying to build a BST based on an array.
my question is once I have a new value (TreeNode value) and want to insert it into my bst, and return the level of this TreeNode after its insertion. is there a (quick) way to know its level/depth information? Moreover, is there a way to know its level without actually build the BST. For example, I have an array [3,1,2], and I want to insert 4, is there a way to know what is the depth/level of 4 without actually build the BST and search for 4 to get is depth?
Thank you.
(If you don't mind, python code is preferred. sorry for any inconvenience.)
A complete BST has 2^k-1 nodes for some k>0. The k-bit binary fractions between 0 and 1 (not inclusive) listed in ascending order with trailing zeros removed serve as "instructions" for deciding where to place each node in the sorted list. For example, if k=3, then the tree has 7 nodes. The binary fractions are
001
01
011
1
101
11
111
Now suppose the sorted list to be placed in the tree is [10,20,30,40,50,60,70]. Interpret a 0 to mean "go left: and every 1 except the final one to mean "go right." The final 1 means "stop here." We start at the root. The match-up is
001 <-> 10
01 <-> 20
011 <-> 30
1 <-> 40
101 <-> 50
11 <-> 60
111 <-> 70
Following the "instructions", you'll end up with the list elements in the tree as follows:
40
20 60
10 30 50 70
Voila! A BST. This works for all k. Proving why is an interesting task! I will leave it to you to turn this observation into a working algorithm. There are several ways to do that.
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.
I'm given the following values:
17 5 20 33 41 30 28 55 17 26 35 19 11 14 60
Using the bottom-up construction, I've found myself in a situation where I cannot decide which node to swap in this case. This happens specifically when percolating the value 60 downwards and swapping. At a point, it reaches here (portion of the tree):
60
/ \
17 17
Now since we need to swap 60 with the smaller of the two child values, which one do I pick, left or right? My instinct tells me to pick the right child since the heaps are normally constructed Top-to-Bottom and sweep left to right since it calls for Heaps being Complete.
**note I could not tag this as homework due to insufficient rep.
It doesn't really matter, since the heap will come out fine no matter which one you pick. I would pick the right, because it's a little faster on average due to the slightly lower average depth.
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.
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.