SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node:
  2.     def __init__(self, data, left=None, right=None):
  3.         self.data = data
  4.         self.left = left
  5.         self.right = right
  6.  
  7.  
  8. class Tree:
  9.     def __init__(self):
  10.         self.root = Node(1)
  11.         self.nodes = [self.root]
  12.         self.array = []
  13.         self.counts = []
  14.  
  15.     def insert(self, node, data, direction):
  16.         for each in self.nodes:
  17.             if each.data == node:
  18.                 if direction == 0 and data != -1:
  19.                     new_node = Node(data)
  20.                     each.left = new_node
  21.                     self.nodes.append(new_node)
  22.                 elif data != -1:
  23.                     new_node = Node(data)
  24.                     each.right = new_node
  25.                     self.nodes.append(new_node)
  26.  
  27.     def display(self, node):
  28.         if node:
  29.             self.display(node.left)
  30.             self.array.append(node.data)
  31.             self.display(node.right)
  32.  
  33.     def getDepth(self, root, count):
  34.         node = root
  35.         if node:
  36.             self.getDepth(node.left, count+1)
  37.             self.counts.append(count)
  38.             self.getDepth(node.right, count+1)
  39.  
  40.     def swap(self, root, query, counter):
  41.         node = root
  42.         if counter+1 == query:
  43.             node.left, node.right = node.right, node.left
  44.             return
  45.         else:
  46.             if node.left is not None:
  47.                 self.swap(node.left, query, counter+1)
  48.             if node.right is not None:
  49.                 self.swap(node.right, query, counter+1)
  50.  
  51.  
  52. def swapNodes(indexes, queries):
  53.     b_tree = Tree()
  54.     b_tree.insert(1, indexes[0], 0)
  55.     b_tree.insert(1, indexes[1], 1)
  56.     my_array = []
  57.     i = -1
  58.     for each in indexes:
  59.         i += 1
  60.         if each == -1:
  61.             i -= 1
  62.             continue
  63.         else:
  64.             counter = i + (i+2)
  65.             if counter > len(indexes)-1:
  66.                 break
  67.  
  68.             b_tree.insert(each, indexes[counter], 0)
  69.             counter += 1
  70.  
  71.             if counter > len(indexes)-1:
  72.                 break
  73.             b_tree. insert(each, indexes[counter], 1)
  74.  
  75.     b_tree.getDepth(b_tree.root, 1)
  76.     depth = max(b_tree.counts)
  77.     for query in queries:
  78.         H = [query * i for i in range(1, depth) if query * i <= depth]
  79.         for h in H:
  80.             b_tree.swap(b_tree.root, h, 0)
  81.         b_tree.display(b_tree.root)
  82.         my_array.append(b_tree.array)
  83.         b_tree.array = []
  84.     return my_array
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top