Find possible routes to locate a number in a tree - data-structures

Prompt :
Assuming there is a binary search tree that stores the integers from 1 to 1000 and we are looking for the number 363. Which of the following sequences of nodes CANNOT be applied to the tree?
a. 2,252,401,398,330,344,397,363
b. 924,220,911,244,898,258,362,363
c. 925,202,911,240,912,245,363
d.2,399,387,219,266,382,381,278,363
d. 2 ,399,387,219,266,382,381,278,363
e. 935,278,347,621,299,392,358,363
Thanks for any help.I really need it.

The problem will be in the last option (e) wherein the binary search property gets violated - it doesn't hold for nodes 347 and 299 since 621 is a right child of 347, all nodes to the right of 347 are supposed to be greater than itself, but 299 isn't.
Hope the below image clarifies.

Related

Build BST with an array and query a new node insertion level/depth

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.

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.

Why is this binary tree not a heap?

I have been teaching myself heaps for an exam and came across the following question:
"Give 2 different reasons why the following binary tree is not a heap"
91
/ \
77 46
/ \ \
68 81 11
I know one of the reasons this is because a heap's children must less than or equal to the value of its parent so 81 violates this rule as 81 > 77, but I am not sure on the other answer.
Could somebody please clarify?
11 should be the left-child of 46, not the right-child.
Wikipedia mentions that a binary heap should be a complete binary tree, which means "every level, except possibly the last, is completely filled, and all nodes are as far left as possible", which is clearly not the case if 11 is where it is now.
The reason why this is advantageous is fairly easy to understand - given the size of the heap, you can quickly determine where the last node on the bottom level is, which is necessary to know for insertion and deletion. If we're using an array representation, it's as simple as the element at heap size - 1 being the last element. For a pointer-based representation, we could easily determine whether we should go left or right to get to the last element.
There may be other ways to get the same performance without the heap being a complete binary tree, but they'd likely add complexity.
That is not a heap because it doesn't conform to the heap property.
In a min-heap, every node's value is less than or equal to its child nodes' values.
In a max-heap, every node's value is greater than or equal to its child nodes' values.
It's clearly not a min-heap because the root node, 91, is larger than either of its children.
And it's clearly not a max-heap because the node 77 is smaller than its right child, 81.
And, as #Dukeling pointed out in his answer, it doesn't conform to the shape property.

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!!

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.

Resources