def test_init_with_list_of_tuples(self): tree = AVLTree([(2, 'B'), (1, 'A'), (3, 'C')]) assert tree.root.data == (2, 'B') assert tree.root.left.data == (1, 'A') assert tree.root.right.data == (3, 'C') assert tree.size == 3 assert tree.is_empty() is False
def __init__(self): self.price_tree = AVLTree() self.price_map = {} self.order_map = {} self.volume = 0 self.num_orders = 0 self.depth = 0
def test_init_with_list(self): tree = AVLTree([2, 1, 3]) assert tree.root.data == 2 assert tree.root.left.data == 1 assert tree.root.right.data == 3 assert tree.size == 3 assert tree.is_empty() is False
def test_init_with_list_of_strings(self): tree = AVLTree(['B', 'A', 'C']) assert tree.root.data == 'B' assert tree.root.left.data == 'A' assert tree.root.right.data == 'C' assert tree.size == 3 assert tree.is_empty() is False
def avl_insert(string): start = time.clock() avl = AVLTree() for i in range(num): avl.insert(i) end = time.clock() print string + str(end - start)
def test_insert_with_7_items(self): # Create a complete binary search tree of 7 items in level-order items = [4, 2, 6, 1, 3, 5, 7] tree = AVLTree() for item in items: tree.insert(item) assert tree.root.data == 4 assert tree.root.left.data == 2 assert tree.root.right.data == 6 assert tree.root.left.left.data == 1 assert tree.root.left.right.data == 3 assert tree.root.right.left.data == 5 assert tree.root.right.right.data == 7
def test_size(self): tree = AVLTree() assert tree.size == 0 tree.insert('B') assert tree.size == 1 tree.insert('A') assert tree.size == 2 tree.insert('C') assert tree.size == 3
class TestAVLTree(unittest.TestCase): def setUp(self): self.avl = None def _initialize(self): self._cleanup() self.avl = AVLTree() lst = [5, 8, 2, 1, 3, 9, 7, 4, 6, 0] for i in lst: self.avl.insert(i) # verify length and that root node is 5 self.assertEqual(len(self.avl), 10) self.assertEqual(self.avl.root.value, 5) def _cleanup(self): if self.avl: del(self.avl) self.avl = None def test_insert(self): self._initialize() self._cleanup() def test_delete(self): self._initialize() self.avl.delete(5) # length should be 9 and root should be 6 (instead of 5) self.assertEqual(len(self.avl), 9) self.assertEqual(self.avl.root.value, 6) self._cleanup() def test_iteration(self): self._initialize() lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for i in self.avl.root: self.assertTrue(i == lst[i]) self._cleanup() def tearDown(self): self._cleanup()
def _initialize(self): self._cleanup() self.avl = AVLTree() lst = [5, 8, 2, 1, 3, 9, 7, 4, 6, 0] for i in lst: self.avl.insert(i) # verify length and that root node is 5 self.assertEqual(len(self.avl), 10) self.assertEqual(self.avl.root.value, 5)
class AVLRangeIndex(object): """AVL Tree-based range index implementation.""" def __init__(self): self.data = AVLTree() def add(self, key): if key is None: raise ValueError('Cannot insert nil in the index') self.data.insert(key) def remove(self, key): self.data.delete(key) def list(self, first_key, last_key): return self.data.as_list(first_key, last_key) def count(self, first_key, last_key): result = len(self.as_list(first_key, last_key)) return result
def test_insert_with_3_items(self): # Create a complete binary search tree of 3 items in level-order tree = AVLTree() tree.insert(2) assert tree.root.data == 2 assert tree.root.left is None assert tree.root.right is None tree.insert(1) assert tree.root.data == 2 assert tree.root.left.data == 1 assert tree.root.right is None tree.insert(3) assert tree.root.data == 2 assert tree.root.left.data == 1 assert tree.root.right.data == 3
def test_delitem(self): # CASE 1: LEAF - no cascading deletion, left rotation a = AVLTree() a[20] = 20 a[10] = 10 a[30] = 30 a[5] = 5 a[15] = 15 a[29] = 29 a[40] = 40 a[8] = 8 a[28] = 28 a[35] = 35 a[50] = 50 a[60] = 60 self.assertEqual(a.verbose_string(), 'AVL{5:5:-1, 8:8:0, 10:10:1, 15:15:0, 20:20:-1, 28:28:0, 29:29:1, 30:30:-1, 35:35:0, 40:40:-1, 50:50:-1, 60:60:0, }') del a[35] self.assertEqual(a.verbose_string(), 'AVL{5:5:-1, 8:8:0, 10:10:1, 15:15:0, 20:20:0, 28:28:0, 29:29:1, 30:30:0, 40:40:0, 50:50:0, 60:60:0, }') # CASE 2: LEAF - no cascading deletion, right left rotation # CASE 3: LEAF - cascading deletion, left following by right predecessor rotation a = AVLTree() a[20] = 20 a[10] = 10 a[30] = 30 a[5] = 5 a[15] = 15 a[29] = 29 a[40] = 40 a[3] = 3 a[8] = 8 a[13] = 13 a[18] = 18 a[28] = 28 a[35] = 35 a[50] = 50 a[2] = 2 a[9] = 9 a[12] = 12 a[14] = 14 a[19] = 19 a[60] = 60 a[11] = 11 self.assertEqual(a.verbose_string(), 'AVL{2:2:0, 3:3:1, 5:5:0, 8:8:-1, 9:9:0, 10:10:-1, 11:11:0, 12:12:1, 13:13:1, 14:14:0, 15:15:1, 18:18:-1, 19:19:0, 20:20:1, 28:28:0, 29:29:1, 30:30:-1, 35:35:0, 40:40:-1, 50:50:-1, 60:60:0, }') del a[35] self.assertEqual(a.verbose_string(), 'AVL{2:2:0, 3:3:1, 5:5:0, 8:8:-1, 9:9:0, 10:10:0, 11:11:0, 12:12:1, 13:13:1, 14:14:0, 15:15:0, 18:18:-1, 19:19:0, 20:20:-1, 28:28:0, 29:29:1, 30:30:0, 40:40:0, 50:50:0, 60:60:0, }')
def avl_search(string): avl = AVLTree() for i in range(num): avl.insert(i) start = time.clock() for i in range(num): avl.find(i) end = time.clock() print string + str(end - start)
def initialize(self, queue_length, update_time): ''' **THIS IS A PRIVATE METHOD.**\n You should NOT USE IT in most conditions. ''' self.queue_length = queue_length self.update_time = update_time self.pointer = 0 self.loop_counter = 0 # it is not running now self.stoped = True # create a list with an AVL tree in each node self.circular_queue = [ AVLTree(), ] * queue_length
def run(self): ''' **THIS IS A PRIVATE METHOD.**\n You should NOT USE IT in most conditions ''' if self.stoped: return None try: # schedules the next update threading.Timer(self.update_time, self.run) # deletes all the time out elements del (self.circular_queue[self.pointer]) self.circular_queue.insert(AVLTree()) # moves the pointer forward pointer = (pointer + 1) % self.queue_length except Exception as e: print(e) pass
def run_case(request_queue, response_queue): while True: request = request_queue.get() id = hash(request) input = list(request.input) rinput = [] v = None try: operations = get_hybrid_operations(input) tree = RedBlackTree( ) if request.clazz == "RedBlackTree" else AVLTree() for o in operations: if o.inserting: successful = tree.put(o.key) assert successful assert o.key in tree else: assert o.key in tree del tree[o.key] assert o.key not in tree for v in input: successful = tree.put(v) assert successful assert v in tree assert tree.valid() rinput = input[:] random.shuffle(rinput) for v in rinput: assert v in tree del tree[v] assert v not in tree assert tree.valid() r = Response(id, True) response_queue.put(r) except Exception as e: r = Response(id, False, input, rinput, v, e) response_queue.put(r)
from avltree import AVLTree from draw import plot_tree import pickle import time from Heap import Heap if __name__ == '__main__': tr = AVLTree().load_tree() plot_tree(tr)
for word in total: if word not in bst_map: bst_map[word] = 1 else: bst_map[word] += 1 for word in total: word in bst_map end = time.time() - start print('Total different words:', len(bst_map)) print('Frequency of pride:', bst_map['pride']) print('Frequency of prejudice:', bst_map['prejudice']) print('bst-tree map: {:.6f}'.format(end)) avl_map = AVLTree() print() print('Total words:', len(total)) start = time.time() for word in total: if word not in avl_map: avl_map[word] = 1 else: avl_map[word] += 1 for word in total: avl_map.contains(word) end = time.time() - start
class OrderTree(object): def __init__(self): self.price_tree = AVLTree() self.price_map = {} self.order_map = {} self.volume = 0 self.num_orders = 0 self.depth = 0 def __len__(self): return len(self.order_map) def get_price_list(self, price): return self.price_map[price] def get_order(self, order_id): return self.order_map[order_id] def create_price(self, price): self.depth += 1 new_list = OrderList() self.price_tree.insert(price, new_list) self.price_map[price] = new_list def remove_price(self, price): self.depth -= 1 self.price_tree.remove(price) del self.price_map[price] def price_exists(self, price): return price in self.price_map def order_exist(self, order): return order in self.order_map def insert_order(self, quote): if self.order_exist(quote['order_id']): self.remove_order_by_order_id(quote['order_id']) self.num_orders += 1 if quote['price'] not in self.price_map: self.create_price(quote['price']) order = Order(quote, self.price_map[quote['price']]) self.price_map[order.price].append_order(order) self.order_map[order.order_id] = order self.volume += order.quantity def remove_order_by_order_id(self, order_id): self.num_orders -= 1 order = self.order_map[order_id] self.volume -= order.quantity order.order_list.remove_order(order) if len(order.order_list) == 0: self.remove_price(order.price) del self.order_map[order_id] def max_price(self): if self.depth > 0: return self.price_tree.max_key() else: return None def min_price(self): if self.depth > 0: return self.price_tree.min_key() else: return None def max_price_list(self): if self.depth > 0: return self.get_price_list(self.max_price()) else: return None def min_price_list(self): if self.depth > 0: return self.get_price_list(self.min_price()) else: return None
def test_init(self): tree = AVLTree() assert tree.root is None assert tree.size == 0 assert tree.is_empty() is True
def test_items_level_order_with_7_numbers(self): # Create a complete binary search tree of 7 items in level-order items = [4, 2, 6, 1, 3, 5, 7] tree = AVLTree(items) # Ensure the level-order traversal of tree items is ordered correctly assert tree.items_level_order() == [4, 2, 6, 1, 3, 5, 7]
def test_items_level_order_with_3_strings(self): # Create a complete binary search tree of 3 strings in level-order items = ['B', 'A', 'C'] tree = AVLTree(items) # Ensure the level-order traversal of tree items is ordered correctly assert tree.items_level_order() == ['B', 'A', 'C']
def test_create_empty_AVLTree(self): a = AVLTree() self.assertEqual(str(a), 'AVL{}')
def test_setitem(self): # CASE 1: into empty tree a = AVLTree() a[10] = 10 self.assertEqual(a.verbose_string(), 'AVL{10:10:0, }') # CASE 2: Root rotation - left a = AVLTree() a[10] = 10 a[5] = 5 a[15] = 15 a[13] = 13 a[20] = 20 self.assertEqual(a.verbose_string(), 'AVL{5:5:0, 10:10:-1, 13:13:0, 15:15:0, 20:20:0, }') a[25] = 25 self.assertEqual(a.verbose_string(), 'AVL{5:5:0, 10:10:0, 13:13:0, 15:15:0, 20:20:-1, 25:25:0, }') # CASE 3: Root rotation - right a = AVLTree() a[10] = 10 a[5] = 5 a[15] = 15 a[3] = 3 a[8] = 8 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:1, 15:15:0, }') a[2] = 2 self.assertEqual(a.verbose_string(), 'AVL{2:2:0, 3:3:1, 5:5:0, 8:8:0, 10:10:0, 15:15:0, }') # CASE 4: Root rotation - right left a = AVLTree() a[10] = 10 a[15] = 15 self.assertEqual(a.verbose_string(), 'AVL{10:10:-1, 15:15:0, }') a[13] = 13 self.assertEqual(a.verbose_string(), 'AVL{10:10:0, 13:13:0, 15:15:0, }') # CASE 5: Root rotation - left right a = AVLTree() a[10] = 10 a[5] = 5 self.assertEqual(a.verbose_string(), 'AVL{5:5:0, 10:10:1, }') a[8] = 8 self.assertEqual(a.verbose_string(), 'AVL{5:5:0, 8:8:0, 10:10:0, }') # CASE 6: Non-Root rotation - left a = AVLTree() a[10] = 10 a[5] = 5 a[3] = 3 a[8] = 8 a[15] = 15 a[13] = 13 a[20] = 20 a[18] = 18 a[25] = 25 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 13:13:0, 15:15:-1, 18:18:0, 20:20:0, 25:25:0, }') a[30] = 30 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 13:13:0, 15:15:0, 18:18:0, 20:20:0, 25:25:-1, 30:30:0, }') # CASE 7: Non-Root rotation - right a = AVLTree() a[10] = 10 a[5] = 5 a[3] = 3 a[8] = 8 a[15] = 15 a[13] = 13 a[18] = 18 a[12] = 12 a[14] = 14 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 12:12:0, 13:13:0, 14:14:0, 15:15:1, 18:18:0, }') a[11] = 11 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 11:11:0, 12:12:1, 13:13:0, 14:14:0, 15:15:0, 18:18:0, }') # CASE 8: Non-Root rotation - right left a = AVLTree() a[10] = 10 a[5] = 5 a[3] = 3 a[8] = 8 a[15] = 15 a[13] = 13 a[20] = 20 a[18] = 18 a[25] = 25 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 13:13:0, 15:15:-1, 18:18:0, 20:20:0, 25:25:0, }') a[17] = 17 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 13:13:0, 15:15:0, 17:17:0, 18:18:0, 20:20:-1, 25:25:0, }') # CASE 9: Non-Root rotation - left right a = AVLTree() a[10] = 10 a[5] = 5 a[3] = 3 a[8] = 8 a[16] = 16 a[13] = 13 a[18] = 18 a[12] = 12 a[14] = 14 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 12:12:0, 13:13:0, 14:14:0, 16:16:1, 18:18:0, }') a[15] = 15 self.assertEqual(a.verbose_string(), 'AVL{3:3:0, 5:5:0, 8:8:0, 10:10:-1, 12:12:0, 13:13:1, 14:14:0, 15:15:0, 16:16:0, 18:18:0, }')
from avltree import AVLTree def get_r_parent(btn): # right parent, i.e. cur node should be a left child while btn.parent and btn.parent.r == btn: btn = btn.parent return btn.parent def find_next(btn): if not btn: return None if btn.r: next = btn.r while next.l: next = next.l else: next = get_r_parent(btn) return next if __name__ == '__main__': t = AVLTree([1, 2, 3, 4, 5, 6, 7]) n = t.root while n.l: n = n.l while n: print(n.key, end=',') n = find_next(n)
from avltree import AVLTree #from binarytree import BinaryTree tree = AVLTree() #tree = BinaryTree() tree.insert(1) tree.insert(2) tree.insert(3) tree.insert(4) tree.insert(5) tree.insert(6) tree.insert(7) tree.insert(8) tree.preorder() #tree.postorder()
def __init__(self): self.data = AVLTree()