I'm assuming because my teacher has never mentioned ctor to us, that I don't have to use that style of initialization?
I guess there's a lot going on here that I should elaborate on. ctor is a common written abbreviation for 'constructor.' In C++, there are three ways of initializing a variable:
int var1 = 0; // Assignment style
int var2(0); // Constructor style
int var3{0}; // Brace style, post C++11 only. Compile your code with the flag -std=c++11 if you are using gcc or clang
If you are using C++11, it is usually recommended to initialize most things with braces, with a few exceptions. This is because among other things, braced initialization does not allow narrowing, which is a term for the loss of data that occurs when you try to initialize a variable with a larger value than will fit.
Also, is that line of code just putting a null value into the left and right child?
That's correct. The list of members following the colon after the constructor name is an initializer list. Initializer lists are the preferred way of initializing a class or struct's members. You can see from the above description of initialization methods that each member is being initialized to nullptr by braced initialization. Constructor style initialization would also work here (but the assignment operator (the equals sign) would not).
As for the traverse function, that's just identifying if a left and/or right child exists? So if I wanted to do preorder traversal, I believe that prints out all the values in the left side of the tree first, and then the right, right? So, if I had, lets say, five nodes on the right side of the tree, and four on the right, would I just need a variable to keep track of the amount of nodes on each side of the tree, and then just loop the traversal by that value?
The traverse function checks if the left or right child exists, and if it does, it calls traverse on that child. If you were to call traverse on the root node (the initial node at the top of the tree with no parents), the function would visit all of the nodes in the tree. No looping or variables needed here, this function already does a preorder traversal. Stepping through the code in a debugger might be helpful in understanding what it's doing.
As for the main function, I've never actually put anything in the "(" ")" before, could you explain that to me?
Do you mean the char ** argv? That is called an argument vector. The argument vector is an array containing strings that are passed to your program as parameters. argc is the length (number of strings) of the argument vector. argv[0] by default is your executable name. The reason these parameters exist is so that you can pass arguments to your program via the command line. Say that you saved the example code I posted as a file called example.cpp and then compiled it to an executable like this:
clang++ example.cpp -std=c++11 -o example // clang is a compiler, you could also use gcc/icc/whatever you have
This would generate an executable in the same directory called example (I believe if you are on windows, the executable would also have an extension, .exe).
Then if you ran the executable with some parameters...
./example some parameters here // I don't know how to run an executable on windows from the command line, but you might not need the ./
The argument vector would contain:
argv[0] = "example"
argv[1] = "some"
argv[2] = "parameters"
argv[3] = "here"
argv[4] = NULL <-- A null terminator, not included in the argument count
If you're interested in this and you're working in a unix based environment, the standard ways of processing command line arguments are either doing it yourself, or using getopt:
https://www.gnu.org/software/libc/manual/html_node/Getopt.htmlThanks again for your help and support man. Seriously means a lot to me!
No problem!