How to delete a value in a B+ Tree? - b-tree

I need to delete 87, how do I do this, I cannot find anything that is the same issue.

You can just remove the 87. If your algorithm requires a node to be at least half full, you need to move the 85 and the 90 in the preceding node.

Related

How to create BST in post order-way - algorithm theory

I'm preparing to exam from algorithms and have exercise which I do not understand:
"Create a BST from given numbers and write it by post-order method. Choose correct order in this notation."
Numbers: 42, 30, 45, 55, 70, 53, 40, 33, 60, 50
Anserws:
So I've started to think about this excercise and decided to sketch BST graph like this:
And I was thought that if I will read numbers from graph in post-order way, I will receive answer, but no. I think BST tree is ok, maybe I should create BST already in post-order notation somehow?
You have made a mistake in drawing the BST. The regulations for making a BST is,
the left-subtree's value for each node, should always be lesser than the current root.
the right-subtree's value for each node, should always be greater than the current root.
Now consider the sub-tree, that you have drew:
45
55 70
53 60
It's not following BST's property, re-draw the diagram, and try for Post-Order as demanded.
The property of Post-order is following:
1. visit left sub-tree.
2. visit right sub-tree.
3. visit the root.
I think BST tree is ok,
You made a mistake in drawing the BST when you inserted 53. It should not become the left child of 45, but its right child. It was the only mistake, but it obviously had big consequences for the rest of the values that you inserted in the BST.
I was thought that if I will read numbers from graph in post-order way, I will receive answer
That is right. If you recreate the BST without mistakes, this is the correct approach.

Can we replace the deleted node by its direct child nodes (instead of replacing by its inorder predecessor or successor) in Binary Search Tree?

As we know that to delete any node in BST we replace that deleted node with its inorder predecessor or successor.
I've tried a new approach in which I replace the deleted node either by its direct left child or by its direct right child (instead of replacing by its inorder pred. or succ.). And I think that this approach is valid for every node in BST. Program for this approach will be also easy as less number of links are changed for a node.I am attaching 2 pics to make you understand my approach.
Is my approach of deleting a node in BST is right or wrong? If wrong then why?
This can get pretty complicated, but you'll probably want to look into some rotations.
Say you have a full tree with 5 levels, and you are trying to delete the roots right child, which contains a quite a few more nodes. The issue is that by simply replacing with it's left or right child, would result in the deleted index "having" more than two child, which as I'm sure you know, is an invalid BST.
The solution? Rotations!
Here are a couple links that explain with some pictures.
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/9-BinTree/BST-delete2.html
While this is a valid way to delete a node in a binary tree, it won't always work for binary search trees. Let's say you want to delete 40 and 35 has a right child, by connecting 35 to 45, you'll be losing its original right child and every other node connected to it (you'll be losing a subtree). For binary search trees (BST), its better to replace the node with the rightmost node of the left subtree, which in this case is still 35 (this guarantees it does not have a right child) or the leftmost node of the right subtree if there is no left subtree.

Why is the successor of a BST node defined as the one larger than the deleted one?

In the following image, if I add 14 to the right side of 12, then 14 can replace the 15 without influencing other nodes, just like the correct answer 16. Why the successor is defined to use the number that is bit larger than it not the one that is a bit smaller?
Well, in terms of language, successor is the one that comes right after, implying that it must be bigger.
In terms of the deleting algorithm, you can use both the successor and the predecessor to replace the deleted node.
Successor: is the smallest node in the right subtree of the deleted node, which means that it is the smallest node that is bigger than the deleted node, so you guarantee that, if you replace the deleted node with it, it will still be smaller than every other node in the right subtree, so it won't break any property.
Predecessor: is the biggest node in the left subtree of the deleted node, which means that it is the biggest node that is smaller than the deleted node, so you guarantee that, if you replace the deleted node with it, it will still be bigger than every other node in the left subtree, so it won't break any property.
In a nutshell, you can use the successor or the predecessor without any problems, is not a question of definition, only a question of choice.

Binary search tree rotation

I want to create an algorithm to sort binary search trees to make them balanced. But I can't do that If I don't understand myself how each step works!
So I have a binary search tree that I want to rotate to make it balanced. I already have and know the answer as shown by figure 2, but am unsure of the steps necessary to get to that point
Figure 1:
Figure 2:
Any pointers would be great.
Also I am using http://visualgo.net/bst.html
The thing you need here is called tree rotation
What you have done from step 1 to step 2 is a left rotation for node 16 like this:
Node 16 is imbalanced and size(left child) < size(right child) =>
We need to transfer the 16 node to left child,
Get a new node (the smallest) from the right child as the new node (18 since is the smallest)
Rebalance the right child

I don't understand A* Pathfinding

From what I understand:
Add the current node to the closed list.
Find adjacent nodes to the current node, and if they are not unwalkable nodes and not on the closed list, add that node to the open list with the parent being the current node and calculate the F, G and H values. If the node already exists on the open list, check if going to that node through the current node will result in a lower G value - if yes, make the parent node of that node the current node.
Find the node on the open list with the highest F value, and make the current node that node.
Repeat until you end up in your destination, then go through the parents of the destination node, and you will come back to your start node. That will be the best path.
So, this makes sense to my brain, but when I actually try it out on a diagram, I think I'm not understanding it correctly.
(From the picture below) Go down from the starting green tile, the one with a F value of 60. That is on the open list, and has a lower F value than bottom-right 74 one. Why is the 74 one picked instead of the 60?
In my opinion, you should take a look at Amit's A* Pages. They really are great to explain how the algorithm works and how to make it work.
As for your case, the diagram shows the G score from the first node on the open list. When you look at the website, the whole diagram is built first for the first node evaluation, and the author shows that the first best node is the one to the right. Then, moving forward uses the G score based on the score of the current node plus the moving cost of the next one, which is not shown on the diagram.
It is said on the website though:
And the last square, to the immediate left of the current square, is checked to see if the G score is any lower if you go through the current square to get there. No dice.
It's G score would actually be 24 (14 (current cost) + 10 (horizontal movement cost)), just like the square below it, if I remember correctly.
The reason that you do not move to the square with an F-value of 60 is because it is in the 'closed list'. Where as the squares with an F-value of 88 and 74 are in the 'open list' and can be examined for the next move.

Resources