Note that there are some explanatory texts on larger screens.

plurals
  1. POPrint a binary tree in a pretty way
    primarykey
    data
    text
    <p>Just wondering if I can get some tips on printing a pretty binary tree in the form of:</p> <pre><code>5 10 11 7 6 3 4 2 </code></pre> <p>Right now what it prints is:</p> <pre><code> 2 4 3 6 7 11 10 5 </code></pre> <p>I know that my example is upside down from what I'm currently printing, which it doesn't matter if I print from the root down as it currently prints. Any tips are very appreciated towards my full question:</p> <p>How do I modify my prints to make the tree look like a tree?</p> <pre><code> //Binary Search Tree Program #include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;queue&gt; using namespace std; int i = 0; class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool isEmpty() const { return root==NULL; } void print_inorder(); void inorder(tree_node*); void print_preorder(); void preorder(tree_node*); void print_postorder(); void postorder(tree_node*); void insert(int); void remove(int); }; // Smaller elements go left // larger elements go right void BinarySearchTree::insert(int d) { tree_node* t = new tree_node; tree_node* parent; t-&gt;data = d; t-&gt;left = NULL; t-&gt;right = NULL; parent = NULL; // is this a new tree? if(isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent while(curr) { parent = curr; if(t-&gt;data &gt; curr-&gt;data) curr = curr-&gt;right; else curr = curr-&gt;left; } if(t-&gt;data &lt; parent-&gt;data) { parent-&gt;left = t; } else { parent-&gt;right = t; } } } void BinarySearchTree::remove(int d) { //Locate the element bool found = false; if(isEmpty()) { cout&lt;&lt;" This Tree is empty! "&lt;&lt;endl; return; } tree_node* curr; tree_node* parent; curr = root; while(curr != NULL) { if(curr-&gt;data == d) { found = true; break; } else { parent = curr; if(d&gt;curr-&gt;data) curr = curr-&gt;right; else curr = curr-&gt;left; } } if(!found) { cout&lt;&lt;" Data not found! "&lt;&lt;endl; return; } // 3 cases : // 1. We're removing a leaf node // 2. We're removing a node with a single child // 3. we're removing a node with 2 children // Node with single child if((curr-&gt;left == NULL &amp;&amp; curr-&gt;right != NULL) || (curr-&gt;left != NULL &amp;&amp; curr-&gt;right == NULL)) { if(curr-&gt;left == NULL &amp;&amp; curr-&gt;right != NULL) { if(parent-&gt;left == curr) { parent-&gt;left = curr-&gt;right; delete curr; } else { parent-&gt;right = curr-&gt;left; delete curr; } } return; } //We're looking at a leaf node if( curr-&gt;left == NULL &amp;&amp; curr-&gt;right == NULL) { if(parent-&gt;left == curr) { parent-&gt;left = NULL; } else { parent-&gt;right = NULL; } delete curr; return; } //Node with 2 children // replace node with smallest value in right subtree if (curr-&gt;left != NULL &amp;&amp; curr-&gt;right != NULL) { tree_node* chkr; chkr = curr-&gt;right; if((chkr-&gt;left == NULL) &amp;&amp; (chkr-&gt;right == NULL)) { curr = chkr; delete chkr; curr-&gt;right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element if((curr-&gt;right)-&gt;left != NULL) { tree_node* lcurr; tree_node* lcurrp; lcurrp = curr-&gt;right; lcurr = (curr-&gt;right)-&gt;left; while(lcurr-&gt;left != NULL) { lcurrp = lcurr; lcurr = lcurr-&gt;left; } curr-&gt;data = lcurr-&gt;data; delete lcurr; lcurrp-&gt;left = NULL; } else { tree_node* tmp; tmp = curr-&gt;right; curr-&gt;data = tmp-&gt;data; curr-&gt;right = tmp-&gt;right; delete tmp; } } return; } } void BinarySearchTree::print_postorder() { postorder(root); } void BinarySearchTree::postorder(tree_node* p) { if(p != NULL) { if(p-&gt;left) postorder(p-&gt;left); if(p-&gt;right) postorder(p-&gt;right); cout&lt;&lt;" "&lt;&lt;p-&gt;data&lt;&lt;"\n "; } else return; } int main() { BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout&lt;&lt;endl&lt;&lt;endl; cout&lt;&lt;" Binary Search Tree Operations "&lt;&lt;endl; cout&lt;&lt;" ----------------------------- "&lt;&lt;endl; cout&lt;&lt;" 1. Insertion/Creation "&lt;&lt;endl; cout&lt;&lt;" 2. Printing "&lt;&lt;endl; cout&lt;&lt;" 3. Removal "&lt;&lt;endl; cout&lt;&lt;" 4. Exit "&lt;&lt;endl; cout&lt;&lt;" Enter your choice : "; cin&gt;&gt;ch; switch(ch) { case 1 : cout&lt;&lt;" Enter Number to be inserted : "; cin&gt;&gt;tmp; b.insert(tmp); i++; break; case 2 : cout&lt;&lt;endl; cout&lt;&lt;" Printing "&lt;&lt;endl; cout&lt;&lt;" --------------------"&lt;&lt;endl; b.print_postorder(); break; case 3 : cout&lt;&lt;" Enter data to be deleted : "; cin&gt;&gt;tmp1; b.remove(tmp1); break; case 4: return 0; } } } </code></pre>
    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.
 

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