- 2 days ago
Transcript
00:00Hi everyone, in this class we are going to study binary search trees.
00:03Now, we call binary search trees or BST.
00:06BST is an evolved version of binary trees.
00:09The search time complexity is very low.
00:12The name of binary search trees is for this reason,
00:15because in binary trees,
00:17we call big of n time,
00:19where n is the total number of nodes in the tree.
00:23While in binary search trees,
00:25we call big of h time,
00:26where h is the height of the tree.
00:29So, in the average cases,
00:30which are the height of the tree,
00:31they are connected to login,
00:33when the tree is completely balanced.
00:35In those cases,
00:36the search time complexity is very low.
00:40Therefore, we use BSTs extensively for fast lookup.
00:45Now, let's understand,
00:46in this class, we will understand what BST is coming,
00:48how to create it,
00:49how to delete it,
00:51how to search it within it,
00:53and then we will solve some questions.
00:55First, let's talk about what is BST.
00:56What is BST?
00:58What is BST inherently a binary tree?
01:00It means,
01:01in BSTs,
01:03there are all properties of binary trees.
01:04If we recall,
01:05what was the property of binary trees?
01:08The most major property was this,
01:10that at max,
01:11two children can be in a binary tree.
01:15meaning,
01:15every node can be maximum two children,
01:18one is our left child,
01:19and one is our right child.
01:20Another node can be like this,
01:22in which only left child is our right child,
01:24one is our null.
01:25One node can be like this,
01:26in which left child is null and right child is null.
01:28One node can be like this,
01:30in which both child are null.
01:31So,
01:32these are four cases,
01:33which are possible in which,
01:34this can be a whole sub-tree,
01:36and this right child is a sub-tree.
01:37So,
01:38these properties are called a BST.
01:41Now,
01:41with BSTs,
01:42there are some additional properties
01:44that we have come to this,
01:45which is the first thing,
01:47that all the left sub-tree nodes
01:49are less than in value as compared to roots value.
01:53We have taken an example of this binary search tree.
01:56In this binary search tree,
01:58if we talk about the left sub-tree,
02:00then,
02:00every single node,
02:02in comparison of root node,
02:04is less than value.
02:06Meaning,
02:06we compare with 4,
02:07compare with 2,
02:07compare with 1,
02:08compare with 3,
02:09compare with all values.
02:11So,
02:11this is the first property of a binary search tree.
02:14The second property is like this,
02:16in which the right sub-tree nodes
02:18are greater in value as compared to the root node.
02:20So,
02:21here,
02:21if we compare our root node,
02:24with all the right sub-tree,
02:27we have 5,
02:29we have 6,
02:30which are greater than value when we compare it to 4.
02:33So,
02:33these are both properties,
02:34plus an additional property comes,
02:37that our left and right sub-tree,
02:39they are also BSTs.
02:42So,
02:42this is not just the root node,
02:44that is the BST property.
02:47If we take a small tree in BST,
02:50it will be our BST property will be satisfied.
02:53In this tree,
02:54if we take a sub-tree,
02:56for example,
02:57if we take a sub-tree,
02:59then this sub-tree will form a BST.
03:03Because,
03:03if there is a root,
03:05in the left of the root node,
03:06all the nodes,
03:07the value is less than value,
03:08and in the right of the root node,
03:09all the nodes,
03:10the value is less than value.
03:12So,
03:12in BST,
03:13how small you take a sub-tree,
03:14all the BST properties will be satisfied.
03:18With this,
03:20there is another special thing,
03:21that in BST,
03:22there are no duplicates.
03:24Some books,
03:25some teachers,
03:26some articles,
03:26you will give you such a definition,
03:27that in BST,
03:29we have duplicates.
03:31But,
03:31we are studying our lecture,
03:32we will not understand the condition of duplicates.
03:36We will consider BST,
03:38by default,
03:39no duplicate values.
03:40That's why,
03:41when duplicates come,
03:43you have to keep some special cases,
03:45and here,
03:46we will study core BST fundamentals,
03:48which are not duplicates.
03:49But,
03:50if you have a question of duplicates,
03:52the interviewer,
03:53or examiner,
03:53will tell you,
03:54that in BST,
03:55there are duplicates,
03:56that in BST,
03:57we can modify it a little bit.
03:59Now,
04:00let's talk about,
04:00if you take a special case,
04:02and there will be duplicates,
04:03then,
04:04how do the values change in BST,
04:06think that in BST,
04:07that in BST,
04:08there will be more duplicates.
04:09So,
04:09it will be added here,
04:11after 3,
04:12in the right,
04:13or,
04:13for 4,
04:14to satisfy BST properties,
04:16it will be added here.
04:18But,
04:18if we consider BST without duplicates,
04:20then,
04:21the same value,
04:22will not get into BST.
04:24So,
04:25we have to keep these 3 properties,
04:28when we talk about BST.
04:30These 3,
04:31we have to automatically,
04:32we have to imagine,
04:33a sorted tree,
04:36in which,
04:36on the left,
04:38there are less values,
04:40and on the right,
04:41there are greater values.
04:44Now,
04:44with BST,
04:44associated,
04:46is a very special property,
04:47which is called,
04:48with the in order traversal.
04:49Now,
04:50we have studied 3,
04:504 types of traversals,
04:52which,
04:52we had first studied,
04:54our pre-order traversal.
04:56The rule of pre-order,
04:57was said,
04:58that,
04:59we will visit the route,
05:00then,
05:00we will visit the left subtree,
05:02then,
05:02and then,
05:02we will visit the right subtree.
05:04In other words,
05:05we had studied the in order,
05:07that,
05:07we will visit the left subtree,
05:09then,
05:10we will visit the route,
05:11then,
05:12we will visit the right subtree.
05:13And,
05:14finally,
05:14there was also a post order,
05:16which,
05:17we have learned,
05:18that,
05:18we will visit the left subtree,
05:20then,
05:21we will visit the right subtree,
05:22and,
05:23finally,
05:24we will visit the route.
05:25Now,
05:26there was also a level order traversal,
05:28I hope,
05:29we all remember.
05:30Now,
05:30the special property,
05:31is that,
05:32when we talk about in order traversal,
05:43we will go back to the right subtree,
05:46we will go back to the right subtree,
05:47we will go back to the right subtree,
06:04and,
06:04we will go back to the right subtree,
06:05we will go back to the right subtree,
06:07so,
06:07we will go back to the right subtree,
06:33So we will go back to 5 and print it back to 5. Now we will go back to 5
06:39in the right of the sub-tree.
06:41On the left of the left, we will go back to 6 and print it back to 6. On the
06:46right of the right, we will go back to 6.
06:47We will go back to 6 and back to 5 and 5 to 4. And finally, what will happen?
06:53At the end of this whole traversal, we will get our in-order sequence.
06:58And if we analyze this in-order sequence, this is our sorted sequence.
07:05All of our numbers are sorted sequence.
07:08So this is a very special and important property that we have to keep in mind.
07:13In fact, we have to keep in mind that in-order traversal is sorted for a BST.
07:18It is sorted for many times. This property will help us whenever we have questions.
07:24So we have to simply in-order.
07:25We will automatically get our sorted sequence automatically.