Tree

Hello I'm trying to wrap my head around the implementation of a simple binary tree

What I'm trying

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 #include using namespace std; struct bintree { int number ; char letter ; bintree *left = NULL ; bintree *right = NULL ; }; bintree *newNode ( int x, char c ) ; void printList ( bintree *r) ; void insertNode (bintree *node, bintree *&r) ; int main() { bintree *node, *root = NULL ; node = newNode (1, 'c') ; insertNode ( node, root ) ; node = newNode (2, 'e') ; insertNode ( node, root ) ; node = newNode (3, 'd') ; insertNode ( node, root ) ; printList (root); return 0; } bintree *newNode ( int x, char c ) { bintree * n = new bintree ; n->number = x; n->letter = c; return n ; } void printList ( bintree *r ) { cout << r->number << r->letter << endl; r = r->right ; cout << r->number << r->letter << endl; r = r->right ; cout << r->number << r->letter << endl; } void insertNode (bintree *node, bintree *&r) { if (r == NULL) r = node ; else if (r->left == NULL && r->right == NULL) { if (r->number > node->number) r->left = node ; if ( r->number < node->number) r->right = node ; else { while (r != NULL) { if(r->number > node->number) r = r->left; else if(r->number < node->number) r = r->right ; } if (r == NULL) r = node; } } }

It crashes when I try to print, I dont think it is inserting the last values into the tree and so when it trys to print a null value it crashes. I'm just curious if I'm heading in the right direction and perhaps how to fix my printing issue as well as some hints on deleting and searching.

Thank you.
 t trys to print a null value it crashes
Yes, you need to check whether r is NULL or not in any case before you can use it