def test_left(self): self.subject.root = Node(80) self.subject.root.left = Node(30) result = self.subject.traverse(mode='postorder') self.assertEqual([30, 80], result)
def test_left(self): self.subject.root = Node(100) self.subject.root.left = Node(50) result = self.subject.search(50) self.assertEqual(Node(50), result)
def test_in(): root = Node(1) root.insert(2) root.insert(3) assert (0 in root) == False assert (1 in root) == True assert (3 in root) == True
def test_right(self): self.subject.root = Node(100) self.subject.root.right = Node(150) result = self.subject.search(150) self.assertEqual(Node(150), result)
def test_bst_left_setter_child(): """test that left child setter works""" from bst import Node node1 = Node(9) node2 = Node(7) node1.left = node2 assert node1.left.val == node2.val
def test_right(self): self.subject.root = Node(80) self.subject.root.right = Node(100) result = self.subject.traverse(mode='level_order') self.assertEqual([80, 100], result)
def test_bst_right_setter_parent(): """test that right child setter sets parent""" from bst import Node node1 = Node(7) node2 = Node(9) node1.right = node2 assert node2.parent.val == node1.val
def test_bst_right_setter_child(): """test rigth child setter works""" from bst import Node node1 = Node(7) node2 = Node(9) node1.right = node2 assert node1.right.val == node2.val
def test_bst_parent_setter_child_parent(): """test parent setter works properly""" from bst import Node node1 = Node(1) node2 = Node(2) node2.parent = node1 assert node1.val == node2.parent.val
def test_left(self): self.subject.root = Node(80) self.subject.root.left = Node(30) result = self.subject.traverse(mode='level_order') self.assertEqual([80, 30], result)
def test_bst_parent_setter_parent_child_left(): """test that parent setter sets child left properly""" from bst import Node node1 = Node(2) node2 = Node(1) node2.parent = node1 assert node1.left.val == node2.val
def test_height_one(self): self.subject.root = Node(5) self.subject.root.right = Node(8) result = self.subject.calculate_height() self.assertEqual(1, result)
def test_right(self): self.subject.root = Node(80) self.subject.root.right = Node(100) result = self.subject.traverse(mode='postorder') self.assertEqual([100, 80], result)
def is_size_consistent(current): if current is None: return True if current.count != 1 + Node.size(current.left) + \ Node.size(current.right): return False return is_size_consistent(current.left) and \ is_size_consistent(current.right)
def test_bst_del_right_child(): """test deleter on right child""" from bst import Node node1 = Node(7) node2 = Node(9) node1.right = node2 del node1.right assert node1.right is None
def test_find(): root = Node(1) root.insert(2) root.insert(-5) assert root.find(1) is root assert root.find(100) is None assert root.find(2) is root.right assert root.find(-5) is root.left
def test_left_subtree(self): self.subject.root = Node(5) self.subject.root.left = Node(4) self.subject.root.left.left = Node(3) result = self.subject.traverse(mode='level_order') self.assertEqual([5, 4, 3], result)
def test_right_subtree(self): self.subject.root = Node(3) self.subject.root.right = Node(4) self.subject.root.right.right = Node(5) result = self.subject.traverse(mode='level_order') self.assertEqual([3, 4, 5], result)
def _create_min_bst(self, array, start, end): if end < start: return None mid = (start + end) // 2 node = Node(array[mid]) node.left = self._create_min_bst(array, start, mid - 1) node.right = self._create_min_bst(array, mid + 1, end) return node
def test_bst_del_left_child(): """test deleter on left child""" from bst import Node node1 = Node(9) node2 = Node(7) node1.left = node2 del node1.left assert node1.left is None
def test_rotate_left(): from bst import Node node1 = Node() node2 = Node() node2.right = node1 node1.rotate_right() assert node1.right == node2 assert not node2.left
def test_init(self): node1 = Node(5) node2 = Node(4) assert (node1.data == 5) assert (node1.left == None) assert (node1.right == None) assert (node2.data == 4) assert (node2.left == None) assert (node2.right == None)
def test_bst_init_node_has_right(empty_bst): """test Node picks up arguments and has_right_child == that input""" from bst import Node a = Node(empty_bst.input_val, empty_bst.input_val, empty_bst.input_val, empty_bst.input_val ) assert a.has_right_child() == empty_bst.input_val
def balanced_bst_from_sorted_array(arr, start, end): if start > end: return mid = (start + end) / 2 root = Node(arr[mid]) root.left = balanced_bst_from_sorted_array(arr, start, mid - 1) root.right = balanced_bst_from_sorted_array(arr, mid + 1, end) return root
def inordersearch(lroot, mroot): arr = [] if lroot: arr = arr + inordersearch(lroot.left, mroot) if lroot.val != None and Node.search( mroot, lroot.val) != None and Node.search(mroot, lroot.val) != mroot: arr.append(lroot.val) arr = arr + inordersearch(lroot.right, mroot) return arr
def __clone(node): if not node: return new_root = Node(node.key, node.data) new_left = __clone(node.left) new_right = __clone(node.right) new_root.left = new_left new_root.right = new_right return new_root
def test_uneven_tree(self): self.subject.root = Node(10) self.subject.root.left = Node(8) self.subject.root.right = Node(12) self.subject.root.left.right = Node(9) self.subject.root.right.left = Node(11) result = self.subject.traverse(mode='level_order') self.assertEqual([10, 8, 12, 9, 11], result)
def test_uneven_tree(self): self.subject.root = Node(10) self.subject.root.left = Node(8) self.subject.root.right = Node(12) self.subject.root.left.right = Node(9) self.subject.root.right.left = Node(11) result = self.subject.traverse(mode='postorder') self.assertEqual([9, 8, 11, 12, 10], result)
def _sorted_array_to_bst(array, left, right): if left == right: return Node(array[left]) if left > right: return None mid = (left + right) / 2 root = Node(array[mid]) root.left = _sorted_array_to_bst(array, left, mid-1) root.right = _sorted_array_to_bst(array, mid+1, right) return root
def test_Node_depth(): from bst import Node node1 = Node() node2 = Node() node3 = Node() node4 = Node() node1.left = node2 node1.right = node3 node3.right = node4 results = [node1.depth, node2.depth, node3.depth] assert results == [3, 1, 2]
def filled_tree(): A = Node(6) a = BST() a.root = A A.left = 4 A.right = 7 A.left.left = 3 A.left.right = 5 A.right.right = 8 a._size = 6 return a
def build_tree(in_order_list, pre_order_list): if pre_order_list and in_order_list: root_val = pre_order_list.pop(0) root = Node(root_val) in_order_index = in_order_list.index(root_val) root.left = build_tree(in_order_list[:in_order_index], pre_order_list) root.right = build_tree(in_order_list[in_order_index + 1:], pre_order_list) return root else: return None
def test_init_node(self): """ Tests initialization of Node """ n1 = Node('data') self.assertIsInstance(n1, Node) self.assertEqual(n1.data, 'data') self.assertIsNone(n1.left) self.assertIsNone(n1.right) # make sure 'value' is writeable n1.data = "new_val" self.assertEqual(n1.data, 'new_val')
def getFreeHoursInCommon1(a, b): result = [[] for i in range(period)] leader = [Node.inorderarr(a[x], []) for x in range(period)] #without time in common #leader = [[12, 13, 13.5], [10, 13.5, 15], [13.5, 15], [13.5, 14], [11, 13.5], [13.5], [13.5]] #with time in common #print(leader) for i in range(len(leader)): for j in range(len(leader[i])): if Node.search(b[i], leader[i][j]) != None and Node.search( b[i], leader[i][j]) != b[i]: result[i].append(leader[i][j]) return result
def uneven_tree(): A = Node(6) a = BST() a.root = A A.left = 4 A.right = 7 A.left.left = 3 A.left.right = 5 A.right.right = 8 A.right.right.right = 9 A.right.right.right.right = 10 a._size = 6 return a
def get_results(file): ''' Pass file name to get result of both Problems. ''' data,root =process_json_file('{0}'.format(file)) root = Node(root) for value in data: root.insert(value['score']) height = root.maxDepth(root) unique_value = len(data) + 1 # Since Middle element is removed as root Node. print "Height of Standard Binary Tree is : {0}".format(height) print 'Unique dict values are : {0}'.format(unique_value)
def create_tree_1(self): tree = Node(8) tree.insert(12) tree.insert(3) tree.insert(4) # Change to 7 to simulate a change in testing fixture tree.insert(6) return tree
def test_left_rotation_three_nodes(): """Test that a right-right becomes balanced.""" from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(15) node3 = Node(20) new_bst.head = node1 node1.right = node2 node2.parent = node1 node2.right = node3 node3.parent = node2 new_bst.head.left_rotation() assert node2.parent is None assert node2.left == node1 assert node2.right == node3 assert node2.left.parent == node2
def test_right_left_conversion_three_nodes(): """ Test that given three nodes in a right-left state converts to right-right. """ from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(15) node3 = Node(12) new_bst.head = node1 node1.right = node2 node2.parent = node1 node2.left = node3 node3.parent = node2 new_bst.head.right_left_conversion() assert new_bst.head.right == node3 assert new_bst.head.right.right == node2 assert new_bst.head.right.right.parent == node3 assert new_bst.head.right.parent == node1
def test_Node_setting_left(): from bst import Node node1 = Node() node2 = Node() node1.left = node2 assert node1.left == node2
def test_right_left_conversion_six_nodes(): """ Test that given six nodes in a left-right state converts to left-left. """ from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(5) node3 = Node(15) node4 = Node(20) node5 = Node(14) node6 = Node(13) new_bst.head = node1 node1.left = node2 node2.parent = node1 node1.right = node3 node3.parent = node1 node3.right = node4 node4.parent = node3 node3.left = node5 node5.parent = node3 node5.left = node6 node6.parent = node5 new_bst.head.right_left_conversion() assert new_bst.head.right == node5 assert new_bst.head.right.right == node3 assert new_bst.head.right.right.right == node4
def test_Node_setting_right(): from bst import Node node1 = Node() node2 = Node() node1.right = node2 assert node1.right == node2
def test_Node_parent(): from bst import Node node1 = Node() node2 = Node() node1.right = node2 assert node2.parent
def test_depth_many(): """Test that a node with one extra on the left has a depth of 2.""" from bst import Node a = Node(20) a.left = Node(16) assert a.depth() == 2
def test_balance_right(): """Test that a right side is larger and returns a negative int.""" from bst import Node a = Node(20) a.right = Node(23) assert a.balance() == -1
def test_balance_left(): """Test that a left side is larger and returns a positive int.""" from bst import Node a = Node(20) a.left = Node(18) assert a.balance() == 1
def node_instance2(): """Create BST instance.""" from bst import Node node = Node() node.val = 7 return node
def test_balance_one(): """Test that a node of one returns a depth of one.""" from bst import Node a = Node(1) assert a.balance() == 0
def test_depth_none(): """Test that a node of nothing returns a value of nothing.""" from bst import Node a = Node() assert a.depth() == 0
def test_balance_none(): """Test that a node of nothing returns a value of nothing.""" from bst import Node a = Node() assert a.balance() == 0
def test_depth_one(): """Test that a node of one returns a depth of one.""" from bst import Node a = Node(1) assert a.depth() == 1