• 2 days ago
Transcript
00:00What I did, I created my class in the name of Classroom, which has started a class called node
00:04In the node, all the nodes have data stored, plus a constructor
00:07In the main function, I have a binary tree already given
00:11This given binary tree, I have already written code
00:15Simply, I have 30 root of the left and right of the root of the right, so I have assigned
00:20data
00:20My final expected BST is this
00:23I need to print the size of 5
00:27Now, I am going to print this size for this code
00:30First, I will create a static class with information
00:33In the name of the information, I will remove all the nodes
00:36I will remove all the nodes
00:38First, my boolean information is BST
00:41After that, I will take my size
00:45After that, I will take int minimum
00:48and int maximum
00:51I will create a constructor
00:54Structor, public info
00:56Jiske ander, myre paas
00:58Pahle to is BST
00:59Aayega, fir meri paas aajaeega int size
01:02Int min
01:04and int max
01:07This dot is BST is equal to is BST
01:10This dot size is equal to size
01:14This dot min is equal to minimum value
01:19And this dot max is equal to my maximum value
01:22In this way, I will return my information class
01:24And I will return my function to this class
01:29I will name the public static
01:31It will return information
01:32Because every single node
01:34will return information
01:35It will name the largest BST
01:38Largest BST
01:40Largest BST
01:40I will pass my root node
01:41Which will be the binary tree
01:43It will be the root node
01:44With a static variable
01:47Public static int
01:49I will name it
01:50BST size
01:51Max BST
01:53In the max BST
01:56Initialize
01:57With 0
01:58No binary tree
02:01Exist
02:02Largest BST code
02:03What do I have to do
02:05First of all
02:05First of all
02:06I will take the information from left and right
02:08First of all
02:11I will call the largest BST
02:13For my root
02:14Left
02:15Which information
02:16Return
02:16I will store
02:18In my left information
02:19Then I will store
02:21Right information
02:23Largest BST
02:24Call
02:25Root
02:25Right information
02:26Now I will make my own information
02:28For my information
02:29I will have 4 values
02:31For my information
02:32The first value
02:32Is BST
02:33Now I will do
02:34Left information
02:35Right information
02:36Now I will make my own information
02:38For my information
02:39For my information
02:39First of all
02:40I will have size
02:41So size
02:42To calculate
02:43Int size
02:44Is equal
02:45To
02:46Left information
02:47Size
02:48Plus
02:49Right information
02:50Size
02:51Plus
02:521
02:53Similar
03:22Similar
03:46For my information
03:47For my information
03:47For my information
03:53For my information
03:57For my information
04:01For my information
04:17So
04:19In
04:19False
04:20Return
04:20In
04:21In
04:21In
04:21In
04:21In
04:22In
04:22BST की Conditions है, तो यहां से मैं Return कर दूँगी New Information, जिसके अंदर इस BST में
04:28Pass कर दूँगी, मैं False, Size हो जाएगा मेरे Size के बराबर, Minimum में Send कर दूँगी Minimum, Maximum में
04:34Maximum, तो यह मेरी False वाली Condition आ जाएगी, उसके बाद Check कर लूँगी कि क्या मेरा Left Subtree,
04:40अगर मेरे Root के लिए यह वाली Condition true हो जाती है, तो उस Case में, मेरा
04:44Root Node है, वो तो Valid BST बना रहे हैं, पर क्या मेरी Left Subtree and Right Subtree
04:48Valid BST बनाते हैं, तो उसके लिए मैं Check कर लूँगी कि अगर Left Information का
04:55Is BST and Right Information का Is BST, मतलब मेरा Left Subtree भी Valid BST है,
05:04And Right Subtree भी Valid BST है, और मैं भी Valid BST हूँ, तो उस Case में मुझे क्या करना
05:08है,
05:09New Information में Return करना है True, उसके साथ साथ Return करूँगी मैं Size,
05:13अपना Minimum and अपना Maximum, अब यह जो True Return कर किया है मैंने, इससे पहले मुझे एक चीज़ करना
05:19है,
05:19क्या करना है, अपने Maximum BST वाले Size को मुझे Update करना है, तो यहां मैं Max BST को Compare
05:26कर लूँगी,
05:27Math.Max of Max BST and My Size, तो यह जो Largest BST का Size होगा, वो Compare हो जाएगा
05:37मेरे Size के साथ,
05:38क्योंकि मैं तो खुद एक BST हुई, तो मेरा Size भी Compare हो जाएगा इस Maximum BST के साथ, कि
05:44Already जो Existing Maximum BST की Value है,
05:46क्या वो मेरी Value से बड़ी है या चोटी है, जो भी Maximum होगी वो इस Max BST के अंदर
05:50Store होकर आ जाएगी,
05:51तो यह हो गई मेरी True की Condition, अगर ऐसा नहीं होता है तो Last में हम Return कर देंगे
05:57New Information जिस में Pass कर दूँगी मैं False,
06:01यानि मेरा जो Node है, मेरा जो Root Node है वो एक Valid BST नहीं है, उस Case में मैंने
06:05Pass कर दिया False,
06:06Size हो जाएगा मेरे Size के बराबर, Minimum में Minimum, Maximum में Maximum,
06:11यह हो गया मेरा Code to Calculate My Information for All Nodes और Last में Base Case ही हो जाएगा
06:17कि अगर Root की Value का भी Null के Equal हो जाती है,
06:20तो उस Case में मैं Return कर दूँगी New Information जिस में Is BST हो जाएगा True, Size हो जाएगा
06:270 के बराबर,
06:28मेरे Minimum की Value हो जाएगी Plus Infinity यानि Integer.Max Value और Maximum की Value हो जाएगी Minus Infinity
06:37यानि Integer.Min Value तो यहां मैंने Base Case को Handle कर लिया,
06:43इसको मैं हलका सा Summarize कर लेती हूं, Largest Binary Search Tree का मुझे Size Calculate करना था अपने Binary
06:49Tree के अंदर,
06:50उस Tree की मुझे Root Node गिवन है, मैंने क्या कि एक Static Variable बना लिया Max BST, जो मेरे
06:55Largest Size को Track करेगा,
06:57उसके साथ साथ मैं हर Node के लिए उसकी Information को Pass करवाती हूं,
07:01तो मैंने पहले Left की Information निकाल ली Recursively Right की Information निकाल ली,
07:04फिर मैं क्या करूँगी, जैसे ही मेरे पास Left & Right की Information आ जाती है,
07:09Size क्या होगा? Size होगा Left का Size Plus Right का Size Plus One,
07:12Minimum क्या होगा? Left का Minimum, Right का Minimum,
07:15and मेरी Value, तीनों का Minimum,
07:17Maximum क्या होगा? Left का Maximum, Right का Maximum,
07:20and मेरी Value, तीनों का Maximum.
07:22फिर मैं सबसे पहले चेक कर लूँगी,
07:23क्या Root का Data Less Than Left का Maximum है,
07:27या Root का Data Greater Than Right का Maximum है,
07:29यह Invalid की Conditions होती है,
07:31जैसे ही Invalid होगा, False Return कर दो,
07:33with Size Minimum and Maximum.
07:36नहीं तो चेक कर लो क्या Left Subtree and Right Subtree भी BST थे?
07:39अगर वो दोनों भी BST थे,
07:40फिर मेरा जो Root Node है वो एक Valid BST है,
07:43उस Case में मुझे True को Return करना है,
07:45और Return करने से पहले मैं अपने Size को Compare कर दूँ
07:47Maximum BST के Size के साथ,
07:49और Last में मतलब यह Condition False होगी,
07:51तो Last में मैं Return कर दूँ False,
07:53with my Size Minimum and Maximum.
07:55तो यह मेरा Function होने वाला है,
07:57Main Function के अंदर क्या करूँगी,
07:59इसके लिए Call लगाऊँगी,
08:01Information,
08:02Info is equal to,
08:04Largest BST के अंदर पास कर दूँगी अपनी Root,
08:07और मुझे Print करवाना है,
08:09Largest BST के Size को,
08:11तो Largest BST,
08:13Size is equal to,
08:15यह हो जाएगा मेरा Max BST के बराबर,
08:17यानि यह वाला Static Variable,
08:19जिसको मैंने Create किया था,
08:20इसकी Value को Last में में Print करवा दूँगी,
08:23इसको कर लेते हैं,
08:24Save & Now Run,
08:25यह मेरा Largest BST था,
08:27इसका Size 5 आना चाहिए था,
08:28बट 1 आ रहा है,
08:30एक बार Check करते हैं,
08:32Code के अंदर छोटी सी चीज
08:34Miss Out करी है हमने,
08:35वह यह है कि यहां पर,
08:37मैंने True को कभी Return ही नहीं किया,
08:39यहां बस मैंने नई Object को Create कर लिया,
08:41तो यहां मैं Return कर दूँगी,
08:43अपनी इस नई Object को,
08:45इसको कर लेते हैं, Save & Run Again,
08:48तो इस बार मेरे
08:49Expected Binary Search Tree का
08:51Largest BST का Size आया,
08:52That is equal to 5,
08:54यानि यह मेरा जो Given Binary Tree था,
08:56इसके अंदर जो Largest BST exist करता है,
08:58उसका Root बन जाएगा यह 60,
09:00और वो BST कुछ ऐसा दिखेगा,
09:02तो इस तरीके से हम Largest Binary Search Tree,
09:05इना Normal Binary Tree को Calculate कर सकते हैं,
09:07उसका Size भी Print करवा सकते हैं,
09:09और साथ के साथ,
09:10जैसे हमने इस Maximum BST का,
09:11Largest BST का Size,
09:12इस Static Integer Variable में रखा,
09:15वैसे हम एक Static Node ले सकते हैं,
09:17और वहां पर इस Root को Store कर सकते हैं,
09:19तो हम अपने पूरे के पूरे BST के
09:21Nodes को भी Print करवा सकते हैं,
09:23तो हो सकता है आपसे Largest BST का Size ना मंगे,
09:25आपसे Largest BST के Nodes का पूछें,
09:28या Largest BST का Root पूछें,
09:29तो उस Case में हम इस Node को Save करा लेंगे,
09:32और हम Print करवा देंगे 60 को,