• 2 days ago
Transcript
00:00Now we are going to learn an important concept in BST which is how to delete a node
00:04We have learned how to insert the node and search
00:08But to delete it is also important
00:11The function of deleting or code of deleting the interviewers also ask you directly
00:17In Adobe it has been asked in some interviews
00:20And in fact it is a little tricky to understand
00:22That's why we understand it very well
00:25Only this is why we will create BST and delete it
00:28It can also be asked as an individual question
00:31From this perspective we are going to learn it
00:34So when we have to delete a node in BST
00:37Like this example of BST
00:41There are three cases in which way our node can exist
00:45Let's assume that the first case will come
00:47We have to delete a node which is a leaf node
00:50Which is not a child
00:51Like 4 is a leaf node which is not a child
00:541 is a leaf node which is not a child
00:5714 is a leaf node
00:58If we have to delete it it will be a different process
01:00The other case will come with a single child
01:03Like 11 comes with a single child
01:0710 is a single child
01:09It means it is only right sub-tree
01:11If we talk about 8
01:15So if we have to delete it
01:17If we have to delete it
01:17If we have to delete it
01:19If we have to delete it
01:20Then we will use case 2
01:21Then we will use case 3
01:23In case 3
01:24There are those nodes
01:24Which are two children
01:26Like node 8
01:28Or node 5
01:29Or node 3
01:33So we will use case 3
01:36Now depending upon the case
01:37This is a different process
01:38To delete it
01:40But before these 3 nodes
01:41To delete it
01:42First of all
01:43It is necessary that
01:44We will search that node
01:45In the beginning
01:45We will search that node
01:47To delete it
01:48After that
01:49We will delete it
01:51Using the case
01:54The process of search
01:56We already know
01:57That if I have to delete it
01:59If I have to delete it
01:59Then I have to delete it
02:00First of all
02:00I have to search for 4
02:02What do we need to do
02:03To compare it to 8
02:03To compare it to 4
02:05Then in left sub-tree
02:06To compare it to 5
02:06To compare it to 4
02:08Then in left sub-tree
02:09To compare it to 3
02:09To compare it to 4
02:11Then in right sub-tree
02:12And finally
02:12I will reach 4
02:13Now my job is to delete it
02:15First of all
02:16We need to write the code
02:18To delete it
02:19If we have to delete it
02:19If we have to see case 1
02:20Case 1 is leaf node
02:22So for that
02:23We have to do two steps
02:23First of all
02:25To delete node
02:26Second of all
02:26Return null to the parent
02:28In the same way
02:29We have to return to the node
02:31In the same way
02:32We have to return to the node
02:32In the same way
02:32We have to return to the node
02:34If we have to return to the node
02:36I will return null value
02:39What do we need to do
02:40What do we need to do
02:41To attach the right sub-tree
02:41To the node
02:43So this node
02:43will automatically delete
02:44Who is our garbage collector
02:50So for case 1
02:51To delete the node
02:52To delete the node
02:53I will simply
02:54To return the node
02:55To a null value
02:56To return the node
02:57To the node
02:57Which will make the node
02:59Left sub-tree
03:00Or right sub-tree
03:01And automatically
03:02Our node will be deleted
03:04The second case
03:05Is our single child
03:07To replace the node
03:08To replace the node
03:09With the child node
03:11Like
03:11In this case
03:13And then
03:13There will be
03:14If I have to return to the node
03:15But with the node
03:17And fill the node
03:18To the node
03:19And fill the node
03:19With the node
03:19That the node
03:21In this node
03:25Then fill the node
03:28we will travel directly from 8 to 11.
03:31So when a single child becomes a child,
03:33we will attach that child to our parents.
03:35For example, if I have this sub-tree
03:42which is 4, 3, 5 and 6
03:46and if I have to delete 5,
03:48what will I do?
03:53The left sub-tree will be the same.
03:54For example, I connect with 4,
03:56I connect with 6.
03:57So here it will come 6
04:00and 5 will be deleted.
04:02If I have a single left child,
04:05like 4, 3, 2, 5.
04:11If I have to delete 3,
04:14what was the process of deleting it?
04:16I will attach 3 directly with 4 to 2
04:19so that my BST looks like this.
04:22Look at this.
04:24So in case 2,
04:25we don't have to do anything.
04:26Our single child,
04:27we have to attach it with our parents.
04:30Now the single child,
04:31it can also be a left sub-tree
04:34or a right sub-tree.
04:37Then we have our case 3.
04:39Now the case 3 is the most special.
04:41We have to take care of it.
04:43For case 3,
04:44our first step will be
04:46to find out the in-order successor.
04:49What is the in-order successor?
04:50What is the in-order successor?
04:50When we get out of any tree
04:51in-order sequence,
04:53such as this tree
04:54in-order sequence will be
04:551, 3, 4, 5, 6, 8, 10, 11 and 14.
05:00If we have to delete node number 5,
05:03then the 5 node comes here.
05:06The next node will be the in-order successor.
05:11This is the 6 node.
05:12This will become the in-order successor.
05:14First of all,
05:15we have to find out the in-order successor.
05:17Why do we find out this?
05:30If we need to find out the next node,
05:31what is the next node?
05:32It will be the 5 node and
05:33the next node is our 6 node.
05:34We will ensure that
05:36the in-order successor
05:37will be the 6 node.
05:38First of all,
05:39we will find the in-order successor.
05:41The next step is
05:42to find out the in-order successor.
05:45It means,
05:45the 6 node.
05:46Then we will replace
05:47the in-order successor value.
05:51So, from the 5th,
05:53we will find out the in-order successor.
05:55Next step is
05:56to delete the in-order success.
05:58we will delete this 6 from our tree
06:01so we have already taken the value of 6 here
06:03we will delete this tree
06:05now if we remove this new tree
06:07we will remove this new tree
06:101, 3, 4, 6, 8, 10, 11 and 14
06:14here you will notice that
06:15we had to delete the 5 node
06:16it was automatically deleted
06:18and the 5 has replaced the 3
06:21in order successor
06:22now the in order successor
06:24by default
06:26if we try to find the in order successor
06:28then it will come
06:32leftmost node
06:33in the right sub tree
06:36in order
06:38successor
06:41in
06:42bst
06:43what will come
06:45leftmost
06:46node
06:48in
06:49right sub tree
06:54what happened
06:55what happened
06:56because if
06:57in order successor
06:58find out
06:59inside the bst
07:00there are many nodes
07:02there are many nodes
07:06in order successor
07:10so
07:10this tree
07:11will be like
07:11that all nodes
07:12in the right side
07:14will increase the root node
07:16from the root node
07:18so I have to remove the minimum value
07:22from the right sub tree
07:26and where the minimum value
07:29will get the right sub tree
07:29will get the leftmost node
07:32if I try to write some values
07:34here
07:35my values can be here
07:3810
07:3920
07:4215
07:4318
07:4414
07:4513
07:45here
07:4625
07:4821
07:4928
07:51so if I have to find
07:52in order successor
07:53then I will come here
07:55until I go left
07:57until I have leftmost node
07:59until I have leftmost node
08:00leftmost node
08:01is the one
08:01that no left child
08:03will be leftmost node
08:03so 13
08:04will be leftmost node
08:05and in this case
08:07the in order successor
08:09will be 13
08:10so this property
08:12we have to remember
08:13that our in order successor
08:15when we find out
08:16in bst
08:16we have to find out
08:17for any node
08:18which we have to delete
08:19it will always be leftmost node
08:22in the right sub tree
08:23because
08:24it will be minimum value
08:25in our right sub tree
08:29besides
08:29one other thing
08:30that in order successor
08:31will always take 0
08:32or 1 child
08:34because it is leftmost node
08:35so it will not exist
08:36its left child
08:37either it can be 0 children
08:39it can be a leaf node
08:40or 13
08:41leaf node
08:42or it can be a right child
08:44like
08:44here
08:45if there is another value
08:47here
08:47if there is another value
08:47meaning 7
08:48what happens here
08:506
08:50becomes our in order successor
08:53because it is leftmost node
08:54and 7
08:55is the right child
08:56so 7 is not in order successor
08:57it is 6
08:58so
08:59when we find out
08:59in order successor
09:00we will delete
09:01either it will delete
09:03from case 1
09:04or it will delete from case 2
09:06let's summarize
09:06a little bit
09:07which we have talked about
09:09we have talked about
09:11we have talked about
09:11we have talked about
09:12first
09:14find in order successor
09:15in this case
09:17in this case
09:18how to find out
09:19minimum node
09:20in the right sub tree
09:22we will know
09:22how to run a loop
09:23until the leftmost child
09:25will not come
09:25until the leftmost child
09:26will be running
09:26there we will find out
09:28our in order successor
09:292
09:30we will replace
09:31our node
09:31which we have to delete
09:32its value
09:33from in order successor
09:353
09:353
09:36finally
09:37in order successor
09:37we will delete
09:39it is a very simple process
09:41and this case 3
09:42is the most important
09:43if we have a doubt
09:44if we have a little doubt
09:45then we will rewind
09:47a little bit
09:48and then we have to understand
09:49very slowly
09:49the whole story
09:51in fact
09:52we have to think about
09:52two examples
09:53which we have to apply
09:55our case 3
09:56so
09:57to delete
09:57we are going to write
09:58the code
10:00before we are going to
10:01write the code
10:02we will update
10:03on the same tree
10:03we will run
10:04we will run
10:04which we have taken
10:06an example
10:06we call our function
10:20public static node
10:23delete
10:23we will pass our root
10:26and the value
10:27which we have to do
10:28delete
10:30first
10:31we will search that node
10:32which we are going to delete
10:34so
10:35if
10:37our root
10:38data
10:39is less than
10:40our value
10:42what we have to do
10:44in that case
10:44we have to do
10:44in that case
10:44we have to go to
10:45right subtree
10:46because if the value
10:47is greater than the root
10:48data
10:48we have to delete
10:50right subtree
10:51so
10:52root.right
10:53is equal to
10:54delete
10:55root.right
10:56and value
10:58so
10:59in the right subtree
10:59we will delete
11:00the right subtree
11:02there
11:03we will update
11:04in the right
11:05pointer
11:06another case
11:07is
11:08root.data
11:09is greater than
11:10our value
11:11so
11:12in this case
11:12we will go to the left subtree
11:14and we will update
11:14the left subtree
11:16delete
11:17root.left
11:19and value
11:21third
11:22is our case
11:23in which
11:23our root
11:24data is equal to
11:26null
11:27this is our
11:27Voila case
11:28where we have
11:29the value
11:32and the node
11:32to delete
11:33here
11:34we will start
11:35to see our cases
11:37first
11:37case 1
11:38which is
11:39leaf node
11:42so
11:43if
11:43root
11:43is equal to
11:45equal to
11:45null
11:46root
11:47is equal to
11:49null
11:49both
11:50left and right
11:51child
11:51are null
11:52so
11:52in this case
11:52leaf node
11:53is a leaf node
11:54if we want to delete
11:54the leaf node
11:55simply return
11:56null
11:57the parent
11:59null
11:59will store
12:03null
12:03so
12:04this node
12:06will automatically
12:06delete
12:07after that
12:09case 2
12:10case 2
12:11was single
12:12child
12:13what to do
12:14in single child
12:14we will check
12:16if
12:16root
12:17is equal to
12:20now
12:21both
12:21is equal to
12:21null
12:22because
12:23leaf node
12:23is covered
12:24so if
12:25root
12:26is null
12:26then
12:27root
12:27is not null
12:29is equal to
12:29so we will return
12:33root
12:33right
12:34here
12:35another case
12:37is
12:38that
12:38if
12:39root
12:39is equal to
12:41right
12:41null
12:42what will
12:43do in that case
12:44we will return
12:44root
12:45left
12:45because
12:46the parent
12:47will return
12:54left
12:54now
12:55we have
12:56case 3
12:57what was
12:57case 3
12:59we have
12:59both
13:00children
13:01root
13:01also
13:02right
13:02what is
13:03in this case
13:04first
13:05we have to
13:08that
13:09in order
13:09successor
13:09for
13:10in order
13:15find
13:16in order
13:20successor
13:21name
13:22is
13:22successor
13:22name
13:23which already
13:24exists
13:24here
13:25we will find
13:27out
13:27where
13:29find
13:29out
13:30from
13:31right
13:31because
13:32we know
13:33what is
13:35leftmost
13:36node
13:36in the
13:37right
13:37subtree
13:38root
13:39right
13:41now
13:42replace
13:43value
13:44we will
13:45create
13:47in order
13:48successor
13:49and
13:49delete
13:51call
13:53root
13:54right
13:55in order
13:57successor
13:58in order
13:59so
13:59now
14:00root
14:00in order
14:01successor
14:02delete
14:02root
14:04right
14:04subtree
14:05update
14:07and
14:08finally
14:09return
14:09root
14:11so
14:12what
14:12in order
14:13successor
14:13replace
14:14and
14:18in order
14:19successor
14:20node
14:20delete
14:21and
14:22I am
14:23saying
14:23in order
14:24successor
14:24because
14:25this term is
14:25very important
14:28now
14:29utility
14:30function
14:31find
14:32in order
14:33successor
14:34public
14:35static
14:36node
14:36root
14:38in order
14:38successor
14:40node
14:41root
14:45root
14:45how to find out
14:46simply
14:46loop
14:47run
14:47until
14:49root
14:50is not
14:51equal to
14:52null
14:52root
14:54is not
14:54null
14:55until
14:55root
14:56is equal to
14:57root
14:57root
14:58root
14:59is not
14:59root
15:01and
15:03return
15:04root
15:05here
15:06if
15:09root
15:09is null
15:09then
15:10how to handle
15:11root
15:12is null
15:13so
15:13root
15:13is null
15:15root
15:16is null
15:18root
15:18is null
15:19root
15:20is null
15:20that case
15:20we have already handled
15:21here
15:22so
15:22find in order
15:24successor
15:24in this case
15:25root
15:26is not null
15:28so
15:29that case
15:30is not
15:31to handle
15:31and
15:32round of applause
15:34for students
15:35who have questions
15:36we are actually thinking
15:37about concepts
15:39so
15:41this function
15:42complete
15:42for our
15:43delete
15:43we have also written
15:45in order
15:45successor
15:45utility
15:46function
15:46now
15:48we have
15:48in order
15:49sequence
15:50now
15:53delete
15:54node
15:55first
15:56we are going to delete
15:58and
16:02in order
16:03of
16:03root
16:071
16:08is
16:081
16:08our leaf node
16:09so
16:091
16:10is deleted
16:11here
16:11but
16:12if I want to delete
16:13for example
16:14I have to delete
16:1510
16:16so
16:17in this case
16:1810 will also
16:20delete
16:20in order
16:21sequence
16:21and
16:22now
16:22I have to delete
16:23if
16:245
16:25in this case
16:285 will also
16:31delete
16:31in order
16:32success
16:33and
16:336
16:35so
16:36deletion
16:38BST
16:38every single node
16:40can
16:40go
16:40so