Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree C++ (Parents)
    text
    copied!<p>I need just a little more help on my BST. This is what my BST looks like when inserting:</p> <p>R, L, J, G</p> <pre><code> R --Root at Index 0 / \ L @ Index1 L NULL / \ J @ Index3 J NULL / \ G @ Index7 G NULL </code></pre> <p>Here is the code that makes it happen.</p> <pre><code>void BST::insert(const data &amp;aData) { if ( items[Parent].empty ) { items[Parent].theData = aData; // insert at leaf. items[Parent].empty = false; size++; return; } for ( int i = 0; i &lt;= size; i++ ) { if ( aData &lt; items[Parent].theData ) { if ( items[2*i+1].empty ) { items[2*i+1].theData = aData; items[2*i+1].empty = false; } else { // we must already have a left child to some root. Parent++; So make the previous data the root??? if ( items[Parent].empty ) { items[Parent].theData = items[2*i+1].theData; items[Parent].empty = false; Parent = (i-1)/2; } } } else { ...// do the same for data greater than but with items[2*i+2] } </code></pre> <p>MY question is that when would i need to make a new root? When would I need to make a new root? For recomparison?</p> <p>Is this approach correct? Thank you to those who even both to look at my posts :)</p> <p>// The constructor the BST Class and its private section.</p> <pre><code>BST::BST(int capacity) : items(new item[capacity]), size(0), Parent(0), leftChild(0), rightChild(0) { items-&gt;empty = true; maxSize = capacity; } private: int size; // size of the ever growing/expanding tree :) int Parent; int maxSize; int leftChild; int rightChild; struct item { bool empty; data theData; }; item *items; // The tree array </code></pre>
 

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