c++ - How can I create the tree? -
I am trying to make a BST and it needs to print recurring, post order and preorder.
Not sure how to make this tree in my main ()
function.
struct tree_net {Tree_Node * right; Tree_Node * left; Difference information; }; Square bTree {Private: Tree_Node * Route; Public: Beatty (); Zero bTree :: Insert (tree_node * and tree, intestine item); Zero bTree :: preorderPrint (Tree_Node * root); }; BTree :: bTree () {root = NULL; } Void bTree :: Insert (Tree_Node * and tree, int item) {if (tree == NULL) {tree = new Tree_Node; Tree-> OK = null; Tree-> Left = null; Tree-> Information = item; } And if (item & lt; tree-> information) insert (tree-> left, object); And insert (tree-> right, item); } Zero bTree :: preorderPrint (Tree_Node * root) {if (root! = NULL) {cout & lt; & Lt; Root- & gt; Information & lt; & Lt; ""; Preder print (root-> left); Preder print (root-> right); }} Void main () {// This is where I need help // I'm not sure how to insert a new node bTree test; Test. Insert ()
With the appearance of things, you can just write
Test.Instert (Test.root, 3); // Enter the test 3. Insert (test.oft, 4); // Insert 4
And of course, you have to make the route public.
However, this is a bit weird, because the first parameter will always be bTree.root - and you do not need to make it. Remember that the users of your data type (you or someone else) Do not care - they only care about their data. Instead, I do not need a feature Insert
method which only takes an integer (a tree node) - it is called overloading.
zero bTree :: enter (int item) {insert (root, item); } // Keep the other insert method, but make it private
Then you can just type:
Test.Instert (3); Test. Insert (4);
Comments
Post a Comment