how can i optimze this furthir like for time complexit and space complexity make it good and explain in furthur 
import sys

class Node:
    def __init__(self, val):
        self.val = val
        self.uid = None
        self.parent = None
        self.child = []
        self.desc_set = set()
        self.isLocked = False

class M_Arr_Tree:
    def __init__(self, n, m, name_list):
        self.node_list = []
        self.node_map = {}

        for name in name_list:
            node = Node(name)
            self.node_list.append(node)
            self.node_map[name] = node
        
        for i in range(n):
            for j in range(m):
                child_idx = m * i + j + 1
                if child_idx < n:
                    self.node_list[i].child.append(self.node_list[child_idx])
                    self.node_list[child_idx].parent = self.node_list[i]
        
        self.root = self.node_list[0]

    def Lock(self, node, uid):
        if node.isLocked:
            return 'false'
        
        curr = node.parent
        while curr:
            if curr.isLocked:
                return 'false'
            curr = curr.parent

        if node.desc_set:
            return 'false'
        
        node.isLocked = True
        node.uid = uid

        curr = node.parent
        while curr:
            curr.desc_set.add(node)
            curr = curr.parent
        
        return 'true'

    def Unlock(self, node, uid):
        if not node.isLocked or node.uid != uid:
            return 'false'
        
        node.isLocked = False
        node.uid = None

        curr = node.parent
        while curr:
            curr.desc_set.discard(node)
            curr = curr.parent
        
        return 'true'

    def Upgrade(self, node, uid):
        if node.isLocked:
            return 'false'
        
        curr = node.parent
        while curr:
            if curr.isLocked:
                return 'false'
            curr = curr.parent
        
        locked_descs = list(node.desc_set)

        if not locked_descs:
            return 'false'
        
        for ld in locked_descs:
            if ld.uid != uid:
                return 'false'
        
        for ld in locked_descs:
            ld.isLocked = False
            ld.uid = None
            node.desc_set.discard(ld)

            curr = ld.parent
            while curr:
                curr.desc_set.discard(ld)
                curr = curr.parent
        
        node.isLocked = True
        node.uid = uid

        curr = node.parent
        while curr:
            curr.desc_set.add(node)
            curr = curr.parent
        
        return 'true'
    
    def Process(self, queries):
        res = []
        for q in queries:
            parts = q.strip().split()
            if len(parts) != 3:
                res.append('false')
                continue
            ops, node_name, uid = parts
            uid = int(uid)
            node = self.node_map.get(node_name, None)
            if ops == '1':
                res.append(self.Lock(node, uid))
            elif ops == '2':
                res.append(self.Unlock(node, uid))
            elif ops == '3':
                res.append(self.Upgrade(node, uid))
            else:
                res.append('false') 
        return res

def main():
    n = int(sys.stdin.readline().strip())
    m = int(sys.stdin.readline().strip())
    q = int(sys.stdin.readline().strip())

    name_list = []
    for _ in range(n):
        name = sys.stdin.readline().strip()
        name_list.append(name)

    queries = []
    for _ in range(q):
        query = sys.stdin.readline().strip()
        queries.append(query)
    
    tree = M_Arr_Tree(n, m, name_list)
    res = tree.Process(queries)
    for r in res:
        print(r)
main()