Personal how does the tree look after the 40's removal?
In my understanding, instead of 40 would be number 41, right 44 and left 30. And the right of 44 the number 49, so right?
Well, you have to define which methodology to adopt. You can choose the left node, 30
, as the new node that will be in place of 40
and then in this case you will have to search for the node to the right of the 30
node, to allocate the node that was to right of 40
, 44
. Or you can also choose 44
as the node that will be in place of 40
, in which case you will have to look for the leftmost node of 44
to allocate what was left of 40
, in if 30
. Either way it depends on your methodology, and in this way will not unbalance your tree. It would not be feasible for you to attempt to rearrange it to put 41
in place of the removed node, because it would be too much trouble to ensure that the tree would not be unbalanced.
SlidebyProfessorMarcosCaetano-UnB.
Asyoucansee,yourtreewillcontinuetobebalanced
As far as I can tell, this tree is a binary search tree (ABB).
Whenever a node is removed, what will replace its position in the tree should keep the tree as still a binary search tree. That is, smaller numbers on the left and larger numbers on the right. For this you should get the child of the subtree to the left which is to the right and put in the place of the node that will be removed.
Inthecase,sincethereisnoleftsubtree,butonlyonenode,itisthatnodeontheleftthatwillbeputinplace.Thatis,the30willreplacethe40'spositionatABB.
Thisoccursbecauseofthefactthatthelargestelementintheleftsubtreeislargerthanthesmallestelementoftherightmostsubtree.
YoucanunderstandmoreaboutBinaryTreesearchremovalonthis video . It was from there that I took that image.
Just remove the one you want and take the larger or smaller left or right and put it in its place.