def heightrandom(): for i in range(2, 10, 2): L = [100 * random() for x in range((2**i) - 1)] BSTTree = BST() TreapTree = Treap() for x in L: BSTTree.insert(x) TreapTree.insert(x) print("Complete Tree would have height of", i - 1) print("Height of BST is: ", BSTTree.height()) print("Height of Treap is: ", TreapTree.height())
def solve(n, shifted): arr = [0 for _ in range(n)] #1~N까지의 숫자를 모두 저장하는 트립을 만든다. candidates = Treap() for i in range(n): candidates.insert(i + 1) #뒤에서부터 arr[]를 채워나간다. for i in range(n - 1, -1, -1): #후보 중 이 수보다 큰 수가 larger개 있다. larger = shifted[i] k = candidates.findKth(i + 1 - larger) arr[i] = k.key candidates.delete(k.key) return arr
class MedianFinder: def __init__(self): self.treap = Treap() self.histogram = {} def addItem(self, item): if item > 0: self.treap.insert(item) self.histogram[item] = self.histogram.get(item, 0) + 1 def delItem(self, item): if item > 0: self.treap.delete(item) self.histogram[item] -= 1 def updateItem(self, item, delta): self.delItem(item) self.addItem(item+delta) def findMedian(self): return self.treap.median()
def test_removeMoreThanOneOccurence(): t = Treap() t.insert("A") t.insert("A") t.remove("A") assert t.getOccurenceCount("A") == 1
def test_removeWithOneOccurence(): t = Treap() t.insert("A") t.remove("A") assert t.find("A") == False
def test_testOccurenceCountMany(): t = Treap() t.insert("A") t.insert("A") t.insert("A") assert t.getOccurenceCount("A") == 3
def test_findKeyNotThere(): t = Treap() t.insert("A") assert t.find("B") == False
def test_findKeyThere(): t = Treap() t.insert("A") assert t.find("A")
def test_insertOne(): t = Treap() assert t.insert("A")
def get_nodes_from_vert(vert): nodes = [] for node in vert.content: nodes.append(node) return nodes nodes = [10, 13, 35, 64, 92, 1231, 1234, 5, 6, 45, 56, 67, 78, 89, 90] keys = [1, 1, 5, 2, 3, 4, 6, 4, 10, 5, 6, 7, 8, 9, 10] root_node = 0 root_key = 0 # Constructor test tree = Treap() tree.insert(root_node, root_key) # insert function test for i in range(len(nodes)): tree.insert(nodes[i], keys[i]) tree.print_tree() # locate test print(tree.locate(10).content) # Single match print(tree.locate(1).content) # Multiple matches print('\n') # TreapVertex test print(tree.root) key_5 = tree.locate(5) print(key_5)