How to use current_node method in Gherkin-python

Best Python code snippet using gherkin-python

binary_search_tree.py

Source:binary_search_tree.py Github

copy

Full Screen

1from queue_array import Queue2class TreeNode:3 4 def __init__(self, key, data, left=None, right=None):5 self.key = key6 self.data = data7 self.left = left8 self.right = right910 11class BinarySearchTree:1213 def __init__(self): # Returns empty BST14 self.root = None1516 17 def is_empty(self): # returns True if tree is empty, else False18 return self.root == None1920 21 def search(self, key): # returns True if key is in a node of the tree, else False22 if self.is_empty():23 return False24 else:25 return self.search_helper(key, self.root)262728 def search_helper(self, key, current_Node):29 if current_Node == None:30 return False31 if current_Node.key == key:32 return True33 elif key < current_Node.key: # search left subtree34 return self.search_helper(key, current_Node.left)35 else: # search right subtree36 return self.search_helper(key, current_Node.right)373839 def insert(self, key, data=None): # inserts new node w/ key and data40 # If an item with the given key is already in the BST, 41 # the data in the tree will be replaced with the new data42 # Example creation of node: temp = TreeNode(key, data)43 if self.is_empty():44 self.root = TreeNode(key, data) # if there is no root node create a TreeNode and set it to be the root node45 else:46 self.insert_helper(key, data, self.root)4748 49 def insert_helper(self, key, data, current_Node):50 if key == current_Node.key: # replace data51 current_Node.data = data52 elif key < current_Node.key: # search left subtree53 if current_Node.left:54 self.insert_helper(key, data, current_Node.left)55 else:56 current_Node.left = TreeNode(key, data)57 else: # search right subtree58 if current_Node.right:59 self.insert_helper(key, data, current_Node.right)60 else:61 current_Node.right = TreeNode(key, data)626364 def find_min(self): # returns a tuple with min key and data in the BST65 # returns None if the tree is empty66 if self.is_empty():67 return None68 else:69 return self.find_min_helper(self.root)7071 72 def find_min_helper(self, current_Node):73 while current_Node.left != None:74 return self.find_min_helper(current_Node.left)75 return (current_Node.key, current_Node.data)7677 78 def find_max(self): # returns a tuple with max key and data in the BST79 # returns None if the tree is empty80 if self.is_empty():81 return None82 else:83 return self.find_max_helper(self.root)8485 86 def find_max_helper(self, current_Node):87 while current_Node.right != None:88 return self.find_max_helper(current_Node.right)89 return (current_Node.key, current_Node.data)9091 92 def inorder_list(self): # return Python list of BST keys representing in-order traversal of BST93 # ascending order94 l = []95 if self.is_empty():96 return []97 return self.inorder_list_helper(self.root, l)9899 100 def inorder_list_helper(self, current_Node, l):101 if current_Node == None:102 return103 self.inorder_list_helper(current_Node.left, l)104 l.append(current_Node.key)105 self.inorder_list_helper(current_Node.right, l)106 return l107108109 def preorder_list(self): # return Python list of BST keys representing pre-order traversal of BST110 # good for making a copy of the tree111 l = []112 if self.is_empty():113 return []114 return self.preorder_list_helper(self.root, l)115116 117 def preorder_list_helper(self, current_Node, l):118 if current_Node == None:119 return120 l.append(current_Node.key)121 self.preorder_list_helper(current_Node.left, l)122 self.preorder_list_helper(current_Node.right, l)123 return l124125 126 def postorder(self): # return Python list of BST keys representing pre-order traversal of BST127 # best for deleting the tree128 l = []129 if self.is_empty():130 return []131 return self.postorder_list_helper(self.root, l)132133 134 def postorder_list_helper(self, current_Node, l):135 if current_Node == None:136 return137 self.postorder_list_helper(current_Node.left, l)138 self.postorder_list_helper(current_Node.right, l)139 l.append(current_Node.key)140 return l141142143 def tree_height(self): # return the height of the tree144 # returns None if tree is empty145 if self.is_empty():146 return None147 return self.tree_height_helper(self.root) - 1148149 150 def tree_height_helper(self, current_Node):151 if current_Node == None:152 return 0153 left_depth = self.tree_height_helper(current_Node.left) + 1154 right_depth = self.tree_height_helper(current_Node.right) + 1155 return max(left_depth, right_depth)156157158 def delete(self, key): # deletes node containing key159 # Will need to consider all cases 160 # This is the most difficult method - save it for last, so that161 # if you cannot get it to work, you can still get credit for 162 # the other methods163 # Returns True if the item was deleted, False otherwise164 if self.search(key):165 if self.root.left == None and self.root.right == None:166 self.root = None167 return True168 elif key == self.root.key: # delete root169 if self.root.left:170 self.root.key = self.find_max_node(self.root.left)171 self.delete_helper(self.root.key, self.root)172 else:173 self.root.key = self.find_min_node(self.root.right)174 self.delete_helper(self.root.key, self.root)175 return True176 else:177 self.delete_helper(key, self.root)178 return True179 else:180 return False181 182 183 def delete_helper(self, key, current_Node):184 if key <= current_Node.key and current_Node.left: # search left subtree185 if current_Node.left: # unneeded186 if current_Node.left.key == key:187 # Then DELETE current_Node.left188 if current_Node.left.left == None and current_Node.left.right == None: # leaf case189 current_Node.left = None190191 elif current_Node.left.left and current_Node.left.right: # 2 child case192 current_Node.left.key = self.find_max_node(self.root.left.left)193 self.delete_helper(current_Node.left.key, current_Node.left)194 elif current_Node.left.left and not current_Node.left.right: # single left child case195 current_Node.left = current_Node.left.left196 elif not current_Node.left.left and current_Node.left.right: # single right child case197 current_Node.left = current_Node.left.right198 else:199 return self.delete_helper(key, current_Node.left)200 else: # search right subtree201 if current_Node.right:202 if current_Node.right.key == key:203 # delete it204 if current_Node.right.left == None and current_Node.right.right == None: # leaf case205 current_Node.right = None206 elif current_Node.right.left and current_Node.right.right:207 current_Node.right.key = self.find_max_node(self.root.right.left)208 self.delete_helper(current_Node.right.key, current_Node.right)209 elif current_Node.right.left and not current_Node.right.right: # single left child case210 current_Node.right = current_Node.right.left211 elif not current_Node.right.left and current_Node.right.right: # single right child case212 current_Node.right = current_Node.right.right213 else:214 return self.delete_helper(key, current_Node.right)215216 217 def find_min_node(self, current_Node): # return min key218 while current_Node.left != None:219 return self.find_min_node(current_Node.left)220 return current_Node.key221 222 223 def find_max_node(self, current_Node): # return max key224 while current_Node.right != None:225 return self.find_max_node(current_Node.right)226 return current_Node.key227228'''229tree = BinarySearchTree()230tree.insert('S', 69)231tree.insert('A', 69)232tree.insert('L', 69)233tree.insert('W', 69)234tree.insert('D', 69)235tree.insert('N', 69)236tree.insert('T', 69)237print(tree.search(12))238tree.delete(26)239print(tree.find_min())240print(tree.preorder_list()) ...

Full Screen

Full Screen

BinaryTree.py

Source:BinaryTree.py Github

copy

Full Screen

1class TreeNode:2 def __init__(self, key, val, left=None, right=None, parent=None):3 self.key = key4 self.val = val5 self.leftChild = left6 self.rightChild = right7 self.parent = parent8 def has_left_child(self):9 return self.leftChild10 def has_right_child(self):11 return self.rightChild12 def is_left_child(self):13 return self.parent and self.parent.leftChild == self14 def is_right_child(self):15 return self.parent and self.parent.rightChild == self16 def is_root(self):17 return not self.parent18 def is_leaf(self):19 return not (self.leftChild or self.rightChild)20 def has_any_children(self):21 return self.leftChild or self.rightChild22 def has_all_children(self):23 return self.leftChild and self.rightChild24 def replace_data(self, key, val, lc, rc):25 self.key = key26 self.val = val27 self.leftChild = lc28 self.rightChild = rc29 if self.has_left_child():30 self.leftChild.parent = self31 if self.has_right_child():32 self.rightChild.parent = self33 def __iter__(self):34 if self:35 if self.has_left_child():36 for elem in self.leftChild:37 if elem is not None:38 yield elem39 yield self.val40 if self.has_right_child():41 for elem in self.rightChild:42 if elem is not None:43 yield elem44class BinaryTree:45 def __init__(self):46 self.root = None47 self.size = 048 def length(self):49 return self.size50 def __len__(self):51 return self.size52 def _put(self, key, val, current_node):53 if key < current_node.key:54 if current_node.has_left_child():55 self._put(key, val, current_node.leftChild)56 else:57 current_node.leftChild = TreeNode(key, val, parent=current_node)58 else:59 if current_node.has_right_child():60 self._put(key, val, current_node.rightChild)61 else:62 current_node.rightChild = TreeNode(key, val, parent=current_node)63 def put(self, key, val):64 if self.root is None:65 self.root = TreeNode(key, val)66 else:67 self._put(key, val, self.root)68 self.size = self.size + 169 def __setitem__(self, key, value):70 self.put(key, value)71 def _get(self, key, current_node):72 if not current_node:73 return None74 elif current_node.key == key:75 return current_node76 elif key < current_node.key:77 return self._get(key, current_node.leftChild)78 else:79 return self._get(key, current_node.rightChild)80 def get(self, key):81 if self.root is None:82 return None83 else:84 res = self._get(key, self.root)85 if res is None:86 return None87 else:88 return res.val89 def __getitem__(self, key):90 return self.get(key)91 def __contains__(self, key):92 if self._get(key, self.root) is None:93 return False94 else:95 return True96 def find_successor(self):97 successor = None98 if self.hasRightChild():99 successor = self.rightChild.find_min()100 else:101 if self.parent:102 if self.isLeftChild():103 successor = self.parent104 else:105 self.parent.rightChild = None106 successor = self.parent.find_c()107 self.parent.rightChild = self108 return successor109 def find_min(self):110 current = self111 while current.hasLeftChild():112 current = current.leftChild113 return current114 def splice_out(self):115 if self.isLeaf():116 if self.isLeftChild():117 self.parent.leftChild = None118 else:119 self.parent.rightChild = None120 elif self.hasAnyChildren():121 if self.hasLeftChild():122 if self.isLeftChild():123 self.parent.leftChild = self.leftChild124 else:125 self.parent.rightChild = self.leftChild126 self.leftChild.parent = self.parent127 else:128 if self.isLeftChild():129 self.parent.leftChild = self.rightChild130 else:131 self.parent.rightChild = self.rightChild132 self.rightChild.parent = self.parent133 def remove(self, current_node):134 if current_node.is_leaf():135 if current_node == current_node.parent.leftChild:136 current_node.parent.leftChild = None137 else:138 current_node.parent.rightChild = None139 elif current_node.hasBothChildren(): # interior140 successor = current_node.find_successor()141 successor.splice_out()142 current_node.key = successor.key143 current_node.payload = successor.payload144 else:145 if current_node.has_left_child():146 if current_node.is_left_child():147 current_node.leftChild.parent = current_node.parent148 current_node.parent.leftChild = current_node.leftChild149 elif current_node.is_right_child():150 current_node.leftChild.parent = current_node.parent151 current_node.parent.rightChild = current_node.leftChild152 else:153 current_node.replaceNodeData(154 current_node.leftChild.key,155 current_node.leftChild.payload,156 current_node.leftChild.leftChild,157 current_node.leftChild.rightChild,158 )159 else:160 if current_node.is_left_child():161 current_node.rightChild.parent = current_node.parent162 current_node.parent.leftChild = current_node.rightChild163 elif current_node.is_right_child():164 current_node.rightChild.parent = current_node.parent165 current_node.parent.rightChild = current_node.rightChild166 else:167 current_node.replaceNodeData(168 current_node.rightChild.key,169 current_node.rightChild.payload,170 current_node.rightChild.leftChild,171 current_node.rightChild.rightChild,172 )173 def delete(self, key):174 if self.size > 1:175 node_for_remove = self._get(key, self.root)176 if node_for_remove:177 self.remove(node_for_remove)178 self.size = self.size - 1179 else:180 raise KeyError("Error, node with key is not found")181 elif self.size == 1 and self.root.key == key:182 self.root = None183 self.size = 0184 else:185 raise KeyError("Error, node with key is not found")186 def __delitem__(self, key):187 self.delete(key)188 def __iter__(self):189 for i in self.root:...

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 Gherkin-python 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