minh_tran_782

Print Binary Tree (C++)

Oct 28th, 2021 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. void SplayTree::printTree ()
  2. {
  3.     prettyPrintTree(root, "", true);
  4.     cout << "------------------------\n";
  5. }
  6. void SplayTree::prettyPrintTree(Node* root, string prefix, bool isLeft) {
  7.     if (root == nullptr) {
  8.         cout << "Empty tree";
  9.         return;
  10.     }
  11.  
  12.     if(root->right) {
  13.         prettyPrintTree(root->right, prefix + (isLeft ? "│   " : "    "), false);
  14.     }
  15.  
  16.     cout << prefix + (isLeft ? "└── " : "┌── ") + root->user.name + "\n";
  17.  
  18.     if (root->left) {
  19.         prettyPrintTree(root->left, prefix + (isLeft ? "    " : "│   "), true);
  20.     }
  21. }
Add Comment
Please, Sign In to add comment