mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -1,71 +1,88 @@
|
||||
# -*- 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):
|
||||
|
||||
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])
|
||||
|
||||
print(self.data[self.head : self.tail])
|
||||
|
||||
|
||||
class my_node:
|
||||
def __init__(self,data):
|
||||
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):
|
||||
|
||||
def setdata(self, data):
|
||||
self.data = data
|
||||
return
|
||||
def setleft(self,node):
|
||||
|
||||
def setleft(self, node):
|
||||
self.left = node
|
||||
return
|
||||
def setright(self,node):
|
||||
|
||||
def setright(self, node):
|
||||
self.right = node
|
||||
return
|
||||
def setheight(self,height):
|
||||
|
||||
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):
|
||||
|
||||
def my_max(a, b):
|
||||
if a > b:
|
||||
return a
|
||||
return b
|
||||
|
||||
|
||||
|
||||
def leftrotation(node):
|
||||
r'''
|
||||
r"""
|
||||
A B
|
||||
/ \ / \
|
||||
B C Bl A
|
||||
@ -75,33 +92,35 @@ def leftrotation(node):
|
||||
UB
|
||||
|
||||
UB = unbalanced node
|
||||
'''
|
||||
print("left rotation node:",node.getdata())
|
||||
"""
|
||||
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
|
||||
h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1
|
||||
node.setheight(h1)
|
||||
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
|
||||
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())
|
||||
"""
|
||||
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
|
||||
h1 = my_max(getheight(node.getright()), getheight(node.getleft())) + 1
|
||||
node.setheight(h1)
|
||||
h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1
|
||||
h2 = my_max(getheight(ret.getright()), getheight(ret.getleft())) + 1
|
||||
ret.setheight(h2)
|
||||
return ret
|
||||
|
||||
|
||||
def rlrotation(node):
|
||||
r'''
|
||||
r"""
|
||||
A A Br
|
||||
/ \ / \ / \
|
||||
B C RR Br C LR B A
|
||||
@ -110,51 +129,60 @@ def rlrotation(node):
|
||||
\ /
|
||||
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):
|
||||
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.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
|
||||
node = rlrotation(node) # new node is the right child of the left child
|
||||
else:
|
||||
node.setright(insert_node(node.getright(),data))
|
||||
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
|
||||
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):
|
||||
|
||||
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))
|
||||
root.setright(del_node(root.getright(), temp_data))
|
||||
elif root.getleft() is not None:
|
||||
root = root.getleft()
|
||||
else:
|
||||
@ -164,12 +192,12 @@ def del_node(root,data):
|
||||
print("No such data")
|
||||
return root
|
||||
else:
|
||||
root.setleft(del_node(root.getleft(),data))
|
||||
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))
|
||||
root.setright(del_node(root.getright(), data))
|
||||
if root is None:
|
||||
return root
|
||||
if getheight(root.getright()) - getheight(root.getleft()) == 2:
|
||||
@ -182,27 +210,31 @@ def del_node(root,data):
|
||||
root = leftrotation(root)
|
||||
else:
|
||||
root = rlrotation(root)
|
||||
height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1
|
||||
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")
|
||||
# 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))
|
||||
|
||||
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
|
||||
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()
|
||||
@ -211,21 +243,21 @@ class AVLtree:
|
||||
cnt = 0
|
||||
while not q.isEmpty():
|
||||
node = q.pop()
|
||||
space = " "*int(math.pow(2,layer-1))
|
||||
print(space,end = "")
|
||||
space = " " * int(math.pow(2, layer - 1))
|
||||
print(space, end="")
|
||||
if node is None:
|
||||
print("*",end = "")
|
||||
print("*", end="")
|
||||
q.push(None)
|
||||
q.push(None)
|
||||
else:
|
||||
print(node.getdata(),end = "")
|
||||
print(node.getdata(), end="")
|
||||
q.push(node.getleft())
|
||||
q.push(node.getright())
|
||||
print(space,end = "")
|
||||
print(space, end="")
|
||||
cnt = cnt + 1
|
||||
for i in range(100):
|
||||
if cnt == math.pow(2,i) - 1:
|
||||
layer = layer -1
|
||||
if cnt == math.pow(2, i) - 1:
|
||||
layer = layer - 1
|
||||
if layer == 0:
|
||||
print()
|
||||
print("*************************************")
|
||||
@ -235,11 +267,13 @@ class AVLtree:
|
||||
print()
|
||||
print("*************************************")
|
||||
return
|
||||
|
||||
|
||||
def test(self):
|
||||
getheight(None)
|
||||
print("****")
|
||||
self.getheight()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
t = AVLtree()
|
||||
t.traversale()
|
||||
@ -248,7 +282,7 @@ if __name__ == "__main__":
|
||||
for i in l:
|
||||
t.insert(i)
|
||||
t.traversale()
|
||||
|
||||
|
||||
random.shuffle(l)
|
||||
for i in l:
|
||||
t.del_node(i)
|
||||
|
@ -1,12 +1,13 @@
|
||||
class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers.
|
||||
class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers.
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
def display(tree): #In Order traversal of the tree
|
||||
|
||||
if tree is None:
|
||||
def display(tree): # In Order traversal of the tree
|
||||
|
||||
if tree is None:
|
||||
return
|
||||
|
||||
if tree.left is not None:
|
||||
@ -19,7 +20,10 @@ def display(tree): #In Order traversal of the tree
|
||||
|
||||
return
|
||||
|
||||
def depth_of_tree(tree): #This is the recursive function to find the depth of binary tree.
|
||||
|
||||
def depth_of_tree(
|
||||
tree
|
||||
): # This is the recursive function to find the depth of binary tree.
|
||||
if tree is None:
|
||||
return 0
|
||||
else:
|
||||
@ -31,18 +35,20 @@ def depth_of_tree(tree): #This is the recursive function to find the depth of bi
|
||||
return 1 + depth_r_tree
|
||||
|
||||
|
||||
def is_full_binary_tree(tree): # This functions returns that is it full binary tree or not?
|
||||
def is_full_binary_tree(
|
||||
tree
|
||||
): # This functions returns that is it full binary tree or not?
|
||||
if tree is None:
|
||||
return True
|
||||
if (tree.left is None) and (tree.right is None):
|
||||
return True
|
||||
if (tree.left is not None) and (tree.right is not None):
|
||||
return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right))
|
||||
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main(): # Main func for testing.
|
||||
def main(): # Main func for testing.
|
||||
tree = Node(1)
|
||||
tree.left = Node(2)
|
||||
tree.right = Node(3)
|
||||
@ -59,5 +65,5 @@ def main(): # Main func for testing.
|
||||
display(tree)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@ -1,13 +1,14 @@
|
||||
'''
|
||||
"""
|
||||
A binary search Tree
|
||||
'''
|
||||
class Node:
|
||||
"""
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, label, parent):
|
||||
self.label = label
|
||||
self.left = None
|
||||
self.right = None
|
||||
#Added in order to delete a node easier
|
||||
# Added in order to delete a node easier
|
||||
self.parent = parent
|
||||
|
||||
def getLabel(self):
|
||||
@ -34,8 +35,8 @@ class Node:
|
||||
def setParent(self, parent):
|
||||
self.parent = parent
|
||||
|
||||
class BinarySearchTree:
|
||||
|
||||
class BinarySearchTree:
|
||||
def __init__(self):
|
||||
self.root = None
|
||||
|
||||
@ -46,90 +47,90 @@ class BinarySearchTree:
|
||||
if self.empty():
|
||||
self.root = new_node
|
||||
else:
|
||||
#If Tree is not empty
|
||||
# If Tree is not empty
|
||||
curr_node = self.root
|
||||
#While we don't get to a leaf
|
||||
# While we don't get to a leaf
|
||||
while curr_node is not None:
|
||||
#We keep reference of the parent node
|
||||
# We keep reference of the parent node
|
||||
parent_node = curr_node
|
||||
#If node label is less than current node
|
||||
# If node label is less than current node
|
||||
if new_node.getLabel() < curr_node.getLabel():
|
||||
#We go left
|
||||
# We go left
|
||||
curr_node = curr_node.getLeft()
|
||||
else:
|
||||
#Else we go right
|
||||
# Else we go right
|
||||
curr_node = curr_node.getRight()
|
||||
#We insert the new node in a leaf
|
||||
# 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
|
||||
# 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
|
||||
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):
|
||||
# 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):
|
||||
# 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):
|
||||
# Has only left children
|
||||
elif node.getLeft() is not None and node.getRight() is None:
|
||||
self.__reassignNodes(node, node.getLeft())
|
||||
#Has two children
|
||||
# Has two children
|
||||
else:
|
||||
#Gets the max value of the left branch
|
||||
# Gets the max value of the left branch
|
||||
tmpNode = self.getMax(node.getLeft())
|
||||
#Deletes the tmpNode
|
||||
# Deletes the tmpNode
|
||||
self.delete(tmpNode.getLabel())
|
||||
#Assigns the value to the node to delete and keesp tree structure
|
||||
# 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
|
||||
# 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 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 node label is less than current node
|
||||
if label < curr_node.getLabel():
|
||||
#We go left
|
||||
# We go left
|
||||
curr_node = curr_node.getLeft()
|
||||
else:
|
||||
#Else we go right
|
||||
# Else we go right
|
||||
curr_node = curr_node.getRight()
|
||||
return curr_node
|
||||
|
||||
def getMax(self, root = None):
|
||||
if(root is not None):
|
||||
def getMax(self, root=None):
|
||||
if root is not None:
|
||||
curr_node = root
|
||||
else:
|
||||
#We go deep on the right branch
|
||||
# We go deep on the right branch
|
||||
curr_node = self.getRoot()
|
||||
if(not self.empty()):
|
||||
while(curr_node.getRight() is not None):
|
||||
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):
|
||||
def getMin(self, root=None):
|
||||
if root is not None:
|
||||
curr_node = root
|
||||
else:
|
||||
#We go deep on the left branch
|
||||
# We go deep on the left branch
|
||||
curr_node = self.getRoot()
|
||||
if(not self.empty()):
|
||||
if not self.empty():
|
||||
curr_node = self.getRoot()
|
||||
while(curr_node.getLeft() is not None):
|
||||
while curr_node.getLeft() is not None:
|
||||
curr_node = curr_node.getLeft()
|
||||
return curr_node
|
||||
|
||||
@ -150,34 +151,34 @@ class BinarySearchTree:
|
||||
return self.root
|
||||
|
||||
def __isRightChildren(self, node):
|
||||
if(node == node.getParent().getRight()):
|
||||
if node == node.getParent().getRight():
|
||||
return True
|
||||
return False
|
||||
|
||||
def __reassignNodes(self, node, newChildren):
|
||||
if(newChildren is not None):
|
||||
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)):
|
||||
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
|
||||
# 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
|
||||
# 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
|
||||
# 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
|
||||
# Returns an string of all the nodes labels in the list
|
||||
# In Order Traversal
|
||||
def __str__(self):
|
||||
list = self.__InOrderTraversal(self.root)
|
||||
str = ""
|
||||
@ -185,6 +186,7 @@ class BinarySearchTree:
|
||||
str = str + " " + x.getLabel().__str__()
|
||||
return str
|
||||
|
||||
|
||||
def InPreOrder(curr_node):
|
||||
nodeList = []
|
||||
if curr_node is not None:
|
||||
@ -193,8 +195,9 @@ def InPreOrder(curr_node):
|
||||
nodeList = nodeList + InPreOrder(curr_node.getRight())
|
||||
return nodeList
|
||||
|
||||
|
||||
def testBinarySearchTree():
|
||||
r'''
|
||||
r"""
|
||||
Example
|
||||
8
|
||||
/ \
|
||||
@ -203,15 +206,15 @@ def testBinarySearchTree():
|
||||
1 6 14
|
||||
/ \ /
|
||||
4 7 13
|
||||
'''
|
||||
"""
|
||||
|
||||
r'''
|
||||
r"""
|
||||
Example After Deletion
|
||||
7
|
||||
/ \
|
||||
1 4
|
||||
|
||||
'''
|
||||
"""
|
||||
t = BinarySearchTree()
|
||||
t.insert(8)
|
||||
t.insert(3)
|
||||
@ -223,20 +226,20 @@ def testBinarySearchTree():
|
||||
t.insert(4)
|
||||
t.insert(7)
|
||||
|
||||
#Prints all the elements of the list in order traversal
|
||||
# Prints all the elements of the list in order traversal
|
||||
print(t.__str__())
|
||||
|
||||
if(t.getNode(6) is not None):
|
||||
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):
|
||||
if t.getNode(-1) is not None:
|
||||
print("The label -1 exists")
|
||||
else:
|
||||
print("The label -1 doesn't exist")
|
||||
|
||||
if(not t.empty()):
|
||||
if not t.empty():
|
||||
print(("Max Value: ", t.getMax().getLabel()))
|
||||
print(("Min Value: ", t.getMin().getLabel()))
|
||||
|
||||
@ -247,11 +250,12 @@ def testBinarySearchTree():
|
||||
t.delete(6)
|
||||
t.delete(14)
|
||||
|
||||
#Gets all the elements of the tree In pre order
|
||||
#And it prints them
|
||||
# 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()
|
||||
|
@ -1,28 +1,28 @@
|
||||
class FenwickTree:
|
||||
|
||||
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
||||
def __init__(self, SIZE): # create fenwick tree with size SIZE
|
||||
self.Size = SIZE
|
||||
self.ft = [0 for i in range (0,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):
|
||||
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)
|
||||
def query(self, i): # query cumulative data from index 0 to i in O(lg N)
|
||||
ret = 0
|
||||
while (i > 0):
|
||||
while i > 0:
|
||||
ret += self.ft[i]
|
||||
i -= i & (-i)
|
||||
return ret
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
f = FenwickTree(100)
|
||||
f.update(1,20)
|
||||
f.update(4,4)
|
||||
f.update(1, 20)
|
||||
f.update(4, 4)
|
||||
print(f.query(1))
|
||||
print(f.query(3))
|
||||
print(f.query(4))
|
||||
f.update(2,-5)
|
||||
f.update(2, -5)
|
||||
print(f.query(1))
|
||||
print(f.query(3))
|
||||
|
@ -1,34 +1,38 @@
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
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
|
||||
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
|
||||
return idx * 2
|
||||
|
||||
def right(self, idx):
|
||||
return idx*2 + 1
|
||||
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)])
|
||||
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]
|
||||
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:
|
||||
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
|
||||
@ -36,22 +40,22 @@ class SegmentTree:
|
||||
|
||||
if r < a or l > b:
|
||||
return True
|
||||
if l >= a and r <= b :
|
||||
if l >= a and r <= b:
|
||||
self.st[idx] = val
|
||||
if l!=r:
|
||||
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)])
|
||||
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]
|
||||
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
|
||||
@ -64,27 +68,27 @@ class SegmentTree:
|
||||
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)
|
||||
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):
|
||||
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]
|
||||
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.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()
|
||||
|
@ -75,7 +75,7 @@ def main():
|
||||
10: [],
|
||||
11: [],
|
||||
12: [],
|
||||
13: []
|
||||
13: [],
|
||||
}
|
||||
level, parent = bfs(level, parent, max_node, graph, 1)
|
||||
parent = creatSparse(max_node, parent)
|
||||
|
@ -700,7 +700,6 @@ def main():
|
||||
|
||||
print_results("Tree traversal", test_tree_chaining())
|
||||
|
||||
|
||||
print("Testing tree balancing...")
|
||||
print("This should only be a few seconds.")
|
||||
test_insertion_speed()
|
||||
|
@ -1,10 +1,12 @@
|
||||
import math
|
||||
|
||||
class SegmentTree:
|
||||
|
||||
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.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):
|
||||
@ -20,51 +22,55 @@ class SegmentTree:
|
||||
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)])
|
||||
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]
|
||||
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 :
|
||||
if l == r:
|
||||
self.st[idx] = val
|
||||
return True
|
||||
mid = (l+r)//2
|
||||
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)])
|
||||
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]
|
||||
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
|
||||
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):
|
||||
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]
|
||||
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)
|
||||
segt.update(1, 3, 111)
|
||||
print(segt.query(1, 15))
|
||||
segt.update(7,8,235)
|
||||
segt.update(7, 8, 235)
|
||||
segt.showData()
|
||||
|
@ -7,6 +7,7 @@ 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()
|
||||
|
Reference in New Issue
Block a user