Print mahogany in level order in C

I have finished red ebony in c and it is difficult for me to print it in level order. I have a print-inorder, but I can’t imagine how I should display it as a tree in console printing. Is it possible? Can we implement BFS or DFS here? I found the algorithm in the wiki, but I cannot apply it. If anyone has the code for this in C, can you post it here so I can learn it? from wiki:

levelorder(root) q = empty queue q.enqueue(root) while not q.empty do node := q.dequeue() visit(node) if node.left β‰  null q.enqueue(node.left) if node.right β‰  null q.enqueue(node.right) 
+4
source share
1 answer

You can do BFS, but it can be easier to do an iterative in-depth search , as this will save you from having to execute the FIFO Queue in C. Pseudo-code:

 algorithm iterative-deepening(root) D = max-depth(root) for d=0 to D depth-limited-search(root, d) /* variant of DFS */ algorithm depth-limited-search(node, d) if node != NULL if d == 0 print node.contents else depth-limited-search(node.left, d - 1) depth-limited-search(node.right, d - 1) 
+4
source

Source: https://habr.com/ru/post/1389800/


All Articles