How to print a Tree-adt
Writing and tree-related code is always inconvenient to debug, because the tree structure can be built with code
But it's better to have a good way to visualize it.
A few days ago I saw a code snippet from MIT and I was thankful ....
At first you might think of a relatively simple iterative implementation, as I did before
- void putout (int S, int *n)
implementation here
http://blog.csdn.net/cinmyheart/article/details/43086233
This function will print a triangle
And I saw the teaching Python code prepared by the MIT teacher with a sense of the light, with a more "precise" printing triangle strategy-based on recursion.
def _str (self): "" "Internal Method for ASCII art." " label = STR (self.key) If Self.left is None:left_lines, left_pos, left_width = [], 0, 0 else: Left_lines, left_pos, left_width = Self.left._str () If Self.right is None:right_lines, Right_po s, right_width = [], 0, 0 else:right_lines, right_pos, right_width = Self.right._str () middle = Max (Right_pos + left_width-left_pos + 1, Len (label), 2) pos = Left_pos + middle//2 width = left_pos + mi Ddle + right_width-right_pos while Len (Left_lines) < Len (right_lines): Left_lines.append ("* Left _width) while Len (Right_lines) < Len (left_lines): Right_lines.append ("* right_width) if (mi Ddle-len (label))% 2 = = 1 and self.parent is not None and the is Self.parent.left and Len (label) < Middle : Label + = '. ' Label = Label.center (Middle, '. ') IF label[0] = = '. ': label = ' + label[1:] if label[-1] = = '. ': label = label[:-1] + ' lines = [' * Left] _pos + label + ' * (right_width-right_pos), ' * left_pos + '/' + ' * (middle-2) + ' \ \ ' + ' * (right_width-right_pos)] + [Left_line + ' * (width-left_width-right_width) + Right_line For Left_line, right_line in Zip (Left_lines, right_lines)] return lines, POS, Width def __str__ (self): Return ' \ n '. Join (SELF._STR () [0])
The code did not give comments, toss me a lot of time ... Recursion ...
(After a while I comment, first put the code out)
Here is a version of the changes, but also a bit of a perfect place, but also can make use of:
def __str__ (self): def recurse (node): If node is None:return [], 0, 0 Else:left_lines, left_pos, left_width = recurse (node.left) Right_lines, R Ight_pos, right_width = recurse (node.right) label = STR (node.number) middle = max (Right_pos + left_width-left_pos +1 , Len (label), 2) pos = left_pos + MIDDLE//2 width = left_pos + middle + right_width-right_pos While Len (Left_lines) < Len (right_lines): Left_lines.append ("* left_width) While Len (Right_lines) < Len (left_lines): Right_lines.append ("* right_width) line = ["] * left_pos + label + ' * (Right_width-right_pos + 1), ' * left_pos + '/' + ' * (mid dle-2) + ' \ \ ' + ' * (right_width-right_pos)] + [left_l INE + "* (Width- Left_width-right_width) + right_line for Left_line, right_line in Zip (Left_lines, right_lines) If node is Self.root:return line Else:return line, POS, width if self.root is None:return ' <empty tree> ' output = recurse (self.root) for I in range (1, Len (OUTP UT)-2): output[0] + = ' \ n ' + output[i] return output[0]
Demo
The overall tree structure and hierarchy are clear
The following will update, the principle is clear ... Recursive implementation
How to print a Tree-adt? Algorithm for printing tree structure