def ExampleTree(self): tree = binary_tree.BinaryTree(1) tree.root.left = binary_tree.Node(2) tree.root.right = binary_tree.Node(3) tree.root.left.left = binary_tree.Node(4) tree.root.left.right = binary_tree.Node(5) return tree
def test_init_node(self): node1 = binary_tree.Node('Cindy', 1) self.assertEqual(node1.key, 'Cindy') self.assertEqual(node1.value, 1) node2 = binary_tree.Node(2, 'Fu') self.assertEqual(node2.key, 2) self.assertEqual(node2.value, 'Fu')
def setUp(self): self.node = bt.Node(1) self.node.left_child = bt.Node(2) self.node.right_child = None self.node_None = bt.create_binary_tree(None) self.node_None1 = bt.create_binary_tree([None]) self.node_full = bt.create_binary_tree( [1, 2, 3, 4, None, None, 5, None, None, 6, None, None])
def test_when_there_are_multiple_same_nodes_in_the_tree(self): root = b.Node(3) e1 = b.Node(3) e2 = b.Node(3) e3 = b.Node(4) e4 = b.Node(5) bt = b.BinaryTree(root) bt.root.lchild = e1 bt.root.rchild = e2 bt.root.lchild.lchild = e3 bt.root.rchild.lchild = e4
def make_testdata(): root = binary_tree.Node(6) root.left = binary_tree.Node(4) root.left.left = binary_tree.Node(1) root.left.left.left = binary_tree.Node(0) root.left.left.right = binary_tree.Node(3) root.right = binary_tree.Node(8) root.right.left = binary_tree.Node(6) root.right.right = binary_tree.Node(9) root.right.right.left = binary_tree.Node(4) root.right.right.right = binary_tree.Node(10) return root
def testInsert(self): tree = self.ExampleTree() node4 = bt.Node(4) tree.insert(node4) self.assertEqual(tree.root.left.left.right.left, node4) tree = self.ExampleTree() node54 = bt.Node(54) tree.insert(node54) self.assertEqual(tree.root.right.right.right.right, node54) # Check inserting into empty tree tree = bst.BST() tree.insert(node4) self.assertEqual(tree.root, node4)
def test_when_there_are_none_of_the_same_nodes_in_the_tree(self): root = b.Node(6) e1 = b.Node(7) e2 = b.Node(8) e3 = b.Node(9) e4 = b.Node(10) bt = b.BinaryTree(root) bt.root.lchild = e1 bt.root.rchild = e2 bt.root.lchild.lchild = e3 bt.root.rchild.lchild = e4 #self.assertEqual(bt.search(6), True) self.assertEqual(bt.search(7), True)
def test_random_concatenate(self): with self.random() as random: # draw k values out of the range [1, 50000) k = random.randint(1, 20000) samples = random.sample(xrange(50000), k) samples.sort() # randomly split samples into three segments: # [a_0, ..., a_l-1] [a_l] [a_l+1, ..., a_n-1] l = random.randint(0, len(samples) - 1) left = samples[:l] middle = samples[l] right = samples[l + 1:] # create balanced trees left_child = self.createAVLTree(left) root = binary_tree.Node(middle) right_child = self.createAVLTree(right) # construct whole tree root.set_child(left_child, middle - 1) root.set_child(right_child, middle + 1) root.update_height() height = root.height # concatenate it new_root = root.concatenate() # invariant: height reduces at most by 1 self.assertTrue(height - 1 <= new_root.height <= height) self.assertAVLTree(new_root) # no element vanished self.assertTreeContains(new_root, samples)
def build_tree(): tree = binary_tree.Node(6) tree.next_value_left = binary_tree.Node(2) tree.next_value_left.next_value_left = binary_tree.Node(-2) tree.next_value_left.next_value_right = binary_tree.Node(4) tree.next_value_right = binary_tree.Node(10) tree.next_value_right.next_value_left = binary_tree.Node(8) tree.next_value_right.next_value_right = binary_tree.Node(12) return tree
def setUpClass(cls): """ A function for initializing a binary tree for testing. """ cls.root = bt.Node(6, 'Root') bt.insert(cls.root, 7, 'A') bt.insert(cls.root, 8, 'B') bt.insert(cls.root, 9, 'C') bt.insert(cls.root, 3, 'D') bt.insert(cls.root, 2, 'E') bt.insert(cls.root, 4, 'F') bt.insert(cls.root, 5, 'G')
def test_creation(self): """ Test the creation of a list. """ bt = binary_tree.BinaryTree() # The BT has an empty root node self.assertIs(bt.root_node, None) # But we can create one with a root node. bt = binary_tree.BinaryTree(binary_tree.Node('g')) self.assertEquals(bt.root_node.data, 'g')
def FADTree(self): a = bt.Node('a') b = bt.Node('b') c = bt.Node('c') d = bt.Node('d') e = bt.Node('e') f = bt.Node('f') g = bt.Node('g') h = bt.Node('h') i = bt.Node('i') j = bt.Node('j') f.left = b f.right = g b.left = a b.right = d d.left = c d.right = e g.right = i i.left = h h.right = j return f
def double_tree_1(root): '''Convert the tree to its double tree''' if root is None: return else: temp = root.left root.left = binary_tree.Node(root.data) root.left.left = temp double_tree_1(root.left.left) double_tree_1(root.right) return root
def double_tree(root): if root is None: return else: double_tree(root.left) double_tree(root.right) temp = root.left root.left = binary_tree.Node(root.data) root.left.left = temp return root
def construct_test_data(): root = binary_tree.Node(4) root.left = binary_tree.Node(2) root.left.left = binary_tree.Node(6) root.left.left.left = binary_tree.Node(7) root.right = binary_tree.Node(1) root.left.right = binary_tree.Node(1) return root
def construct_tree(list, s_in, e_in): if s_in > e_in: return index = (s_in + e_in) / 2 if index < len(list): root = binary_tree.Node(list[index]) root.left = construct_tree(list, s_in, index - 1) root.right = construct_tree(list, index + 1, e_in) return root
def construct_tree(pre, preLN): global index index += 1 if len(pre) == 0: return root = binary_tree.Node(pre[index]) if preLN[index] == "N": root.left = construct_tree(pre, preLN) root.right = construct_tree(pre, preLN) return root
def testRemove(self): # Remove leaf node tree = self.ExampleTree() self.assertTrue(tree.root.right.left.right.left.right) tree.remove_value(31) self.assertTrue(tree.root.right.left.right.left.right is None) # Remove node with one child tree = self.ExampleTree() self.assertEqual(tree.root.right.left.right.left.value, 29) tree.remove_value(29) self.assertEqual(tree.root.right.left.right.left.value, 31) # Remove node with two children tree = self.ExampleTree() self.assertEqual(tree.root.right.value, 43) tree.remove_value(43) self.assertEqual(tree.root.right.value, 47) self.assertEqual(tree.root.right.right.value, 53) self.assertEqual(tree.root.right.left.value, 23) # Remove root node tree = self.ExampleTree() self.assertEqual(tree.root.value, 19) self.assertEqual(tree.root.left.value, 7) self.assertEqual(tree.root.right.value, 43) tree.remove_value(19) self.assertEqual(tree.root.value, 23) self.assertEqual(tree.root.left.value, 7) self.assertEqual(tree.root.right.value, 43) # Remove root node from otherwise empty tree tree = bst.BST(bt.Node(19)) self.assertEqual(tree.root.value, 19) tree.remove_value(19) self.assertEqual(tree.root, None) # Remove root node from lopsided right-only tree tree = self.ExampleTree() tree.root.left = None self.assertEqual(tree.root.value, 19) self.assertEqual(tree.root.right.value, 43) tree.remove_value(19) self.assertEqual(tree.root.value, 43) self.assertEqual(tree.root.left.value, 23) self.assertEqual(tree.root.right.value, 47) # Remove root node from lopsided right-only tree tree = self.ExampleTree() tree.root.right = None self.assertEqual(tree.root.value, 19) tree.remove_value(19) self.assertEqual(tree.root.value, 7) self.assertEqual(tree.root.left.value, 3) self.assertEqual(tree.root.right.value, 11)
def construct_tree(inorder, st_in, end_in): if st_in > end_in: return max_ind = find_max(inorder, st_in, end_in) root = binary_tree.Node(inorder[max_ind]) if st_in == end_in: return root root.left = construct_tree(inorder, st_in, max_ind - 1) root.right = construct_tree(inorder, max_ind + 1, end_in) return root
def insert(array, start, end): """ Algorithm: 1. Insert into tree the middle element of array. 2. Insert into left subtree the left subarry elements 3. Insert into right subtree the right subarray elements. """ if end < start: return None mid = (start + end) / 2 node = binary_tree.Node(array[mid]) node.left = insert(array, start, mid) node.right = insert(array, mid + 1, end) return node
def construct_tree(inorder, preorder, s_in, e_in): global pre_index if s_in > e_in: return root = binary_tree.Node(preorder[pre_index]) pre_index += 1 ind = search(inorder, root.data) root.left = construct_tree(inorder, preorder, s_in, ind - 1) root.right = construct_tree(inorder, preorder, ind + 1, e_in) return root
def construct_tree(head, i): if head is None: return value = get_value_at_index(head, i) if not value: return root = binary_tree.Node(value) root.left = construct_tree(head, 2 * i + 1) root.right = construct_tree(head, 2 * i + 2) return root
def special_tree(sequence, left, right): if left > right: return else: index = left # Find the highest element in the increasing sequence while (index < right and sequence[index] < sequence[index + 1]): index = index + 1 root = binary_tree.Node(sequence[index]) root.left = special_tree(sequence, left, index - 1) root.right = special_tree(sequence, index + 1, right) return root
def construct_tree(head, index): '''Construct tree from linked list and return root of the tree''' current = head count = 0 while (count < index and current): current = current.nextnode count = count + 1 if current is None: return root = binary_tree.Node(current.data) root.left = construct_tree(head, 2 * index + 1) root.right = construct_tree(head, 2 * index + 2) return root
def construct_tree(preorder, inorder, start, end): global preindex if start > end: return root = binary_tree.Node(preorder[preindex]) preindex += 1 if start == end: return root index = inorder.index(root.data) if index >= 0: root.left = construct_tree(preorder, inorder, start, index-1) root.right = construct_tree(preorder, inorder, index+1, end) return root
def update(self, page): """Update page visit counts with Webpage log. Time complexity: O(log N) for BST pop and insert Space complexity: O(1) Args: page: Webpage object """ if page.id not in self.hashmap: self.hashmap[page.id] = 1 self.bst.insert(bt.Node((1, page.id))) return old_count = self.hashmap[page.id] self.hashmap[page.id] += 1 node = self.bst.remove_value((old_count, page.id)) # Update count and place in tree node.value = (node.value[0] + 1, node.value[1]) self.bst.insert(node)
def construct_tree(preorder, maximum, minimum, key): global preIndex if preIndex > len(preorder): return if key > minimum and key < maximum: root = binary_tree.Node(key) preIndex += 1 if preIndex < len(preorder): root.left = construct_tree(preorder, root.data, minimum, preorder[preIndex]) root.right = construct_tree(preorder, maximum, root.data, preorder[preIndex]) return root
def construct_tree(preorder, postorder, st_in, end_in): global index_pre if index_pre >= len(preorder) or st_in > end_in: return else: root = binary_tree.Node(preorder[index_pre]) index_pre += 1 if st_in == end_in: return root # Look for the preorder value in postorder, all the elements before index_pre will be in left subtree index = postorder.index(preorder[index_pre]) if index <= len(postorder): root.left = construct_tree(preorder, postorder, st_in, index) root.right = construct_tree(preorder, postorder, index+1, end_in-1) return root
def test_set_child(self): root = binary_tree.Node(10) left = binary_tree.Node(5) left_left = binary_tree.Node(2) left_left2 = binary_tree.Node(4) left_right = binary_tree.Node(6) left_right_right = binary_tree.Node(8) result = root.set_child(left) self.assertEqual(result, None) result = left.set_child(left_left) self.assertEqual(result, None) result = left.set_child(left_right) self.assertEqual(result, None) result = left_right.set_child(left_right_right) self.assertEqual(result, None) self.assertEqual(root.left_child, left) self.assertEqual(root.right_child, None) self.assertEqual(root.left_child.left_child, left_left) self.assertEqual(root.left_child.right_child, left_right) self.assertEqual(root.left_child.right_child.right_child, left_right_right) # test the replacement of a node replaced = left.set_child(left_left2) self.assertEqual(replaced, left_left) self.assertEqual(root.left_child.left_child, left_left2) # replaced won't be fully detached self.assertEqual(replaced.parent, left) # test set child to None according to the key replaced = left.set_child(None, left_left2.key) self.assertEqual(replaced, left_left2) self.assertEqual(root.left_child.left_child, None)
import sys import time import network_utils as nu import binary_tree as bt # Receiving Rule list and packet list as input arguments rule_file = sys.argv[1] packet_file = sys.argv[2] # Reading all the rule entries and putting them into a list all_rules = nu.read_rules(rule_file) # Creating the root node root = bt.Node() # Converting the Rule's source and destination netIDs into the binary format and putting them into two separate lists src_sub_binaries = [x.src_sub_binary for x in all_rules] dst_sub_binaries = [x.dst_sub_binary for x in all_rules] # Going through all Rule's source and destination sub binaries to create the tree for i in range(0, len(src_sub_binaries)): bt.add_src_nodes(root, src_sub_binaries[i], 0, dst_sub_binaries[i], i) # Draw the tree that was created i the previous step #bt.show(root) # Reading all the incoming packets and putting them into a list packets = nu.read_packets(packet_file) # Starting the timer for measuring the Classification time start = time.time_ns()