- 2 days ago
Transcript
00:00Next question which we will talk about is to mirror a binary search tree
00:04I have given a BST and on BST side I have a mirror given
00:10Now I have a binary search tree in this mirror, or in the eye, I have to convert its form
00:16to make it that way
00:18If I have a mirror here, then in binary search tree, the root node is the same
00:25So the root node is the same, but the original BST is the left subtree, it will become the right
00:33subtree
00:34And the original BST is the right subtree and the original BST is the right subtree
00:40And this is not only for root node, it will be for every single node
00:45If I have a 5 example, 5 is now on my root side
00:50And also 5 is the left child 3, but now 5 is the right child 3
00:55Before 6 is the right child, now 6 is the left child
00:59So the same thing which is from the mirror from the left side
01:04The reflection will also be the right subtree
01:07And the mirror left subtree will also appear from the left subtree
01:20Since I will be making changes in which tree I will change the same tree
01:23I will change the same tree in the same changes
01:24So this is not a new tree or new root to make. I will change the same root to make
01:30changes
01:31At every single node
01:32So we will talk about how how to create the mirror of a BST
01:35How do we do in a mirror?
01:37So assume that you have a given node
01:40In mind that my own known node is this given node
01:50Now if I need mirror image, what will I do?
01:56I always know that if there is binary search, then I will be the same in my root node.
02:01If the mirror will be the same in the background, then the reflection of the mirror will be the same
02:07in the background.
02:08So what will I do?
02:09The root node will be the same in the background.
02:11But in the mirror image, the left subtree was the same in the background.
02:20The right child will be the same in the background.
02:26And the right child will be the same in the background.
02:30In the left node, I will create a node in the background.
02:35So BST's conditions will be reversed.
02:38The final tree will not be the same in the background.
02:42But yes, this is a mirror image.
02:44So if I have this node, I will create a single left child, single right child.
02:50What will I do?
02:52I will create my left child, I will create my right child.
02:54And I will create my right child.
02:56I will create my left child.
02:58Now let's assume one more case.
03:00Let's assume that here, 5 is the root root.
03:03So this is the right side.
03:04So the left side, the right side is the same in the top.
03:06There's so many more candied of the structure,
03:07So this is the same in the left hand.
03:08And the right side has a smaller structure.
03:12I will create this key.
03:18So I will create the right side to a smaller T.
03:26It will create a larger structure.
03:33Subtree था और यह मेरा पहले Left Subtree था तो यहाँ पर मेरा Right Subtree आ जाएगा Right Subtree यानि
03:38मेरी
03:39Notes कुछ ऐसी देखेंगी और Left Subtree यानि मेरी Notes अब कुछ ऐसी देखेंगी
03:45तो यहाँ पर Right Subtree जो पहले Right Side में था जो मैं Mirror Image जिसका
03:50Calculate कर चुकी थी वो पूरा का पूरा अब Left Side में आ जाएगा और जो Left Subtree था
03:55जिसका Mirror Image Calculate उस रुका था वो पूरा का पूरा उठकर मेरे Right Side में आ जाएगा
04:00इसको कहने का मतलब है अगर मैं किसी भी Root पर हूँ या किसी भी Node पर हूँ
04:05वहां पर Mirror Image क्रियेट करने के लिए मुझे क्या करना पड़ता है
04:08Simply जो मेरा पूरा का पूरा Left Subtree था पहले उसके लिए Mirror Calculate कर लो
04:12और पूरा का पूरा उठकर Right Side में रख दो
04:35चोटे वाले हिससे कर सकते हैं तो बड़े वाले हिससे Automatically कर पाएंगे
04:38और हम चोटे हिससे के लिए Code लिखने वाले हैं
04:40यानि अगर मैं किसी Node पर खड़ी हूँ और मेरे Left Subtree और Right Subtree ने Mirror Calculate कर दिया
04:45तो फिर मुझे अपना Mirror के से Calculate करना है
04:47Simply by swapping और by exchanging the values of my Left Subtree and my Right Subtree
04:54इसके लिए एक बार हल का साम sudo code लिखकर देखते हैं
04:58मान लेते हैं मैंने एक function बनाया Mirror नाम से जिसमें मैंने अपनी Root को Pass किया
05:03अब मुझे सबसे पहले क्या करना है सबसे पहले मुझे Mirror के लिए Call करना है
05:09Root.Left के साथ
05:11यानि मैं अपने Left Subtree का Mirror Calculate करती हूँ
05:13फिर मैं क्या करूँगी Mirror को Call कर दूँगी अपने Root.Right के लिए
05:18यानि फिर मैंने Right Subtree का Mirror Calculate कर दिया
05:21अब एक बार जब मेरे पास दोनों का Mirror Calculate हो गया
05:24तो क्या कह सकती हूँ कि इनकी Nodes भी मेरे पास आ जाएंगी
05:27अगर मैं Return Type इसका Node रखूँ तो मैं Nodes को Return करवा सकती हूँ
05:31यहाँ पर आ जाएगा अब मेरा नया Left Subtree
05:34यहाँ पर आ जाएगा अब मेरा नया Right Subtree
05:36अब मुझे क्या करना है
05:38अपने root के Left को
05:41अब अगर मैं Point करवा दू अपने Right Subtree की तरफ
05:44और अपने Root के Right को
05:47अब मैं Point करवा दू अपने Left Subtree की तरफ
05:50इन दोनों Lines को लिखने का मतलब है
05:52कि एक बार मैंने अपने Left और अपने Right को Mirror करा गया
05:55अब मेरे Tree का Left पॉइंट करेगा Right की तरफ
05:58or tree का right point करेगा left की तरफ.
06:00Example एक देखते हैं.
06:02मान लेते हैं, मेरे पास एक node है 5.
06:045 के नीचे एक और subtree है.
06:07यहाँ पर आगया है 2,
06:08यहाँ पर आगया है 3,
06:09और यहाँ पर एक और subtree है,
06:12जिसमें आगया है मेरे पास 8.
06:14सबसे बहुता काम जो मुझे करना है,
06:15मुझे अपने left subtree का mirror calculate करना है.
06:18Left subtree का mirror calculate करना है,
06:20यानि इस part का मुझे mirror calculate करना है.
06:23इस part का mirror assume करते है,
06:25मेरा recursive function मुझे अपने आप calculate करके दे देगा.
06:28इस part का mirror कुछ ऐसा देखेगा,
06:30जिसमें 2 उपर होगा, 3 नीचे होगा.
06:32क्योंकि यहाँ पर अगर मैं mirror रग दू,
06:34तो मेरा 2 यहाँ पर आएगा,
06:36और 3 आएगा mirror के पास में.
06:38तो कुछ ऐसा मेरे पास calculation होकर आजाएगा.
06:40Right subtree का अगर मुझे mirror calculate करना है,
06:43यानि इस part का.
06:44तो single node का mirror तो क्या होता है?
06:45Single node ही होती है.
06:47तो यहाँ पर मैंने calculate कर लिया अपने
06:50right subtree का mirror.
06:51तो मैंने अपने left subtree का भी mirror calculate कर लिया,
06:53right subtree का भी mirror calculate कर लिया.
06:55अब मैं root के left को point करवा दू,
06:58अपने right subtree की तरफ.
06:59यानि अब जो मेरा 5 है,
07:01यह directly point करेगा 8 की तरफ.
07:04क्योंकि 8 मेरा right subtree है,
07:055 का left 8 बन जाएगा.
07:07यानि यहाँ से मेरा पुराना tree हटेगा,
07:11और मैं अपने 8 को बना दूँगी अपना left subtree.
07:15फिर क्या करूँगी?
07:16फिर मैं root के right में fit कर दूँगी अपने left subtree को.
07:20यानि यह मेरा left subtree था,
07:21जिसका mirror form हो चुका है,
07:22अब मैं इसको मैं लगा दूँगी root के right subtree में.
07:24तो यहाँ से root का right हट जाएगा,
07:26और यहाँ पर directly आजाएगा,
07:29मेरे पास मेरा left subtree.
07:31तो मेरा जो final tree बनेगा,
07:33वो कुछ ऐसा देखेगा,
07:35जिसमें क्या हुआ recursively?
07:36मैंने assume किया, fade किया,
07:38कि मेरा left subtree और right subtree का जैसी mirror हो जाता है,
07:41तो मेरी ऊपर वाली parent node का में,
07:42अपने आप mirror calculate कर सकती हूँ.
07:44कैसे?
07:45अपने root के left को मैं right subtree से join कर दूँगी,
07:48और root के right को मैं left subtree से join कर दूँगी,
07:50यानि values को swap कर दूँगी.
07:51और क्योंकि return करनी होती है node,
07:53तो यहां से return कर दूँगी,
07:54फिर finally अपनी root को.
07:56Base case यह हो जाएगा कि कभी भी,
07:58अगर मेरे पास null node आजाती है,
08:00तो null node का,
08:01तो कोई reverse होता नहीं,
08:02तो यहां से simply हम return कर देंगे अपनी null node को,
08:06यह मेरा base case हो सकता है.
08:07अब इसे एक बार dry run करके देखते हैं,
08:09अपने इस BST पर.
08:10सबसे पहले आएंगे हम 8 के पास,
08:12यानि अपनी root के पास.
08:13Root क्या चाहेगी कि सबसे पहले हमने
08:15left sub 3 का mirror calculate कर ले.
08:17तो उसके बाद आएंगे हम 5 के पास,
08:195 भी चाहेगा सबसे पहले left sub 3 का mirror calculate कर ले.
08:22पर आएंगे 3 के पास,
08:233 should be, first of all, calculate the left subtree of the mirror. Now when we go to the left
08:27subtree, we will get a null. What does it do?
08:30It has no reverse, no mirror, no height, nothing about null. What will null return?
08:35Null will return null. Null has null returned here and here it has null returned. Now, 3 is left subtree
08:41and right subtree.
08:43If 3 is null to null, then there will be null in both values and 3 will be equal to
08:47my leaf.
08:48So, leaf nodes will be the same. Now, leaf nodes will be the same.
08:53Now, leaf nodes will be the same. Directly, 5 will return its value.
08:57And 6 will be the same. 6 will return its value. Now, 5 will be the same.
09:035 will have left subtree and right subtree will be the same.
09:09So, 5 will exchange values. So, if I have my 5 node, then 5 will be the same.
09:19In fact, this is why i will return at the left subtree, here i will return to the left subtree.
09:27Now, the value of 5 will return, its value of 5, for now, on the left subtree …
09:30… The value of value of 5 will return with its value into the 8.
09:32These will return to 5 node.
09:34This means this that i will return to the entire node, 4x row of 5.
09:46Now, 8 will calculate the right sub-tree to the value.
09:508 will go before 10, 10 will see left sub-tree to null.
09:54So, there is no mirror here.
09:55Now, he will ask the right sub-tree to the 11.
09:58The 11 will return the left node to the left node to 10.
10:0410 will return the right sub-tree to the left sub-tree to the left and right sub-tree.
10:16That will be null value. Left from the left, with the left 11, 10 will return to the right of
10:2011.
10:24Now, with 8, 10 will send the entire root node to the right node.
10:2910 will return the total sub-tree to the right sub-tree.
10:32We have the left sub-tree, right sub-tree.
10:35So, this is the right sub-tree.
10:37In the right sub-tree as the left button will return to the right,
10:40join will be here, if my 8 node is here, then left from this sub-tree
10:47we will join in 8-right, then we will join in some way, and the right from this sub-tree
10:56we will join in 8-left, so we will join in 8-left, then we will join in 8-left,
11:07then we will join in 8-left
11:09so this is the initial-tree mirror image, we can also see here, if I use a line for mirror
11:17so we can simply see that this node is the first layer of mirror, this is the second layer
11:24this is the second layer, this is the second layer, this is the second layer, this is the second layer
11:28how did it go, first we have started to convert the small-tree, and if we have left-tree and
11:35right-tree
11:36convert, then automatically we will convert our complete root, so recursively we have
11:42this problem tackle, and since we will go to each node, then the time complexity of this problem
11:48will be big of n. So we have a class, a classroom name, we have a static node type class
11:55which is data, left child, right child and a constructor, with this main function
12:02we have already defined a BST, how do we define this BST?
12:06root is 8, left child is 5, right child is 10, and 3, 6, 11, so this is the same
12:12BST
12:13which we have dry run code, how to create BST? BST directly assign our root node to left child and
12:19right child
12:20our mirror BST should be something like this, so first we have a function that will create a mirror BST
12:28this function will name public static, this return type is called node, because nodes
12:33which are changed after mirror, it will be returned, we will name it create mirror
12:40create mirror, within the single parameter, which is my root node, now we will know what to do
12:47so first we have a base case, that if my root is equal to null, if my root is equal
12:50to null
12:51so if my root is equal to null, then I don't have a mirror, so in this case
12:53what do I do? directly return a null value
12:56no, first of all my work will be to create my left sub-tree and create my right sub-tree
13:02and create my right sub-tree to create mirror
13:03so I will call this, create mirror for my root left
13:15in fact, in fact, in left sub-tree, I can write left sub-tree
13:19so I will call this right sub-tree
13:26or I will call this right sub-tree
13:28in fact, here in left sub-tree, I can write left mirror
13:33i can write mirror image, or left sub-tree
13:36or right sub-tree mirror image
13:39then what I do after, what do I do?
13:40the same as I have both mirror images
13:42I will swap the nodes, left and right nodes
13:44I will call this one
13:45or whatever my root node was
13:47now its left will become my right mirror
13:51and root
13:52its right will become my left mirror
13:55and what will last mean
13:56I will return my root as my final answer
14:00so this is my create mirror
14:01which is the complete function
14:02in this function
14:03we go to every single node
14:05so the time complexity of recursive leaks
14:07will be big of n
14:11now I will create a pre-order function
14:13pre-order
14:15why is it?
14:15because I will create mirror image
14:16in my tree
14:17so the final answer
14:19will get the root
14:20what will I do?
14:22I will print every node
14:23so I will print the pre-order sequence
14:25so I will print the pre-order sequence
14:26and tally
14:26that the answer has come
14:28that the answer is
14:28the right answer
14:29so I will create a function
14:31public
14:31static
14:33void
14:33pre-order
14:35pre-order
14:36parameter
14:36jayega
14:37node root
14:39pre-order
14:41pre-order
14:41we will write now
14:42that the root
14:43null
14:44we will simply
14:45return
14:46no
14:47pre-order
14:48print
14:48we will print
14:50root
14:51data
14:52we will call
14:53pre-order
14:54for root
14:55left
14:56and then
14:57pre-order
14:57for root
14:59right
14:59this is my pre-order
15:02main function
15:03with this mirror
15:05vst
15:05should be
15:05so I will call
15:07create mirror
15:08for my root
15:10and here
15:11the root
15:12which
15:12create mirror
15:13has returned
15:15to me
15:15store
15:16after I will call
15:18pre-order
15:19for my root
15:21save
15:22and now
15:22run
15:27so I have the pre-order sequence
15:29printed
15:29first
15:30root
15:31is
15:318
15:31then we will print
15:3310
15:33then we will print
15:3410
15:35then we will print
15:3611
15:38then we will print
15:395
15:40then we will print
15:416
15:42then we will print
15:44rightmost node
15:453
15:47so in this way
15:48pre-order sequence
15:49is right
15:49and we have
15:50created a mirror
15:50for this particular
15:52vst
15:52so in this way
15:53we can create a mirror
15:55image in linear
15:57time complexity
15:58time complexity
15:58remember
15:58x
15:58second
15:58ミー
15:58to the