Note that there are some explanatory texts on larger screens.

plurals
  1. POPretty Printing a Binary tree - Converting from C++ to Java
    primarykey
    data
    text
    <p>Can someone please help implement the code in <a href="http://www.ihas1337code.com/2010/09/how-to-pretty-print-binary-tree.html" rel="nofollow">this website</a> in Java based on the following class as the Node class:</p> <pre><code>public class Node&lt;A extends Comparable&lt;A&gt;&gt; { Node&lt;A&gt; left, right; A data; public Node(A data){ this.data = data; } } </code></pre> <p>The code is for pretty printing binary trees:</p> <pre><code> #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;deque&gt; #include &lt;iomanip&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;cmath&gt; using namespace std; struct BinaryTree { BinaryTree *left, *right; int data; BinaryTree(int val) : left(NULL), right(NULL), data(val) { } }; // Find the maximum height of the binary tree int maxHeight(BinaryTree *p) { if (!p) return 0; int leftHeight = maxHeight(p-&gt;left); int rightHeight = maxHeight(p-&gt;right); return (leftHeight &gt; rightHeight) ? leftHeight + 1: rightHeight + 1; } // Convert an integer value to string string intToString(int val) { ostringstream ss; ss &lt;&lt; val; return ss.str(); } // Print the arm branches (eg, / \ ) on a line void printBranches(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque&lt;BinaryTree*&gt;&amp; nodesQueue, ostream&amp; out) { deque&lt;BinaryTree*&gt;::const_iterator iter = nodesQueue.begin(); for (int i = 0; i &lt; nodesInThisLevel / 2; i++) { out &lt;&lt; ((i == 0) ? setw(startLen-1) : setw(nodeSpaceLen-2)) &lt;&lt; "" &lt;&lt; ((*iter++) ? "/" : " "); out &lt;&lt; setw(2*branchLen+2) &lt;&lt; "" &lt;&lt; ((*iter++) ? "\\" : " "); } out &lt;&lt; endl; } // Print the branches and node (eg, ___10___ ) void printNodes(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque&lt;BinaryTree*&gt;&amp; nodesQueue, ostream&amp; out) { deque&lt;BinaryTree*&gt;::const_iterator iter = nodesQueue.begin(); for (int i = 0; i &lt; nodesInThisLevel; i++, iter++) { out &lt;&lt; ((i == 0) ? setw(startLen) : setw(nodeSpaceLen)) &lt;&lt; "" &lt;&lt; ((*iter &amp;&amp; (*iter)-&gt;left) ? setfill('_') : setfill(' ')); out &lt;&lt; setw(branchLen+2) &lt;&lt; ((*iter) ? intToString((*iter)-&gt;data) : ""); out &lt;&lt; ((*iter &amp;&amp; (*iter)-&gt;right) ? setfill('_') : setfill(' ')) &lt;&lt; setw(branchLen) &lt;&lt; "" &lt;&lt; setfill(' '); } out &lt;&lt; endl; } // Print the leaves only (just for the bottom row) void printLeaves(int indentSpace, int level, int nodesInThisLevel, const deque&lt;BinaryTree*&gt;&amp; nodesQueue, ostream&amp; out) { deque&lt;BinaryTree*&gt;::const_iterator iter = nodesQueue.begin(); for (int i = 0; i &lt; nodesInThisLevel; i++, iter++) { out &lt;&lt; ((i == 0) ? setw(indentSpace+2) : setw(2*level+2)) &lt;&lt; ((*iter) ? intToString((*iter)-&gt;data) : ""); } out &lt;&lt; endl; } // Pretty formatting of a binary tree to the output stream // @ param // level Control how wide you want the tree to sparse (eg, level 1 has the minimum space between nodes, while level 2 has a larger space between nodes) // indentSpace Change this to add some indent space to the left (eg, indentSpace of 0 means the lowest level of the left node will stick to the left margin) void printPretty(BinaryTree *root, int level, int indentSpace, ostream&amp; out) { int h = maxHeight(root); int nodesInThisLevel = 1; int branchLen = 2*((int)pow(2.0,h)-1) - (3-level)*(int)pow(2.0,h-1); // eq of the length of branch for each node of each level int nodeSpaceLen = 2 + (level+1)*(int)pow(2.0,h); // distance between left neighbor node's right arm and right neighbor node's left arm int startLen = branchLen + (3-level) + indentSpace; // starting space to the first node to print of each level (for the left most node of each level only) deque&lt;BinaryTree*&gt; nodesQueue; nodesQueue.push_back(root); for (int r = 1; r &lt; h; r++) { printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); branchLen = branchLen/2 - 1; nodeSpaceLen = nodeSpaceLen/2 + 1; startLen = branchLen + (3-level) + indentSpace; printNodes(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); for (int i = 0; i &lt; nodesInThisLevel; i++) { BinaryTree *currNode = nodesQueue.front(); nodesQueue.pop_front(); if (currNode) { nodesQueue.push_back(currNode-&gt;left); nodesQueue.push_back(currNode-&gt;right); } else { nodesQueue.push_back(NULL); nodesQueue.push_back(NULL); } } nodesInThisLevel *= 2; } printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); printLeaves(indentSpace, level, nodesInThisLevel, nodesQueue, out); } int main() { BinaryTree *root = new BinaryTree(30); root-&gt;left = new BinaryTree(20); root-&gt;right = new BinaryTree(40); root-&gt;left-&gt;left = new BinaryTree(10); root-&gt;left-&gt;right = new BinaryTree(25); root-&gt;right-&gt;left = new BinaryTree(35); root-&gt;right-&gt;right = new BinaryTree(50); root-&gt;left-&gt;left-&gt;left = new BinaryTree(5); root-&gt;left-&gt;left-&gt;right = new BinaryTree(15); root-&gt;left-&gt;right-&gt;right = new BinaryTree(28); root-&gt;right-&gt;right-&gt;left = new BinaryTree(41); cout &lt;&lt; "Tree pretty print with level=1 and indentSpace=0\n\n"; // Output to console printPretty(root, 1, 0, cout); cout &lt;&lt; "\n\nTree pretty print with level=5 and indentSpace=3,\noutput to file \"tree_pretty.txt\".\n\n"; // Create a file and output to that file ofstream fout("tree_pretty.txt"); // Now print a tree that's more spread out to the file printPretty(root, 5, 0, fout); return 0; } </code></pre> <p><a href="http://www.ihas1337code.com/2010/09/how-to-pretty-print-binary-tree.html" rel="nofollow">http://www.ihas1337code.com/2010/09/how-to-pretty-print-binary-tree.html</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload