Learning about binary trees

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...)
Last edited on
Personally, I appreciate the effort and time you took to write your post and to give context.
However, you must keep in mind that the shorter the post, the better.

Ask your question clearly, succinctly, and provide just the code that your question is about.
Thus you make it easier for us to help you.

Now regarding the questions at the end of your post...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int a;

int *p = &a; // pointer declaration
// * is part of the declaration
// & is the reference operator, takes address of a

*p = 5; // * is the dereference operator
// now a equals 5

int &ra = a; // reference declaration
// & is part of the declaration
// ra is an alias (different name) for a

ra = 6; // now both a and ra equal 6
a = 16; // now both a and ra equal 16

void func(int *p) // * is part of the declaration of p
{
}

void func(int &r) // & is part of the declaration of r
{
}


The symbols * and & also have a role as binary operators. (In the example above they were unary operators.)

*a == dereference operator
a * b == multiplication operator

&a == reference (address-of) operator
a & b == "bitwise AND" operator


So keep in mind, when they are used in a declaration, as in sandwiched in between a type and an identifier (e.g. variable name, function name, etc.) they are not operators.
Last edited on
Sorry it took me awhile to pop back online and thank you for your thoughtful answer. I reviewed what you said as well as some additional information about pointers and structures. I think that part of my issue in this case, aside from the obvious new-ness to pointers, is how I was (mentally) interpreting what the structure was doing and how it was being called in the code. I *think* I have a somewhat better understanding of this now.

Thank you again!
Topic archived. No new replies allowed.