rishiilluri

Untitled

Nov 17th, 2022
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. # problem 4
  2. class Wavelet_Tree:
  3.     def __init__(self , A, depth =0):
  4.         self.A = A
  5.         self.B = []
  6.         self.left = None
  7.         self.right = None
  8.         self.depth = depth
  9.         self.p = 0
  10.         self.construct()
  11. #         print(self.A, self.B)
  12.        
  13.     def construct(self):
  14.         if len(self.A)<=1:
  15.             return None
  16.        
  17.         small = self.A[0]
  18.         large = self.A[0]
  19.         for i in self.A:
  20.             if i < small:
  21.                 small = i
  22.             if i > large:
  23.                 large = i
  24.  
  25.         p = (large-small)//2
  26.         p += small
  27.         self.p = p
  28.        
  29.         left = []
  30.         right = []
  31.         self.B = [0 for i in self.A]
  32.         for i in range(len(self.A)):
  33.             if self.A[i]<=p:
  34.                 left.append(self.A[i])
  35.                 if i-1>=0:
  36.                     self.B[i] = self.B[i-1]+1
  37.                 else:
  38.                     self.B[0] = 1
  39.             else:
  40.                 right.append(self.A[i])
  41.                 self.B[i] = self.B[i-1]
  42.         self.left = Wavelet_Tree(left, self.depth+1)
  43.         self.right = Wavelet_Tree(right, self.depth+1)
  44.        
  45.        
  46.     def print(self):
  47.         q = []
  48.         q.append(self)
  49.         count = 0
  50.         while q:
  51.             print("Level "+str(count)+":", end = " ")
  52.             size = len(q)
  53.             for _ in range(size):
  54.                 x = q.pop(0)
  55.                 r = ''
  56.                 for i in x.A:
  57.                     if i<=x.p:
  58.                         r+='0'
  59.                     else:
  60.                         r+='1'
  61.                 if len(r)==1:
  62.                     print("x", end = " ")
  63.                 else:
  64.                     print(r, end = " ")
  65.                 if x.left:
  66.                     q.append(x.left)
  67.  
  68.                 if x.right:
  69.                     q.append(x.right)
  70.  
  71.             print()
  72.             count+=1
  73.            
  74.     def RQQ(self, k:int , left:int , right:int):
  75.         print("Level "+ str(self.depth)+":",k, left, right)
  76.         if left == right:
  77.             return self.A[0]
  78.        
  79.         x1 = self.B[right-1]
  80.         x2 = 0
  81.         if left-2>=0:
  82.             x2 = self.B[left-2]
  83.         left_c = x1-x2
  84. #         print(k, left, right, self.A, self.B,x1,x2, left_c)
  85.         if k <= left_c:
  86.             return self.left.RQQ(k, x2+1, x1)
  87.        
  88.         return self.right.RQQ(k-left_c, left-x2, right-x1)
  89.            
  90.  
  91. wv_tree = Wavelet_Tree([6, 2, 0, 7, 9, 3, 1, 8, 5, 4])
  92. wv_tree.print()
  93. print()
  94. print()
  95. wv_tree.RQQ(5, 3, 9)
  96.    
  97.  
  98.        
  99.  
Advertisement
Add Comment
Please, Sign In to add comment