Print binary tree structure with its contents
Write an efficient algorithm to print a binary tree structure in standard output.
For example, a binary tree on the left can be displayed as a binary tree on the right programmatically. The following code in C++, Java, and Python serves as an excellent helper function to tree problems for printing a binary tree or BST:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include <iostream> #include <string> using namespace std; // Data structure to store a binary tree node struct Node { int data; Node *left, *right; Node(int data) { this->data = data; this->left = this->right = nullptr; } }; struct Trunk { Trunk *prev; string str; Trunk(Trunk *prev, string str) { this->prev = prev; this->str = str; } }; // Helper function to print branches of the binary tree void showTrunks(Trunk *p) { if (p == nullptr) { return; } showTrunks(p->prev); cout << p->str; } void printTree(Node* root, Trunk *prev, bool isLeft) { if (root == nullptr) { return; } string prev_str = " "; Trunk *trunk = new Trunk(prev, prev_str); printTree(root->right, trunk, true); if (!prev) { trunk->str = "———"; } else if (isLeft) { trunk->str = ".———"; prev_str = " |"; } else { trunk->str = "`———"; prev->str = prev_str; } showTrunks(trunk); cout << " " << root->data << endl; if (prev) { prev->str = prev_str; } trunk->str = " |"; printTree(root->left, trunk, false); } int main() { // Construct above tree Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); root->left->left->left = new Node(8); root->left->left->right = new Node(9); root->left->right->left = new Node(10); root->left->right->right = new Node(11); root->right->left->left = new Node(12); root->right->left->right = new Node(13); root->right->right->left = new Node(14); root->right->right->right = new Node(15); // print constructed binary tree printTree(root, nullptr, false); return 0; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
class Trunk { Trunk prev; String str; Trunk(Trunk prev, String str) { this.prev = prev; this.str = str; } }; // A Binary Tree Node class Node { int data; Node left, right; Node() {} Node(int data) { this.data = data; this.left = this.right = null; } } class Main { public static void showTrunks(Trunk p) { if (p == null) { return; } showTrunks(p.prev); System.out.print(p.str); } public static void printTree(Node root, Trunk prev, boolean isLeft) { if (root == null) { return; } String prev_str = " "; Trunk trunk = new Trunk(prev, prev_str); printTree(root.right, trunk, true); if (prev == null) { trunk.str = "———"; } else if (isLeft) { trunk.str = ".———"; prev_str = " |"; } else { trunk.str = "`———"; prev.str = prev_str; } showTrunks(trunk); System.out.println(" " + root.data); if (prev != null) { prev.str = prev_str; } trunk.str = " |"; printTree(root.left, trunk, false); } public static void main(String[] args) { // Construct above tree Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); root.left.left.left = new Node(8); root.left.left.right = new Node(9); root.left.right.left = new Node(10); root.left.right.right = new Node(11); root.right.left.left = new Node(12); root.right.left.right = new Node(13); root.right.right.left = new Node(14); root.right.right.right = new Node(15); // print constructed binary tree printTree(root, null, false); } } |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# A Binary Tree Node class Node: def __init__(self, data=None, left=None, right=None): self.data = data self.left = left self.right = right class Trunk: def __init__(self, prev=None, str=None): self.prev = prev self.str = str def showTrunks(p): if p is None: return showTrunks(p.prev) print(p.str, end='') def printTree(root, prev, isLeft): if root is None: return prev_str = ' ' trunk = Trunk(prev, prev_str) printTree(root.right, trunk, True) if prev is None: trunk.str = '———' elif isLeft: trunk.str = '.———' prev_str = ' |' else: trunk.str = '`———' prev.str = prev_str showTrunks(trunk) print(' ' + str(root.data)) if prev: prev.str = prev_str trunk.str = ' |' printTree(root.left, trunk, False) if __name__ == '__main__': # Construct above tree root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) root.left.left.left = Node(8) root.left.left.right = Node(9) root.left.right.left = Node(10) root.left.right.right = Node(11) root.right.left.left = Node(12) root.right.left.right = Node(13) root.right.right.left = Node(14) root.right.right.right = Node(15) # print constructed binary tree printTree(root, None, False) |
Set next pointer to the inorder successor of all nodes in a binary tree
Determine whether two nodes lie on the same path in a binary tree
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)