mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
.vs/ directory, matrix_multiplication_addition file and binary tree directory (#894)
* Added print function into matrix_multiplication_addition.py and removed blank space in data_structures/binary tree directory * Removed .vs/ folder per #893 * Rename matrix_multiplication_addition.py to matrix_operation.py
This commit is contained in:
255
data_structures/binary_tree/AVL_tree.py
Normal file
255
data_structures/binary_tree/AVL_tree.py
Normal file
@ -0,0 +1,255 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
An auto-balanced binary tree!
|
||||
'''
|
||||
import math
|
||||
import random
|
||||
class my_queue:
|
||||
def __init__(self):
|
||||
self.data = []
|
||||
self.head = 0
|
||||
self.tail = 0
|
||||
def isEmpty(self):
|
||||
return self.head == self.tail
|
||||
def push(self,data):
|
||||
self.data.append(data)
|
||||
self.tail = self.tail + 1
|
||||
def pop(self):
|
||||
ret = self.data[self.head]
|
||||
self.head = self.head + 1
|
||||
return ret
|
||||
def count(self):
|
||||
return self.tail - self.head
|
||||
def print(self):
|
||||
print(self.data)
|
||||
print("**************")
|
||||
print(self.data[self.head:self.tail])
|
||||
|
||||
class my_node:
|
||||
def __init__(self,data):
|
||||
self.data = data
|
||||
self.left = None
|
||||
self.right = None
|
||||
self.height = 1
|
||||
def getdata(self):
|
||||
return self.data
|
||||
def getleft(self):
|
||||
return self.left
|
||||
def getright(self):
|
||||
return self.right
|
||||
def getheight(self):
|
||||
return self.height
|
||||
def setdata(self,data):
|
||||
self.data = data
|
||||
return
|
||||
def setleft(self,node):
|
||||
self.left = node
|
||||
return
|
||||
def setright(self,node):
|
||||
self.right = node
|
||||
return
|
||||
def setheight(self,height):
|
||||
self.height = height
|
||||
return
|
||||
|
||||
def getheight(node):
|
||||
if node is None:
|
||||
return 0
|
||||
return node.getheight()
|
||||
|
||||
def my_max(a,b):
|
||||
if a > b:
|
||||
return a
|
||||
return b
|
||||
|
||||
|
||||
|
||||
def leftrotation(node):
|
||||
r'''
|
||||
A B
|
||||
/ \ / \
|
||||
B C Bl A
|
||||
/ \ --> / / \
|
||||
Bl Br UB Br C
|
||||
/
|
||||
UB
|
||||
|
||||
UB = unbalanced node
|
||||
'''
|
||||
print("left rotation node:",node.getdata())
|
||||
ret = node.getleft()
|
||||
node.setleft(ret.getright())
|
||||
ret.setright(node)
|
||||
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
|
||||
node.setheight(h1)
|
||||
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
|
||||
ret.setheight(h2)
|
||||
return ret
|
||||
|
||||
def rightrotation(node):
|
||||
'''
|
||||
a mirror symmetry rotation of the leftrotation
|
||||
'''
|
||||
print("right rotation node:",node.getdata())
|
||||
ret = node.getright()
|
||||
node.setright(ret.getleft())
|
||||
ret.setleft(node)
|
||||
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
|
||||
node.setheight(h1)
|
||||
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
|
||||
ret.setheight(h2)
|
||||
return ret
|
||||
|
||||
def rlrotation(node):
|
||||
r'''
|
||||
A A Br
|
||||
/ \ / \ / \
|
||||
B C RR Br C LR B A
|
||||
/ \ --> / \ --> / / \
|
||||
Bl Br B UB Bl UB C
|
||||
\ /
|
||||
UB Bl
|
||||
RR = rightrotation LR = leftrotation
|
||||
'''
|
||||
node.setleft(rightrotation(node.getleft()))
|
||||
return leftrotation(node)
|
||||
|
||||
def lrrotation(node):
|
||||
node.setright(leftrotation(node.getright()))
|
||||
return rightrotation(node)
|
||||
|
||||
|
||||
def insert_node(node,data):
|
||||
if node is None:
|
||||
return my_node(data)
|
||||
if data < node.getdata():
|
||||
node.setleft(insert_node(node.getleft(),data))
|
||||
if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected
|
||||
if data < node.getleft().getdata(): #new node is the left child of the left child
|
||||
node = leftrotation(node)
|
||||
else:
|
||||
node = rlrotation(node) #new node is the right child of the left child
|
||||
else:
|
||||
node.setright(insert_node(node.getright(),data))
|
||||
if getheight(node.getright()) - getheight(node.getleft()) == 2:
|
||||
if data < node.getright().getdata():
|
||||
node = lrrotation(node)
|
||||
else:
|
||||
node = rightrotation(node)
|
||||
h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1
|
||||
node.setheight(h1)
|
||||
return node
|
||||
|
||||
def getRightMost(root):
|
||||
while root.getright() is not None:
|
||||
root = root.getright()
|
||||
return root.getdata()
|
||||
def getLeftMost(root):
|
||||
while root.getleft() is not None:
|
||||
root = root.getleft()
|
||||
return root.getdata()
|
||||
|
||||
def del_node(root,data):
|
||||
if root.getdata() == data:
|
||||
if root.getleft() is not None and root.getright() is not None:
|
||||
temp_data = getLeftMost(root.getright())
|
||||
root.setdata(temp_data)
|
||||
root.setright(del_node(root.getright(),temp_data))
|
||||
elif root.getleft() is not None:
|
||||
root = root.getleft()
|
||||
else:
|
||||
root = root.getright()
|
||||
elif root.getdata() > data:
|
||||
if root.getleft() is None:
|
||||
print("No such data")
|
||||
return root
|
||||
else:
|
||||
root.setleft(del_node(root.getleft(),data))
|
||||
elif root.getdata() < data:
|
||||
if root.getright() is None:
|
||||
return root
|
||||
else:
|
||||
root.setright(del_node(root.getright(),data))
|
||||
if root is None:
|
||||
return root
|
||||
if getheight(root.getright()) - getheight(root.getleft()) == 2:
|
||||
if getheight(root.getright().getright()) > getheight(root.getright().getleft()):
|
||||
root = rightrotation(root)
|
||||
else:
|
||||
root = lrrotation(root)
|
||||
elif getheight(root.getright()) - getheight(root.getleft()) == -2:
|
||||
if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()):
|
||||
root = leftrotation(root)
|
||||
else:
|
||||
root = rlrotation(root)
|
||||
height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1
|
||||
root.setheight(height)
|
||||
return root
|
||||
|
||||
class AVLtree:
|
||||
def __init__(self):
|
||||
self.root = None
|
||||
def getheight(self):
|
||||
# print("yyy")
|
||||
return getheight(self.root)
|
||||
def insert(self,data):
|
||||
print("insert:"+str(data))
|
||||
self.root = insert_node(self.root,data)
|
||||
|
||||
def del_node(self,data):
|
||||
print("delete:"+str(data))
|
||||
if self.root is None:
|
||||
print("Tree is empty!")
|
||||
return
|
||||
self.root = del_node(self.root,data)
|
||||
def traversale(self): #a level traversale, gives a more intuitive look on the tree
|
||||
q = my_queue()
|
||||
q.push(self.root)
|
||||
layer = self.getheight()
|
||||
if layer == 0:
|
||||
return
|
||||
cnt = 0
|
||||
while not q.isEmpty():
|
||||
node = q.pop()
|
||||
space = " "*int(math.pow(2,layer-1))
|
||||
print(space,end = "")
|
||||
if node is None:
|
||||
print("*",end = "")
|
||||
q.push(None)
|
||||
q.push(None)
|
||||
else:
|
||||
print(node.getdata(),end = "")
|
||||
q.push(node.getleft())
|
||||
q.push(node.getright())
|
||||
print(space,end = "")
|
||||
cnt = cnt + 1
|
||||
for i in range(100):
|
||||
if cnt == math.pow(2,i) - 1:
|
||||
layer = layer -1
|
||||
if layer == 0:
|
||||
print()
|
||||
print("*************************************")
|
||||
return
|
||||
print()
|
||||
break
|
||||
print()
|
||||
print("*************************************")
|
||||
return
|
||||
|
||||
def test(self):
|
||||
getheight(None)
|
||||
print("****")
|
||||
self.getheight()
|
||||
if __name__ == "__main__":
|
||||
t = AVLtree()
|
||||
t.traversale()
|
||||
l = list(range(10))
|
||||
random.shuffle(l)
|
||||
for i in l:
|
||||
t.insert(i)
|
||||
t.traversale()
|
||||
|
||||
random.shuffle(l)
|
||||
for i in l:
|
||||
t.del_node(i)
|
||||
t.traversale()
|
258
data_structures/binary_tree/binary_search_tree.py
Normal file
258
data_structures/binary_tree/binary_search_tree.py
Normal file
@ -0,0 +1,258 @@
|
||||
'''
|
||||
A binary search Tree
|
||||
'''
|
||||
from __future__ import print_function
|
||||
class Node:
|
||||
|
||||
def __init__(self, label, parent):
|
||||
self.label = label
|
||||
self.left = None
|
||||
self.right = None
|
||||
#Added in order to delete a node easier
|
||||
self.parent = parent
|
||||
|
||||
def getLabel(self):
|
||||
return self.label
|
||||
|
||||
def setLabel(self, label):
|
||||
self.label = label
|
||||
|
||||
def getLeft(self):
|
||||
return self.left
|
||||
|
||||
def setLeft(self, left):
|
||||
self.left = left
|
||||
|
||||
def getRight(self):
|
||||
return self.right
|
||||
|
||||
def setRight(self, right):
|
||||
self.right = right
|
||||
|
||||
def getParent(self):
|
||||
return self.parent
|
||||
|
||||
def setParent(self, parent):
|
||||
self.parent = parent
|
||||
|
||||
class BinarySearchTree:
|
||||
|
||||
def __init__(self):
|
||||
self.root = None
|
||||
|
||||
def insert(self, label):
|
||||
# Create a new Node
|
||||
new_node = Node(label, None)
|
||||
# If Tree is empty
|
||||
if self.empty():
|
||||
self.root = new_node
|
||||
else:
|
||||
#If Tree is not empty
|
||||
curr_node = self.root
|
||||
#While we don't get to a leaf
|
||||
while curr_node is not None:
|
||||
#We keep reference of the parent node
|
||||
parent_node = curr_node
|
||||
#If node label is less than current node
|
||||
if new_node.getLabel() < curr_node.getLabel():
|
||||
#We go left
|
||||
curr_node = curr_node.getLeft()
|
||||
else:
|
||||
#Else we go right
|
||||
curr_node = curr_node.getRight()
|
||||
#We insert the new node in a leaf
|
||||
if new_node.getLabel() < parent_node.getLabel():
|
||||
parent_node.setLeft(new_node)
|
||||
else:
|
||||
parent_node.setRight(new_node)
|
||||
#Set parent to the new node
|
||||
new_node.setParent(parent_node)
|
||||
|
||||
def delete(self, label):
|
||||
if (not self.empty()):
|
||||
#Look for the node with that label
|
||||
node = self.getNode(label)
|
||||
#If the node exists
|
||||
if(node is not None):
|
||||
#If it has no children
|
||||
if(node.getLeft() is None and node.getRight() is None):
|
||||
self.__reassignNodes(node, None)
|
||||
node = None
|
||||
#Has only right children
|
||||
elif(node.getLeft() is None and node.getRight() is not None):
|
||||
self.__reassignNodes(node, node.getRight())
|
||||
#Has only left children
|
||||
elif(node.getLeft() is not None and node.getRight() is None):
|
||||
self.__reassignNodes(node, node.getLeft())
|
||||
#Has two children
|
||||
else:
|
||||
#Gets the max value of the left branch
|
||||
tmpNode = self.getMax(node.getLeft())
|
||||
#Deletes the tmpNode
|
||||
self.delete(tmpNode.getLabel())
|
||||
#Assigns the value to the node to delete and keesp tree structure
|
||||
node.setLabel(tmpNode.getLabel())
|
||||
|
||||
def getNode(self, label):
|
||||
curr_node = None
|
||||
#If the tree is not empty
|
||||
if(not self.empty()):
|
||||
#Get tree root
|
||||
curr_node = self.getRoot()
|
||||
#While we don't find the node we look for
|
||||
#I am using lazy evaluation here to avoid NoneType Attribute error
|
||||
while curr_node is not None and curr_node.getLabel() is not label:
|
||||
#If node label is less than current node
|
||||
if label < curr_node.getLabel():
|
||||
#We go left
|
||||
curr_node = curr_node.getLeft()
|
||||
else:
|
||||
#Else we go right
|
||||
curr_node = curr_node.getRight()
|
||||
return curr_node
|
||||
|
||||
def getMax(self, root = None):
|
||||
if(root is not None):
|
||||
curr_node = root
|
||||
else:
|
||||
#We go deep on the right branch
|
||||
curr_node = self.getRoot()
|
||||
if(not self.empty()):
|
||||
while(curr_node.getRight() is not None):
|
||||
curr_node = curr_node.getRight()
|
||||
return curr_node
|
||||
|
||||
def getMin(self, root = None):
|
||||
if(root is not None):
|
||||
curr_node = root
|
||||
else:
|
||||
#We go deep on the left branch
|
||||
curr_node = self.getRoot()
|
||||
if(not self.empty()):
|
||||
curr_node = self.getRoot()
|
||||
while(curr_node.getLeft() is not None):
|
||||
curr_node = curr_node.getLeft()
|
||||
return curr_node
|
||||
|
||||
def empty(self):
|
||||
if self.root is None:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __InOrderTraversal(self, curr_node):
|
||||
nodeList = []
|
||||
if curr_node is not None:
|
||||
nodeList.insert(0, curr_node)
|
||||
nodeList = nodeList + self.__InOrderTraversal(curr_node.getLeft())
|
||||
nodeList = nodeList + self.__InOrderTraversal(curr_node.getRight())
|
||||
return nodeList
|
||||
|
||||
def getRoot(self):
|
||||
return self.root
|
||||
|
||||
def __isRightChildren(self, node):
|
||||
if(node == node.getParent().getRight()):
|
||||
return True
|
||||
return False
|
||||
|
||||
def __reassignNodes(self, node, newChildren):
|
||||
if(newChildren is not None):
|
||||
newChildren.setParent(node.getParent())
|
||||
if(node.getParent() is not None):
|
||||
#If it is the Right Children
|
||||
if(self.__isRightChildren(node)):
|
||||
node.getParent().setRight(newChildren)
|
||||
else:
|
||||
#Else it is the left children
|
||||
node.getParent().setLeft(newChildren)
|
||||
|
||||
#This function traversal the tree. By default it returns an
|
||||
#In order traversal list. You can pass a function to traversal
|
||||
#The tree as needed by client code
|
||||
def traversalTree(self, traversalFunction = None, root = None):
|
||||
if(traversalFunction is None):
|
||||
#Returns a list of nodes in preOrder by default
|
||||
return self.__InOrderTraversal(self.root)
|
||||
else:
|
||||
#Returns a list of nodes in the order that the users wants to
|
||||
return traversalFunction(self.root)
|
||||
|
||||
#Returns an string of all the nodes labels in the list
|
||||
#In Order Traversal
|
||||
def __str__(self):
|
||||
list = self.__InOrderTraversal(self.root)
|
||||
str = ""
|
||||
for x in list:
|
||||
str = str + " " + x.getLabel().__str__()
|
||||
return str
|
||||
|
||||
def InPreOrder(curr_node):
|
||||
nodeList = []
|
||||
if curr_node is not None:
|
||||
nodeList = nodeList + InPreOrder(curr_node.getLeft())
|
||||
nodeList.insert(0, curr_node.getLabel())
|
||||
nodeList = nodeList + InPreOrder(curr_node.getRight())
|
||||
return nodeList
|
||||
|
||||
def testBinarySearchTree():
|
||||
r'''
|
||||
Example
|
||||
8
|
||||
/ \
|
||||
3 10
|
||||
/ \ \
|
||||
1 6 14
|
||||
/ \ /
|
||||
4 7 13
|
||||
'''
|
||||
|
||||
r'''
|
||||
Example After Deletion
|
||||
7
|
||||
/ \
|
||||
1 4
|
||||
|
||||
'''
|
||||
t = BinarySearchTree()
|
||||
t.insert(8)
|
||||
t.insert(3)
|
||||
t.insert(6)
|
||||
t.insert(1)
|
||||
t.insert(10)
|
||||
t.insert(14)
|
||||
t.insert(13)
|
||||
t.insert(4)
|
||||
t.insert(7)
|
||||
|
||||
#Prints all the elements of the list in order traversal
|
||||
print(t.__str__())
|
||||
|
||||
if(t.getNode(6) is not None):
|
||||
print("The label 6 exists")
|
||||
else:
|
||||
print("The label 6 doesn't exist")
|
||||
|
||||
if(t.getNode(-1) is not None):
|
||||
print("The label -1 exists")
|
||||
else:
|
||||
print("The label -1 doesn't exist")
|
||||
|
||||
if(not t.empty()):
|
||||
print(("Max Value: ", t.getMax().getLabel()))
|
||||
print(("Min Value: ", t.getMin().getLabel()))
|
||||
|
||||
t.delete(13)
|
||||
t.delete(10)
|
||||
t.delete(8)
|
||||
t.delete(3)
|
||||
t.delete(6)
|
||||
t.delete(14)
|
||||
|
||||
#Gets all the elements of the tree In pre order
|
||||
#And it prints them
|
||||
list = t.traversalTree(InPreOrder, t.root)
|
||||
for x in list:
|
||||
print(x)
|
||||
|
||||
if __name__ == "__main__":
|
||||
testBinarySearchTree()
|
29
data_structures/binary_tree/fenwick_tree.py
Normal file
29
data_structures/binary_tree/fenwick_tree.py
Normal file
@ -0,0 +1,29 @@
|
||||
from __future__ import print_function
|
||||
class FenwickTree:
|
||||
|
||||
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
||||
self.Size = SIZE
|
||||
self.ft = [0 for i in range (0,SIZE)]
|
||||
|
||||
def update(self, i, val): # update data (adding) in index i in O(lg N)
|
||||
while (i < self.Size):
|
||||
self.ft[i] += val
|
||||
i += i & (-i)
|
||||
|
||||
def query(self, i): # query cumulative data from index 0 to i in O(lg N)
|
||||
ret = 0
|
||||
while (i > 0):
|
||||
ret += self.ft[i]
|
||||
i -= i & (-i)
|
||||
return ret
|
||||
|
||||
if __name__ == '__main__':
|
||||
f = FenwickTree(100)
|
||||
f.update(1,20)
|
||||
f.update(4,4)
|
||||
print (f.query(1))
|
||||
print (f.query(3))
|
||||
print (f.query(4))
|
||||
f.update(2,-5)
|
||||
print (f.query(1))
|
||||
print (f.query(3))
|
91
data_structures/binary_tree/lazy_segment_tree.py
Normal file
91
data_structures/binary_tree/lazy_segment_tree.py
Normal file
@ -0,0 +1,91 @@
|
||||
from __future__ import print_function
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
def __init__(self, N):
|
||||
self.N = N
|
||||
self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N
|
||||
self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update
|
||||
self.flag = [0 for i in range(0,4*N)] # flag for lazy update
|
||||
|
||||
def left(self, idx):
|
||||
return idx*2
|
||||
|
||||
def right(self, idx):
|
||||
return idx*2 + 1
|
||||
|
||||
def build(self, idx, l, r, A):
|
||||
if l==r:
|
||||
self.st[idx] = A[l-1]
|
||||
else :
|
||||
mid = (l+r)//2
|
||||
self.build(self.left(idx),l,mid, A)
|
||||
self.build(self.right(idx),mid+1,r, A)
|
||||
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])
|
||||
|
||||
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N) for each update)
|
||||
def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b]
|
||||
if self.flag[idx] == True:
|
||||
self.st[idx] = self.lazy[idx]
|
||||
self.flag[idx] = False
|
||||
if l!=r:
|
||||
self.lazy[self.left(idx)] = self.lazy[idx]
|
||||
self.lazy[self.right(idx)] = self.lazy[idx]
|
||||
self.flag[self.left(idx)] = True
|
||||
self.flag[self.right(idx)] = True
|
||||
|
||||
if r < a or l > b:
|
||||
return True
|
||||
if l >= a and r <= b :
|
||||
self.st[idx] = val
|
||||
if l!=r:
|
||||
self.lazy[self.left(idx)] = val
|
||||
self.lazy[self.right(idx)] = val
|
||||
self.flag[self.left(idx)] = True
|
||||
self.flag[self.right(idx)] = True
|
||||
return True
|
||||
mid = (l+r)//2
|
||||
self.update(self.left(idx),l,mid,a,b,val)
|
||||
self.update(self.right(idx),mid+1,r,a,b,val)
|
||||
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])
|
||||
return True
|
||||
|
||||
# query with O(lg N)
|
||||
def query(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b]
|
||||
if self.flag[idx] == True:
|
||||
self.st[idx] = self.lazy[idx]
|
||||
self.flag[idx] = False
|
||||
if l != r:
|
||||
self.lazy[self.left(idx)] = self.lazy[idx]
|
||||
self.lazy[self.right(idx)] = self.lazy[idx]
|
||||
self.flag[self.left(idx)] = True
|
||||
self.flag[self.right(idx)] = True
|
||||
if r < a or l > b:
|
||||
return -math.inf
|
||||
if l >= a and r <= b:
|
||||
return self.st[idx]
|
||||
mid = (l+r)//2
|
||||
q1 = self.query(self.left(idx),l,mid,a,b)
|
||||
q2 = self.query(self.right(idx),mid+1,r,a,b)
|
||||
return max(q1,q2)
|
||||
|
||||
def showData(self):
|
||||
showList = []
|
||||
for i in range(1,N+1):
|
||||
showList += [self.query(1, 1, self.N, i, i)]
|
||||
print (showList)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
|
||||
N = 15
|
||||
segt = SegmentTree(N)
|
||||
segt.build(1,1,N,A)
|
||||
print (segt.query(1,1,N,4,6))
|
||||
print (segt.query(1,1,N,7,11))
|
||||
print (segt.query(1,1,N,7,12))
|
||||
segt.update(1,1,N,1,3,111)
|
||||
print (segt.query(1,1,N,1,15))
|
||||
segt.update(1,1,N,7,8,235)
|
||||
segt.showData()
|
71
data_structures/binary_tree/segment_tree.py
Normal file
71
data_structures/binary_tree/segment_tree.py
Normal file
@ -0,0 +1,71 @@
|
||||
from __future__ import print_function
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
def __init__(self, A):
|
||||
self.N = len(A)
|
||||
self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N
|
||||
self.build(1, 0, self.N - 1)
|
||||
|
||||
def left(self, idx):
|
||||
return idx * 2
|
||||
|
||||
def right(self, idx):
|
||||
return idx * 2 + 1
|
||||
|
||||
def build(self, idx, l, r):
|
||||
if l == r:
|
||||
self.st[idx] = A[l]
|
||||
else:
|
||||
mid = (l + r) // 2
|
||||
self.build(self.left(idx), l, mid)
|
||||
self.build(self.right(idx), mid + 1, r)
|
||||
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])
|
||||
|
||||
def update(self, a, b, val):
|
||||
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
|
||||
|
||||
def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b]
|
||||
if r < a or l > b:
|
||||
return True
|
||||
if l == r :
|
||||
self.st[idx] = val
|
||||
return True
|
||||
mid = (l+r)//2
|
||||
self.update_recursive(self.left(idx), l, mid, a, b, val)
|
||||
self.update_recursive(self.right(idx), mid+1, r, a, b, val)
|
||||
self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)])
|
||||
return True
|
||||
|
||||
def query(self, a, b):
|
||||
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
|
||||
|
||||
def query_recursive(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b]
|
||||
if r < a or l > b:
|
||||
return -math.inf
|
||||
if l >= a and r <= b:
|
||||
return self.st[idx]
|
||||
mid = (l+r)//2
|
||||
q1 = self.query_recursive(self.left(idx), l, mid, a, b)
|
||||
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
|
||||
return max(q1, q2)
|
||||
|
||||
def showData(self):
|
||||
showList = []
|
||||
for i in range(1,N+1):
|
||||
showList += [self.query(i, i)]
|
||||
print (showList)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8]
|
||||
N = 15
|
||||
segt = SegmentTree(A)
|
||||
print (segt.query(4, 6))
|
||||
print (segt.query(7, 11))
|
||||
print (segt.query(7, 12))
|
||||
segt.update(1,3,111)
|
||||
print (segt.query(1, 15))
|
||||
segt.update(7,8,235)
|
||||
segt.showData()
|
129
data_structures/binary_tree/treap.py
Normal file
129
data_structures/binary_tree/treap.py
Normal file
@ -0,0 +1,129 @@
|
||||
from random import random
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Node:
|
||||
"""
|
||||
Treap's node
|
||||
Treap is a binary tree by key and heap by priority
|
||||
"""
|
||||
def __init__(self, key: int):
|
||||
self.key = key
|
||||
self.prior = random()
|
||||
self.l = None
|
||||
self.r = None
|
||||
|
||||
|
||||
def split(root: Node, key: int) -> Tuple[Node, Node]:
|
||||
"""
|
||||
We split current tree into 2 trees with key:
|
||||
|
||||
Left tree contains all keys less than split key.
|
||||
Right tree contains all keys greater or equal, than split key
|
||||
"""
|
||||
if root is None: # None tree is split into 2 Nones
|
||||
return (None, None)
|
||||
if root.key >= key:
|
||||
"""
|
||||
Right tree's root will be current node.
|
||||
Now we split(with the same key) current node's left son
|
||||
Left tree: left part of that split
|
||||
Right tree's left son: right part of that split
|
||||
"""
|
||||
l, root.l = split(root.l, key)
|
||||
return (l, root)
|
||||
else:
|
||||
"""
|
||||
Just symmetric to previous case
|
||||
"""
|
||||
root.r, r = split(root.r, key)
|
||||
return (root, r)
|
||||
|
||||
|
||||
def merge(left: Node, right: Node) -> Node:
|
||||
"""
|
||||
We merge 2 trees into one.
|
||||
Note: all left tree's keys must be less than all right tree's
|
||||
"""
|
||||
if (not left) or (not right):
|
||||
"""
|
||||
If one node is None, return the other
|
||||
"""
|
||||
return left or right
|
||||
if left.key > right.key:
|
||||
"""
|
||||
Left will be root because it has more priority
|
||||
Now we need to merge left's right son and right tree
|
||||
"""
|
||||
left.r = merge(left.r, right)
|
||||
return left
|
||||
else:
|
||||
"""
|
||||
Symmetric as well
|
||||
"""
|
||||
right.l = merge(left, right.l)
|
||||
return right
|
||||
|
||||
|
||||
def insert(root: Node, key: int) -> Node:
|
||||
"""
|
||||
Insert element
|
||||
|
||||
Split current tree with a key into l, r,
|
||||
Insert new node into the middle
|
||||
Merge l, node, r into root
|
||||
"""
|
||||
node = Node(key)
|
||||
l, r = split(root, key)
|
||||
root = merge(l, node)
|
||||
root = merge(root, r)
|
||||
return root
|
||||
|
||||
|
||||
def erase(root: Node, key: int) -> Node:
|
||||
"""
|
||||
Erase element
|
||||
|
||||
Split all nodes with keys less into l,
|
||||
Split all nodes with keys greater into r.
|
||||
Merge l, r
|
||||
"""
|
||||
l, r = split(root, key)
|
||||
_, r = split(r, key + 1)
|
||||
return merge(l, r)
|
||||
|
||||
|
||||
def node_print(root: Node):
|
||||
"""
|
||||
Just recursive print of a tree
|
||||
"""
|
||||
if not root:
|
||||
return
|
||||
node_print(root.l)
|
||||
print(root.key, end=" ")
|
||||
node_print(root.r)
|
||||
|
||||
|
||||
def interactTreap():
|
||||
"""
|
||||
Commands:
|
||||
+ key to add key into treap
|
||||
- key to erase all nodes with key
|
||||
|
||||
After each command, program prints treap
|
||||
"""
|
||||
root = None
|
||||
while True:
|
||||
cmd = input().split()
|
||||
cmd[1] = int(cmd[1])
|
||||
if cmd[0] == "+":
|
||||
root = insert(root, cmd[1])
|
||||
elif cmd[0] == "-":
|
||||
root = erase(root, cmd[1])
|
||||
else:
|
||||
print("Unknown command")
|
||||
node_print(root)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
interactTreap()
|
Reference in New Issue
Block a user