r/programminghelp • • Oct 24 '22

C++ New to Binary Trees

Ok so i just started learning Binary trees. The problem im having is my print function wont give me the output of the values in the left side of the binary tree. Can someone tell me how I can get the ouput i want?


#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;


struct Node{     // That Binary Tree

    int data;

    Node* right;
    Node* left;





};

Node* createNode(int data){

    Node* newNode = new Node();
    newNode->data = data;
    newNode->left = newNode-> right  = nullptr;
    return newNode;





}


void PrintNumbers(Node* root){

    if(root = nullptr) return;
    
    cout<<root->data <<endl;
    PrintNumbers(root->left);
    PrintNumbers(root->right);



}

int main(){

        srand(time(0));


    // Random list of numbers


Node*  root=NULL;

int Small0 = 1 + rand() % 3;
int Small1 = 1 + rand() % 3;
int small2 = 1 + rand() % 3;
int small3 = 1 + rand() % 3;
int small4 = 1 + rand() % 3;
int small5 = 1 + rand() % 3;

root->left = createNode(Small0);
root->left = createNode(Small1);
root->left = createNode(small2);
root->left = createNode(small3);
root->left = createNode(small4);
root->left = createNode(small5);


PrintNumbers(root);






}

Output i get

zsh: segmentation fault
3 Upvotes

8 comments sorted by

View all comments

Show parent comments

1

u/Luffysolos Oct 24 '22

Amazing. I also have another question. How would you insert data into a parent node?

1

u/link3333 Oct 24 '22

Are you asking how you would take new data (int in this example) and figure out where the new node for that new data goes in an existing tree?

1

u/Luffysolos Oct 24 '22

I mean yes

1

u/link3333 Oct 25 '22

Say you have 50 as the data for your root node, and you want to insert a 33. I would expect the left side of the tree to represent smaller values and the right side to represent larger values. So lets say 25 is already on the left and 75 on the right.

So you compare 33 against the current node you are looking at (root node to start, 50). If it's less than, you'll be inserting on the left side of the tree. If it's greater than, you'll be inserting on the right side of the tree. Can probably ignore equal for now. Probably don't need that case at the moment (or maybe never needed it).

So with the comparison, you know which side of the tree to add to. Well if you pretend that whole side is just another tree, you can do the same comparisons and repeat again. So we look at the left node 25.

You stop walking down the left or right nodes when it's null. Then you set the left or right of the node you are looking at to the new node you have created (for the 33 in this example). We were looking at the 25 node and found 33 to be bigger, and the right of 25 is null, so we set it to the 33 node.

    50
   /   \
25      75

    50
   /   \
25      75
   \
   33

If you have no root, then just assign your new node as the root. You can start with nothing and continually insert into the tree. Remember that an insert into the whole tree is going to look exactly the same as inserting into a section of the tree (recursion).

I'd start with making that recursive insert function working.

You may notice that you would run into a unbalanced tree if you were to start at 50 and go down to 1 for inserting. It would always want to insert to the left side. You'd really just have a linked list. The height of the left side would be 50 and the height of the right would be 0. Not really great for a binary search tree. You'll want to do a Google search for balancing a binary search tree.

1

u/Luffysolos Oct 25 '22

Good explanation, thanks a lot

1

u/eatmorepies23 Oct 25 '22 edited Oct 25 '22

Good response.

u/Luffysolos, if you're still having any trouble implementing this or another data structure, a good "first step" is to find a book on the subject. Many of them provide implementation details for data structures, whether using pseudocode or tailored to a specific language.

My undergrad class used "Data Structures and Other Objects using Java", by Michael Main. I liked it; it is very thorough and has a ton of projects. He has a C++ version out which is probably just as good.