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?
Related
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 not quite sure how to pick a root for a binary search tree (I'm wanting to do without any code):
5, 9, 2, 1, 4, 8 ,3, 7, 6
How do I pick a root?
The steps are confusing me for this algorithm.
You can initialize an empty BST (binary search tree), then iterate the list and insert each item.
You don't need to pick a root, just build the tree. But maybe you want balanced the tree, you can insert as first element the middle value of the list, but the right answer is to use a balanced binary search tree (AVL tree).
Median number will be a better choice, because you want to have less depth.
Here is one example, the root is find the median the next one is also find the median
5
3 8
2 4 7 9
1 6
5 is get by (1+9)/2. 3 get from ceiling(1+4)/2 (you can also choose the floor of the median as the role of choosing median root)
BST with the same values can have many forms. For example, a tree containing 1,2 can be:
1 <- root
\
2 <-- right son
or
2 <- root
/
1 <-- left son
So you can have a tree where 1 is the root and it goes 1->2->3... and no left sons. You can have 5 as the root with 4 and 6 as left and right sons respectively, and you can have many other trees with the same values, but different ordering (and maybe different roots)
How do I pick a root?
In whichever way you want to. Any number of your data can be the root.
You would like to choose the median though, in this case, 5. With that choice, your tree should get as balanced as it gets, four nodes on the left of 5 and four nodes in the right subtree of 5.
Notice that any element could be the rood (even a random choice, or the first number in your example).
Um, then why should I worried finding the median and not always picking the first number (easiest choice)?
Because you want your Binary Search Tree (BST) to be as balanced as possible.
If you pick the min or the max number as a root, then your tree will reach its maximum depth (worst case scenario), and will emulate a single linked list, which will result in a worst case scenario for the search algorithm as well. However, as Michel stated, picking the minimum or maximum item for the root won't necessarily lead to a degenerate tree. For example, if you picked the minimum item for the root and but the right branch that contains the rest of the items is balanced, then the tree's height is only one level more than optimum. You only get a degenerate tree if you choose the nodes in ascending or descending order.
Keep in mind that in a BST, this rule must be respected:
Left children are less than the parent node and
all right children are greater than the parent node.
For more, read How binary search tree is created??
I have tried many online sources but i am unable to understand how digital binary search tree works.Below Link is the example for your reference
(LINK: http://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec15/lec15-10.html)
Is anybody construct a tree using these values and tell in detail that how it works?
A 00001
S 10011
E 00101
R 10010
C 00011
H 10100
The tree is constructed in such a way that the binary representations of the keys (A,S,E,R,C,H) can be used to locate them into the tree. In each searching step, the key is compared to the curren node (which is the root of the current search three). If the the key is not the root, the most significant bit of the key's binary representation is used to select the left subree (if the bit is 0) or the right subtree (if the bit is 1). This process is explained in more detail here.
In the example you provided, the key H (binary representation 10100) can be found as follows.
In the first step, the root is node A. As A does not equal H, the bit 1 is used, indicating that the right subtree should be chosen. Consequently, we consider the node S and the bit string 0100 which results from the original binary representation by omission of the most significant bit.
Since A does not equal H, we use the most significant bit, which is 0, indicating a choice of the left subtree. We consider the node R and the bit string 100.
As R does not equal H, again we use the most significant bit, which is 1, which means that the right subtree is to be chosen. We consider the node H and the bit string 00.
Since H equals H, we have found the desired key and the search terminates.
The DST works like by checking the bit level by level. Checks the starting of the bit if o then moves to left or else to right. At the same time it compares the bit position with the level.
For example:
Root is in the O level,
if the bit to be inserted is in the 1 st level then checks the 1st bit,
if 0 then inserts as a left or if it is 1 then insert in the right.
Similarly for the second level, checks the second bit to be inserted and works like this for the remaining levels.
In the given example;
First A (00001) is a root node, then S(10011) since 1 , move to the right and inserted.
The next is E(00101) , since 0 moves to left and inserted, next in the sequence is R(10010), since 1 move to the right , the bit in the second position is 0 so it is inserted as a left child of S.
In the sequence next is C(00011), 0 so moves to the left since 2nd bit is 0 insert in the left side, next is H(10100), since it starts with 1, moves to right, it has to be inserted as 3rd level so check the 3rd bit position as it is with 1, it is inserted in the right.
Hope this will clear your doubt.
So the final DST looks like this
[DST] [1]: https://i.stack.imgur.com/Iet4n.gif
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.
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.