I'm still relatively new to coding and practicing C++ on my own just to learn :) I was researching binary trees and pointers. I wrote a small program that is supposed to put random numbers into a vector and then store the elements of the vector into a binary tree. This is what I have so far (note that there is no code for the vector elements to be stored to a binary tree here yet):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
struct binaryTree{
int integerValue;
binaryTree* Right;
binaryTree* Left;
};
void printBinaryTreeToScreen(binaryTree* node){
if (node){
printBinaryTreeToScreen(node->Left);
cout << node->integerValue << endl;
printBinaryTreeToScreen(node->Right);
}
}
void printVectorToScreen (vector<int> &vectorInput) {
cout << "The integer vector elements are: " << endl;
for (size_t i = 0; i < vectorInput.size(); i++) {
cout << vectorInput[i] << " ";
}
cout << endl;
}
void populateVectorWithRandomIntegers (vector<int> &vectorInput, int sizeOfNewVector) {
for (int i = 0; i < sizeOfNewVector; i++){
vectorInput.push_back(rand() % 100 + 1);
}
}
int main()
{
srand (time(NULL));
vector<int> vectorWithIntegerElements;
int sizeOfVector = 7;
populateVectorWithRandomIntegers(vectorWithIntegerElements, sizeOfVector);
printVectorToScreen(vectorWithIntegerElements);
binaryTree* Root = NULL;
binaryTree* RightBranch = NULL;
binaryTree* LeftBranch = NULL;
binaryTree* Current = NULL;
Root = new binaryTree;
RightBranch = new binaryTree;
LeftBranch = new binaryTree;
Current = new binaryTree;
//Test code so I can see how my tree prints...
Root->integerValue = 1;
Root->Right = RightBranch;
Root->Left = LeftBranch;
RightBranch->integerValue=2;
RightBranch->Right = NULL;
RightBranch->Left = NULL;
LeftBranch->integerValue = 3;
LeftBranch->Right = NULL;
LeftBranch->Left = NULL;
Current = Root;
cout << endl << "Printing the binary tree: " << endl;
printBinaryTreeToScreen(Root);
return 0;
}
|
(Disclaimer: Once I figure out how to store the vector elements to the binary tree, I'm going to remove the 1-2-3 binary tree as it won't be necessary. Again, I just wanted to make sure my print function worked.)
I was having trouble figuring out a good way to store the values of the vector to the tree because I couldn't figure out how to compensate for the size of the tree at each level (root -> left, right and both the left and right nodes have their own left and right nodes, etc.) It occurred to me at some point that I was trying to balance the tree from the start and that it might be easier for learning if I just focused on getting numbers INTO the tree and then try to balance the tree.
To that end I tried researching how to store values into a binary tree. I came across this snippet on Wikipedia (
http://en.wikipedia.org/wiki/Binary_search_tree):
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void insert(Node* node, int value) {
if (value < node->key) {
if (node->leftChild == NULL)
node->leftChild = new Node(value);
else
insert(node->leftChild, value);
} else {
if(node->rightChild == NULL)
node->rightChild = new Node(value);
else
insert(node->rightChild, value);
}
}
|
From what I know about pointers, * is a pointer de-reference. From what I can understand of this function, it looks like the function stores anything that is less than the current node's value to the left child and anything greater than or equal to the current node's value to the right child. My questions about this are:
- I'm not sure why the function is calling for a pointer de-reference? Can someone help clarify this?
- I'm not sure how the function is calling the de-reference. From what I've learned about pointers so far, there is a difference between p, *p, and &p. For *p that refers to the actual value that is stored at the address referenced by the pointer &p itself. Correct? (Please clarify if I'm wrong or missing something here...)