MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __str__

Method __str__

data_structures/binary_tree/avl_tree.py:296–329  ·  view source on GitHub ↗
(
        self,
    )

Source from the content-addressed store, hash-verified

294 self.root = del_node(self.root, data)
295
296 def __str__(
297 self,
298 ) -> str: # a level traversale, gives a more intuitive look on the tree
299 output = ""
300 q = MyQueue()
301 q.push(self.root)
302 layer = self.get_height()
303 if layer == 0:
304 return output
305 cnt = 0
306 while not q.is_empty():
307 node = q.pop()
308 space = " " * int(math.pow(2, layer - 1))
309 output += space
310 if node is None:
311 output += "*"
312 q.push(None)
313 q.push(None)
314 else:
315 output += str(node.get_data())
316 q.push(node.get_left())
317 q.push(node.get_right())
318 output += space
319 cnt = cnt + 1
320 for i in range(100):
321 if cnt == math.pow(2, i) - 1:
322 layer = layer - 1
323 if layer == 0:
324 output += "\n*************************************"
325 return output
326 output += "\n"
327 break
328 output += "\n*************************************"
329 return output
330
331
332def _test() -> None:

Callers

nothing calls this directly

Calls 8

pushMethod · 0.95
get_heightMethod · 0.95
is_emptyMethod · 0.95
popMethod · 0.95
MyQueueClass · 0.85
get_dataMethod · 0.80
get_leftMethod · 0.80
get_rightMethod · 0.80

Tested by

no test coverage detected