- 2 days ago
Transcript
00:00Next question, which we will talk about is Sorted Array to Balanced Binary Search Tree
00:05In the question, we will be given a sorted array which is sorted in ascending order
00:12What do I have to create a binary search tree in this ascending order?
00:17So, I have sorted array 3, 6, 5, 8, 10, 11, 12, then my binary search tree will look like
00:24this
00:24And I will try to find the height of binary search tree, that is the minimum possible height
00:29So, I have created this binary search tree
00:32Here, Sorted Array will be 5 and here will be 6
00:36So, this is my sorted array from which I have created BST
00:39Now, the BST we are going to create minimum possible height
00:44Minimum possible height means that one possible BST is also possible
00:48So, I will take my first node as root, then I will create 5 to right child
00:53Then I will create 6 to right child, then I will create 6 to right child
00:57Then I will create 8 to 6 to right child, then I will create 10 to right child
01:00Then I will create 10 to right child
01:01And this way, my nodes are 11, 12, here will expand
01:07So, the BST has the maximum possible height
01:11So, this is the maximum height of BST
01:14I will create the maximum length of BST
01:15So, I will create a balanced BST
01:17And this with balance BST is the maximum possible height
01:20For this BST has the minimum possible height
01:21So, this is the maximum height of BST
01:24This is the maximum height of BST
01:24I will create other large nodes in which we have not created a chain
01:28Like this particular array
01:30I will create many multiple BSTs
01:33Where one of these has the same.
01:34But I will create it
01:37I will calculate the height in which I have balanced height, like here I have 8 in the middle
01:42and then I have 5 in the left child, left child 3 and right child 6.
01:48So, this is the minimum possible height.
01:51This is the minimum possible height.
01:51It is not possible to form 3 height from this area.
01:57Therefore, I have a minimum height, or balanced BST, and I have a balanced BST
02:03using any sorted array.
02:05So, what do we need to use this technique?
02:07When we talk about binary search tree,
02:10I have told you that the most important property,
02:14which always needs to be,
02:15is that BST's in order sequence is always a sorted sequence.
02:26If I calculate it in order,
02:29it will be a sorted sequence.
02:31The same thing, the same property, we will observe here,
02:35that the array is a sorted array.
02:39So, what can I say?
02:40This sorted array means that
02:42if the binary search tree is in order sequence,
02:44it will be the same array.
02:47And I will create that binary search tree.
02:49Now, in order sequence,
02:51if I analyze the in order sequence,
02:53what is in order sequence?
02:54In order sequence,
02:55first of all,
02:56the left child nodes,
02:58or left subtree nodes,
03:01then the current node,
03:03then the root node,
03:06then the right subtree node.
03:09Now, when I need minimum possible height,
03:12I want to say that the current root is the two sides,
03:16the rest of the nodes are equally distributed.
03:19That means,
03:20the other nodes are left subtree,
03:22and the other nodes are right subtree,
03:24then the BST will be balanced.
03:26So, I will find some sort of work,
03:29that my sorted array,
03:31which I have assumed that
03:32it is the in order sequence,
03:34I will create the middle node node.
03:35I will create the root node.
03:38And in middle node,
03:39the left node,
03:40the elements will be left subtree,
03:43and the right node,
03:44the right subtree will be left subtree.
03:46So, the approach is what I have to follow.
03:48I know that in order sequence,
03:50the property is followed.
03:51So, I will remove the sorted array
03:53for the middle,
03:56So, the middle suited to which is theowned upon node.
03:57To make the root node node,
03:57I will start the root node.
03:59For the root node,
04:00this is my starting point,
04:02which is equal to zero.
04:04This is my ending point,
04:05which is equal to zero,
04:07one, two, three, four, five, and six.
04:09Then the next value is to be 6.
04:11And I always know,
04:12what is mid and mid?
04:13My mid is always equal to
04:15start plus end, divided by 2.
04:18What did you just find 6 plus 0 by 2,
04:20which is equal to 3?
04:22So, this also means,
04:220 1 2 3
04:23this value will become my mid node
04:26this is my mid
04:28I will do that
04:29as I got a mid node
04:30I know what will become mid node
04:32middle node is my current node
04:34my left node is my left sub tree node
04:36right node is my right sub tree node
04:38so I will make my mid node
04:40I will create my current node
04:42so I have created 8 nodes
04:45now I will create my left sub tree
04:48I will know that
04:49left subtree के अंदर जितनी भी notes आएंगी वो सारी की सारी root के left में lie करेंगी
04:54तो 8 से left तक जितनी भी notes है वो सारी की सारी मेरे left subtree का part बनेगी
04:59यानि अब मेरे पास आ जाएगा 3, 5, 6 यहां पर start हो जाएगा 0 के equal
05:06और जो मेरा end होगा that will be equal to mid minus 1 यानि end बन जाएगा मेरे पास 2
05:11के बराबर
05:11तो इतनी notes को मैं अपने left subtree में जोडने की कोशिश करूँगी
05:15अब इन notes को अगर मुझे left subtree में जोडना है तो इनके लिए भी recursively में क्या find out
05:20कर लूँ
05:20इसका भी mid find out कर लूँ क्योंकि इसका जो भी mid बनेगा वो मेरे root का left child बनने
05:26वाला है
05:26तो इसका mid क्या होगा start plus end by 2 यानि 1 के equal तो ये value बन जाएगी मेरा
05:33mid
05:33जैसी 5 mid बना मैंने क्या किया 5 का node create कर लिया 5 का node मैंने create कर लिया
05:39अब मुझे 5 के left subtree को बनाना है
05:415 के left subtree को बनाने के लिए मैं 5 के left में जितने भी nodes रह गए उनको create
05:46करूँगी
05:46यानि 5 के left में मेरे पास 3 बचाए तो मैं call लगा दूँगी सिफ 3 के लिए
05:51अब मेरा start भी 0 है मेरा end भी 0 है तो मेरा mid जो होगा वो यही node बनेगा
05:55तो 3 आ जाएगा मेरे पास 5 के left में
05:59अब 3 के left में कोई भी nителя नहीं यानि 3 के left में क्या होगा द्री के left में
06:04nal आएगा
06:043 के right में भी no exposed 3 क्या बन फ्री के go क्या बन गया?
06:08अब 3 क्या बन गया? 3 बन गया 5 का left child
06:11तो 3 को connect करदोंगी मैं 5 के साथ
06:13अब 5 का left वाले सारे nodes connect हो चुके हैं अब
06:17I want to create all the right nodes in the right nodes.
06:19I put 6 on the right nodes in 5.
06:22I will make a node for 6 and that will be in the right sub-tree.
06:26Then, 6 is neither left sub-tree nor right sub-tree
06:29because there are no other numbers.
06:31So, 6 will be connected to the left and right child.
06:35Now, 6 will be connected with 5.
06:37So, we will connect with 6 with 5.
06:39So, here I have seen that 5 has been created. 5 left has been created.
06:435 right has been created. This whole sub-tree is ready.
06:47So, this whole sub-tree was my left root child.
06:50So, I have to connect this whole node to my root node.
06:54I will make it and create my root child.
06:57So, this whole connection will form here.
07:00So, I have created 8 and created 8 whole left sub-tree.
07:05Now, I will create 8 whole right sub-tree.
07:07So, I will create 8 whole right sub-tree.
07:08For the right sub-tree, I will be able to create these 3 nodes.
07:14These 3 nodes will be 10, 11 and 12.
07:18Here, my start will be equal to mid plus 1.
07:23So, if the value of mid was 3, then start will be 4.
07:26And where will it go?
07:27My end is equal to 6.
07:29Now, I will add mid to this value.
07:31So, I will create 11.
07:32What will I do?
07:33I will create mid node.
07:3511.
07:36Now, I will add call to this left sub-tree.
07:38For the left sub-tree, it will be only 10.
07:40I will add 10.
07:41Here, start will be 10 and end will be 10.
07:45So, I will create my value of 10.
07:47Now, 10 is left child or right child.
07:50So, 10 will go to 11 and connect.
07:5310 and 11 will connect.
07:54Then, 10 will become 11 left child.
07:57Now, I will create 11 to the right child.
07:59We will create 12 to the right child.
08:02We will create 12.
08:02The node will create 12.
08:03Now, I will connect 12 with 11.
08:06So, I will connect 12 with my right child.
08:09So, I will create my right sub-tree.
08:13Now, I will connect my middle node with root node.
08:17So, I will create 11 with 8.
08:19So, I will create 11 with 8.
08:20So, I will create the right child with 8.
08:22So, I will create a whole BST.
08:25And how is BST?
08:26It is a balanced BST.
08:28The balanced BST will always come.
08:30How did I ensure that?
08:31I have ensure that I always make middle node.
08:35I will create a root node.
08:36So, I will create the rest of the other elements.
08:37I will create equally divided.
08:39Equal number of elements will go left sub-tree.
08:41And equal number of elements will go right sub-tree.
08:44So, I will know that it will be balanced.
08:46What could I do?
08:47If I can do 3, if I create a root node.
08:50If I create a root node,
08:50then I will create a right sub-tree.
08:52And left sub-tree is empty.
08:54Unbalanced tree is the one that
08:56in which one side,
08:57is left sub-tree or right sub-tree,
08:59there are more nodes.
09:00I will ensure that the two sides are equal number of nodes.
09:04So, I have made the middle node node.
09:06Then I have made the left sub-tree.
09:08Then I have made the left sub-tree.
09:08And the right side, I have made the right sub-tree.
09:11And I have only followed this process
09:11only for one root.
09:13I have recursively followed this process.
09:16I have recursively followed this process.
09:16For each node,
09:17I have recursively followed this process.
09:19And what is my BST,
09:20that is equal to my final answer.
09:23Or not my final BST.
09:24Now, how can we write the pseudocode?
09:26First, let's write the pseudocode.
09:28Then we will add the actual code.
09:29I have no function.
09:32I have no function,
09:32which is the name of create BST.
09:34This is the BST,
09:35I have no array given.
09:37I have an array given,
09:37an start given,
09:38an end given.
09:40I always know that,
09:41I have to calculate the mid.
09:45My mid is start plus end divided by 2.
09:57that will be equal to new node of my array
10:02mid index
10:05which value is 8
10:07then I will create a new node
10:10that will create
10:11recursively create
10:12left subtree
10:14in which I will create
10:16in this screen
10:17I will create
10:20my array
10:21point
10:22here
10:23from mid
10:28to mid
10:29and can
10:30create
10:31So, whatever value return, I will make my root left.
10:35Besides, I can call the create BST for my right subtree.
10:39Right subtree, when we call the right subtree, I have to consider the nodes here.
10:44So, my starting point is mid plus 1 and ending point is end.
10:49So, I pass my mid plus 1 and my end.
10:54So, whatever node return, I will make my root right node.
10:58So, recursively, what did I do?
11:00Recursively, I calculated my middle.
11:02Then, I calculated my middle number with my node.
11:05Now, I have recursively create BST.
11:07First, I have to start with mid minus 1 to my left subtree.
11:11Then, I have to end to my right subtree.
11:14Now, I can return my root to my root function.
11:18So, I can return my root directly.
11:21What will be the base case?
11:22What will be the base case?
11:23What will be the base case?
11:23The base case is low.
11:24In the base case, when I start my root is even more.
11:28In that case, I have no valid node.
11:33Not the middle node is valid or my middle node will be valid.
11:35If the start-it is greater than nth,
11:38So, the base case will return my root as null.
11:51code or my actual code will be shown in which this is my base case and this is my recursive
11:58level.
11:58We are going to perform what action and operations are going to perform.
12:01If you want to see that the time complexity of this code will be bigger of n.
12:06Because recursively I have created one node once again, so if I have created an array size
12:12of n nodes here and I have created n nodes for bst, so I have performed n operations every
12:17time complexity, that is why this is going to be a linear time complexity code.
12:23Now what are we going to do in the code?
12:27I have my classroom class, I have a static node class in which there are my bst nodes.
12:32I have already written a pre-order function for my pre-order.
12:36How do I use this?
12:39When my final tree will create bst, I will print it in the pre-order form.
12:44In my main function, I have a given array, which is sorted array given and my expected bst
12:50is something like this.
12:52So I will create a function, public static node create bst.
12:58This is my binary search tree.
13:00I will actually create my binary search tree.
13:01In this, I will pass my integer array.
13:04I will pass my start and end.
13:09Now I know that as I have started and end, I will calculate my mid point.
13:14So start plus end divided by 2.
13:17Then I have my mid.
13:18Then I will create a new node.
13:21I will take my root node.
13:23So root is equal to new node of array of mid.
13:27So the middle of the array will create a root node.
13:32Then I will create my root node.
13:35Then I will call me the whole left subtree.
13:36I will call again create bst.
13:39I will pass my array in starting point as start and ending point as mid-1.
13:45Because from mid-1 to my left subtree to my left subtree
13:57node को return कर देगा जिसको मैं store करवा दूंगी root के left में
14:00same चीज़ करेंगे हम root के right के लिए root का right is equal to create
14:05bst of array हम कहां से कहां तक जाएंगे mid plus 1 से लेकर
14:09मुझे end तक जाना है जैसे अगर 8 root बन गया तो अब 10 से
14:13लेकर 12 तक सारे के सारे जो nodes हैं वो मेरे right
14:15subtree में जाएंगे और last में मैं return कर दूंगे अपने root को
14:20base case के यहां पर यह होगा कि अगर कभी भी मेरे पास मेरा
14:24starting point becomes greater than my ending point तो यह invalid condition का
14:28case है इस case में मुझे किसी node को create नहीं करना मुझे simply
14:31यहां से return कर देना है null value को तो इसको कर लेते हैं safe अब मैं main
14:36function के अंदर node root बनाऊंगी जिसमें मैं call करूंगी अपने create
14:41BST को जिसके अंदर pass करूंगी अपना array starting point शुरुवात में
14:45होगा 0 के बराबर ending point होगा array.length-1 के बराबर क्योंकि मैं इसको start
14:52रखना चाहती हूं इसको मैं अपना end रखना जाती हूं इसको कर लेते हैं
14:55अब call लगा देंगे pre-order को for my root. अब करते हैं save and run
15:01तो मेरे पास pre-order sequence कुछ ऐसा निकल कर आया है
15:04अगर मेरा expected binary search tree ही था तो इसमे pre-order में
15:08सबसे पहले 8 print होता, फिर 5 print होता
15:10चुकि ही यह print हुआ है, फिर मेरे पास 3 print होता
15:13फिर मेरे पास 6 print होता, which is this
15:16फिर मेरे पास 11 print होता, which is this value
15:18फिर मेरे पास 10 print होता pre-order में, which is this
15:21and फिर 12 print होता, which is this value
15:23तो मेरे पास pre-order sequence निकल कर आया है, बिल्कुल सही sequence है
15:27and इसी तरहिके से, एक sorted array से हम create करते हैं
15:31एक balanced binary search tree को
15:33और यह सवाल individually सिर्फ यहां खतम नहीं होता
15:36यह सवाल आने वाले काफी सवालों के अंदर
15:38as a helper function हम इस पूरे concept को use कर रहे होंगे
15:41because it is really important to know
15:43कि अगर हमें sorted sequence दे दिया
15:45या मान लेते हमें in order दे दिया
15:47तो उससे हम अपने BST को कैसे create कर पाएंगे
15:49in a balanced form