How To Delete Root Node In Binary Search Tree | Python Program | Data Structure | Part 2

  Рет қаралды 24,225

Amulya's Academy

Amulya's Academy

Күн бұрын

Пікірлер
@ianlin641
@ianlin641 3 жыл бұрын
Thanks a lot for your contributions to the Python community. The most detailed and clear explanation of BST implemented by Python I have ever seen on KZbin and I will follow the other ones of your channel. It helps a lot for Python self-learners like me. Appreciate that, madam.
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
Thank you so much 😊❤️
@jagajeevansanapala2638
@jagajeevansanapala2638 2 жыл бұрын
This playlist is made so clearly by putting in a lot of efforts…. Wonderful explanation…. You made understanding these complex topics a cake walk
@pini5076
@pini5076 Жыл бұрын
thank you so much! for real you are the best teacher ever!
@vachapatel9874
@vachapatel9874 3 жыл бұрын
amazing work madam...thank you so much for these videos!!
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
Pleasure 😊
@sumanhowlader3657
@sumanhowlader3657 3 жыл бұрын
Thanks a lot mam, the way you explain is great. One problem i'm having in this section . i am not able to delete root key when there is two child . can you PLEASE check it once THE CODE: def delete(self,data,curr): #1st step - check tree is empty or not if self.key is None: print("Tree is empty") return # 2nd step - find the node [left s.t -- right s.t-- root] if data < self.key : #i.e if the node is available then it should be in left s.t if self.lchild: # if left s.t is available self.lchild = self.lchild.delete(data,curr) # we will call the delet func recursively else: print("given node is not present in the tree") elif data > self.key : if self.rchild: # if right s.t is available self.rchild = self.rchild.delete(data,curr) # we will call the delet func recursively else: print("given node is not present in the tree") # after perfoming the upper part now self is point towards the node which we want to delet # 3rd step -- delete that node [1. 0 child(no need to replace) , 2. 1 child(replace with child) , 3. 2 child (replace with greatest value in left s.t or smallest value in right s.t) ] ## NOW WE WILL MODIFY THIS PART FOR DELETING ROOT KEY WHEN ROOT KEY HAS ONLY ONE CHILD else: # if node contain 0 child or one child if self.lchild is None: temp = self.rchild if data == curr: # ###we are deleting root key when it hs only one child self.key = temp.key self.rchild = temp.rchild self.lchild = temp.lchild temp = None #i.e after assigning we are deleting the child return self = None return temp if self.rchild is None: temp = self.lchild if data == curr: # ###we are deleting root key when it hs only one child self.key = temp.key self.rchild = temp.rchild self.lchild = temp.lchild temp = None #i.e after assigning we are deleting the child return self = None return temp # if node contain 2 child [ here we are taking condition replace with smallest value in right s.t] node = self.rchild while node.lchild: # min value will be in left side only node = node.lchild # now node point towards that value which we have to place in required position self.key = node.key # now deleting that smallest node self.rchild = self.rchild.delete(node.key,curr) return self def count(node): if node is None: # this s the base case need for using recursion return 0 return 1 + count(node.lchild) + count(node.rchild) # if node is none is not true then we use this recursive case to count no of node in bst root = BST(10) # this the root key # in this case one node is already there before insertion # now using loop we can insert values in aa tree from a given list list1 = [5,12] for i in list1: root.insert(i) # we are calling the insert method to insert the values from list to tree print(count(root)) print("preorder") root.preorder() print() # now before performing the deletion operation we have to check no of nodes if count(root)>1: root.delete(10,root.key) else: print("cant perform deletion operation!") print("after deleting:") root.preorder() RESULT: 3 preorder 10 5 12 after deleting: 10 5 12
@vardhanash7127
@vardhanash7127 3 жыл бұрын
Very detailed and explanatory
@chandankumar-wp1kx
@chandankumar-wp1kx Жыл бұрын
thanks
@CC-mx5uy
@CC-mx5uy 2 жыл бұрын
could u please write a shorter code and can u explain it cause other than these 2 videos of deletion i understood all othermethods so pls put this deletion method in a simple manner and xplain it mam
@gusionfusion1073
@gusionfusion1073 2 жыл бұрын
maam, is it mandatory to create count because in previous video delete function , it is doing all the jobs right? like there you have shown if want to delete node with 2 child we were able to do it easily even if it was for root node (you have shown in white board) ..... So only things that was missing in previous delete() was not identifying if there only root node in the tree right? so i believe we only need to write that part
@nandakishor5889
@nandakishor5889 Ай бұрын
You can mention this thing in previous video. Wasted 2 days because of root node doesn't deleted after previous video😪
@sk-sm5zg
@sk-sm5zg 2 жыл бұрын
Why count function is defined outside the class ??
@PradeepPanchariya1
@PradeepPanchariya1 2 жыл бұрын
Hi, Can you elaborate the count function, So we can get intuitive how does it work?
@selvanm2872
@selvanm2872 3 жыл бұрын
thank you 👍👌
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
Pleasure 😊
@hermanunspieters6969
@hermanunspieters6969 3 жыл бұрын
I'm bit confused about how the self variables work, I don't understand how it changes (How it gets a new value when the recursion method is ran)
@theberozgaar2815
@theberozgaar2815 2 жыл бұрын
It does not work for the case like, 20,11,6,13,3,8,12,15
@anshukumarvikal3184
@anshukumarvikal3184 3 жыл бұрын
Mam please make a vedio on factorial addition
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
Noted 😊
@babug4455
@babug4455 2 жыл бұрын
Mam instead of root.delete(10, root.key) or root.delete(10) we code like root=root.delete(10) if a tree contains more than one node and root node have one child.......and self =None (this line dont need) because, here self becomes a variable for only for that methods
@Sandeep-tv1px
@Sandeep-tv1px Жыл бұрын
You cannot do this -> root=root.delete(10) because 'root' will have value of type 'None' instead of type BST(Binary Search Tree) and we won't be able to call any BST methods on that. -> root.preorder_traversal() # gives error We need to create an empty node, instead of just storing None in root. This below 'if statment' inside delete() method might do the trick: # if deleting the root node and it is the only node in the tree.(i.e root node with no-child) if (data == curr) and (self.lchild is None) and (self.rchild is None): self.key = None
@babug4455
@babug4455 9 ай бұрын
@@Sandeep-tv1px No.....root=root.delete(10) works for all conditions, if root a have no child, we delete the root, root becomes none, because there is no root, so we cant traverse, we are not assigning root.key =None, we have to delete the root node, so root becomes none if root have no child, and above video self=None (this line is not need) def delete(self,data): if self.key>data: if self.left!=None: self.left=self.left.delete(data) else: print("Not present") elif self.key
@babug4455
@babug4455 9 ай бұрын
@@Sandeep-tv1px No....root=root.delete(10) works in all conditions, if a node have no child, then deleting that node, we cant traverse. we are not assigning root.key=None, we have to delete that node, this means node becomes None.. so we cant traverse and self =None (this line dont need)
@king-yv3ln
@king-yv3ln 5 ай бұрын
can you share the codes please?
@chaitanyadattu5640
@chaitanyadattu5640 2 жыл бұрын
❤️❤️❤️
@sabarishsusiraj577
@sabarishsusiraj577 3 жыл бұрын
Why it isn't deleting root with one node?
@murtykunapuli8084
@murtykunapuli8084 3 жыл бұрын
Mam your explanation is very good I tried for the deletion of node in BST for some values its nor working the list I tried for [10,20, 22,30, 7, 1, 5, 6, 9, 22, 33, 45, 2, 66,75] all it is ok except for 5 ,7,10 I am getting error message TypeError: '
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
Give me the program, I will check 😊
@rajarajan1338
@rajarajan1338 11 ай бұрын
May be you assigned self.key = BST(data) instead of self.left (or self.right) = BST(data)
@nareshkumar2008
@nareshkumar2008 2 жыл бұрын
Any body have complete code . pls share
@RAJ05ADITYA
@RAJ05ADITYA 2 жыл бұрын
I think there is some error in the program. When we are deleting root (i.e. 10 in this case), then it is working fine. But when we are deleting any other node (e.g. say 11) by calling the delete function, it is saying "TypeError: delete() missing 1 required positional argument: 'curr'". Please have a look at the problem.
@RAJ05ADITYA
@RAJ05ADITYA 2 жыл бұрын
I think at every recursive "delete" function call, we have to also pass the curr as an argument.
@vighneshgaikwad7011
@vighneshgaikwad7011 2 жыл бұрын
@@RAJ05ADITYA yes it is mentoned at the last
@black_eye7105
@black_eye7105 3 жыл бұрын
😊😊😊😊😊
@AmulsAcademy
@AmulsAcademy 3 жыл бұрын
😊😊
@rajatdave2110
@rajatdave2110 Жыл бұрын
def delete(self, key): if self.root: self.root = self._delete(key, self.root) def _delete(self, key, cur_node): if not cur_node: return cur_node elif key < cur_node.key: cur_node.left = self._delete(key, cur_node.left) elif key > cur_node.key: cur_node.right = self._delete(key, cur_node.right) else: if not cur_node.left and not cur_node.right: cur_node = None elif not cur_node.left: cur_node = cur_node.right elif not cur_node.right: cur_node = cur_node.left else: successor = cur_node.right while successor and successor.left: successor = successor.left cur_node.key = successor.key cur_node.right = self._delete(successor.key, cur_node.right) return cur_node
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН
Leetcode - Delete Node in a BST (Python)
8:30
Timothy H Chang
Рет қаралды 15 М.
Python: Binary Search Tree part-2: Remove
13:04
Oggi AI - Artificial Intelligence Today
Рет қаралды 28 М.
Binary Search Tree in Python
22:59
NeuralNine
Рет қаралды 54 М.
Delete a node from Binary Search Tree
18:27
mycodeschool
Рет қаралды 1,1 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 835 М.
C in 100 Seconds
2:25
Fireship
Рет қаралды 3,2 МЛН
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 452 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН