How to use _delete_node method in lisa

Best Python code snippet using lisa_python

d.py

Source:d.py Github

copy

Full Screen

...13 def insert(self, val):14 self.root = self._insert_node(self.root, val)15 16 def delete(self, val):17 self.root = self._delete_node(self.root, val)18 19 def _insert_node(self, cur, val):20 if not cur:21 return TreeNode(val)22 elif val < cur.val:23 cur.left = self._insert_node(cur.left, val)24 elif val > cur.val:25 cur.right = self._insert_node(cur.right, val)26 27 cur.height = 1 + max(self._get_height(cur.left),28 self._get_height(cur.right))29 30 balance = self._get_balance(cur)31 if balance > 1 and val > cur.left.val: # Left-Right case32 cur.left = self._left_rotate(cur.left)33 cur = self._right_rotate(cur)34 35 elif balance > 1 and val < cur.left.val: # Left-Left case36 cur = self._right_rotate(cur)37 38 elif balance < -1 and val > cur.right.val: # Right-Right case39 cur = self._left_rotate(cur)40 41 elif balance < -1 and val < cur.right.val: # Right-Left case42 cur.right = self._right_rotate(cur.right)43 cur = self._left_rotate(cur)44 return cur45 46 def _delete_node(self, cur, val):47 if not cur:48 return False49 elif cur == self.root and cur.val == val:50 if cur.left and cur.right:51 pre_val = self._find_predecessor(cur.left)52 self._delete_node(cur, pre_val)53 cur.val = pre_val54 elif cur.left or cur.right:55 if cur.left:56 self.root = cur.left57 elif cur.right:58 self.root = cur.right59 else:60 self.root = None61 62 elif cur.left and cur.left.val == val:63 if cur.left.left and cur.left.right:64 pre_val = self._find_predecessor(cur.left.left)65 self._delete_node(cur, pre_val)66 cur.left.val = pre_val67 cur.left.height = 1 + \68 max(self._get_height(cur.left.left),69 self._get_height(cur.left.right))70 elif cur.left.left or cur.left.right:71 if cur.left.left:72 cur.left = cur.left.left73 elif cur.left.right:74 cur.left = cur.left.right75 cur.left.height = 1 + \76 max(self._get_height(cur.left.left),77 self._get_height(cur.left.right))78 else:79 cur.left = None80 cur.height = 1 + max(self._get_height(cur.left),81 self._get_height(cur.right))82 83 elif cur.right and cur.right.val == val:84 if cur.right.left and cur.right.right:85 pre_val = self._find_predecessor(cur.right.left)86 self._delete_node(cur, pre_val)87 cur.right.val = pre_val88 cur.right.height = 1 + \89 max(self._get_height(cur.right.left),90 self._get_height(cur.right.right))91 elif cur.right.left or cur.right.right:92 if cur.right.left:93 cur.right = cur.right.left94 elif cur.right.right:95 cur.right = cur.right.right96 cur.right.height = 1 + \97 max(self._get_height(cur.right.left),98 self._get_height(cur.right.right))99 else:100 cur.right = None101 cur.height = 1 + max(self._get_height(cur.left),102 self._get_height(cur.right))103 104 elif cur.val > val:105 cur.left = self._delete_node(cur.left, val)106 107 elif cur.val < val:108 cur.right = self._delete_node(cur.right, val)109 110 balance = self._get_balance(cur)111 # Left-Left case112 if balance > 1 and self._get_balance(cur.left) >= 0:113 cur = self._right_rotate(cur)114 # Left-Right case115 elif balance > 1 and self._get_balance(cur.left) < 0:116 cur.left = self._left_rotate(cur.left)117 cur = self._right_rotate(cur)118 # Right-Left case119 elif balance < -1 and self._get_balance(cur.right) > 0:120 cur.right = self._right_rotate(cur.right)121 cur = self._left_rotate(cur)122 # Right-Right case...

Full Screen

Full Screen

treap.py

Source:treap.py Github

copy

Full Screen

...81 node = node.left_rotate()82 return node83 def insert(self, value):84 self.root = self._insert_node(self.root, value)85 def _delete_node(self, node, value):86 if not node:87 raise ValueError('Not found')88 if value == node.value:89 if (node.left is None) and (node.right is None): # Leaf nodes can be simply deleted.90 self.size -= 191 return None92 elif node.left is None:93 self.size -= 194 return node.right95 elif node.right is None:96 self.size -= 197 return node.left98 elif node.left and node.right:99 if node.left.priority < node.right.priority:100 node = node.right_rotate()101 node.right = self._delete_node(node.right, value)102 else:103 node = node.left_rotate()104 node.left = self._delete_node(node.left, value)105 elif value < node.value:106 node.left = self._delete_node(node.left, value)107 elif value > node.value:108 node.right = self._delete_node(node.right, value)109 return node110 def delete(self, value):111 self.root = self._delete_node(self.root, value)112 def inorder_traverse(self, node):113 if not node:114 return115 yield from self.inorder_traverse(node.left)116 yield node.value117 yield from self.inorder_traverse(node.right)118 def levelorder_traverse_nodes(self, node=DEFAULT_TO_ROOT):119 if node == self.DEFAULT_TO_ROOT:120 node = self.root121 current_level = [node, ]122 while current_level:123 next_level = []124 for node in current_level:125 if node:...

Full Screen

Full Screen

Binary_Search_Tree.py

Source:Binary_Search_Tree.py Github

copy

Full Screen

...68 def delete_node(self, data):69 if self.node is None:70 return False71 else:72 self.node = self._delete_node(data, self.node)73 def _delete_node(self, data, cur_node):74 if cur_node is None:75 return cur_node76 if data < cur_node.root:77 cur_node.left = self._delete_node(data, cur_node.left)78 elif data > cur_node.root:79 cur_node.right = self._delete_node(data, cur_node.right)80 else:81 # 子ノードが一つまたはなしの場合82 if cur_node.left is None:83 tmp = cur_node.right84 cur_node = None85 return tmp86 elif cur_node.right is None:87 tmp = cur_node.left88 cur_node.left = None89 return tmp90 # 子ノードが2つの場合は右側の一番小さい値91 tmp = self.find_min(cur_node.right)92 cur_node.root = tmp.root93 cur_node.right = self._delete_node(tmp.root, cur_node.right)94 return cur_node95 # 探索 ループ版 メソッド96 def find_by_while(self, data):97 if self.node is None:98 return None99 cur_node = self.node100 while cur_node:101 if data == cur_node.root:102 return True103 if data < cur_node.root:104 cur_node = cur_node.left105 else:106 cur_node = cur_node.right107 return False...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful