Search code examples
c++binary-treebinary-search-treecomputer-sciencebreadth-first-search

C++ Binary Tree Print Function Implementation


I have to realize a binary tree in C++ and the problem is I have just started to code in C++. So I am really new to this topic and before I have coded in C or Python, not in C++.

I allready have created and initalized a binary tree, overgiving some leafes, a tree root element and now I want to see if what I've done is okay, hence it works or it's total nonsense.

Check my code, I use Visual Studio 2017 Enterprise:

"bintree.cpp"
#include "cseatreebin.h"
#include <iostream>
using namespace std;

void main() 
{
CSearchTreeBin MyTree;
std::cout << "#######################################################\n"
             "##################Binary Tree C++######################\n";

MyTree.Insert(5);
MyTree.Insert(15);
MyTree.Insert(7);
MyTree.Insert(-5);
MyTree.Insert(6);
MyTree.Insert(3);
MyTree.Insert(650);
MyTree.Insert(20);
MyTree.Insert(-20);
MyTree.Insert(510);
MyTree.Print();
MyTree.Print(); cout << endl;
//cout << "Amount of Treenodes: " << MyTree.GetNrOfNodes() << endl;
// Amount/Number should be calculated again if allready called once
//cout << "Amount of Treenodes: " << MyTree.GetNrOfNodes() << endl;
// ... only if the tree has changed... 
//MyTree.Insert(99);
//cout << "Number of treenodes: " << MyTree.GetNrOfNodes() << endl;
}

The custom headerfile:

"cseatreebin.h"
#ifndef SEARCHTREEBIN_H
#define SEARCHTREEBIN_H

class CSearchTreeBinInt;

class CSearchTreeBin 
{
public:
CSearchTreeBin(void);

void Insert(int);

void Print();

private:
CSearchTreeBinInt *pInternalRep;
};

#endif  // SEARCHTREEBIN_H

And my Binary Tree Initalationfile:

#include "cseatreebinint.h"
#include <stdlib.h>
#include <iostream>

using namespace std;
CSearchTreeBinInt::CSearchTreeBinInt()
{
pRoot   = 0; //init and create treeroot
};

void CSearchTreeBinInt::Insert(int dat)  
{
Insert(pRoot, dat); //insert the root to the tree
}

void CSearchTreeBinInt::Insert(Node*& rpNode, int dat) 
{
if (rpNode == NULL) //check if there are nodes in the tree
{
rpNode        = new Node; //create new nodes if there are none
rpNode->dat   = dat; 
rpNode->pLeft = rpNode->pRight = NULL;
std::cout << "Binary Tree  has been initalized correctly-> inserting new Elements!\n\n";
 }
else
 {
   if (dat < rpNode->dat) {            // inserted data is less then existing?
       Insert(rpNode->pLeft, dat);    // put it on the left
       std::cout << "A Node has been inserted on the left!\n";
   }
   else {                              // if it's bigger then already existing nodes
       Insert(rpNode->pRight, dat);   // put it on the right side of the tree
       std::cout << "A Node has been inserted on the right side!\n";
   }
  }
  }

And in this file something is messed up, I don't know. I just want to print the elements, not always the text message "Call Printfunction!", I want to print them to output console. An graphical output could be done later, now I just want to make it run.

#include "cseatreebin.h"
#include "cseatreebinint.h"
#include <stdlib.h>
#include <iostream>

using namespace std;



CSearchTreeBin::CSearchTreeBin()
{
pInternalRep=new CSearchTreeBinInt; //init. and creation of Binarytree
};
void CSearchTreeBin::Insert(int dat)  //dat = is this the node which will   be inserted?
{
pInternalRep->Insert(dat);
}

void CSearchTreeBin::Print() {
int a;
std::cout << "Printfunction has been called!\n\n";
if (pInternalRep == NULL) return;

//CSearchTreeBin::Print(); // this won't work that easily
//pInternalRep->CSearchTreeBin::Print();
}

Somehow I/we have to find a way to print the containing elements, if they are already inside the tree, otherwise I have to find the error why the tree remains empty.

I just started to develop in C++, as mentioned above. And yes there some sample algorithms of BFS or something related but none of them has such a complexity like mine.


Solution

  • From your code I can generate the missing header file. But some of my declarations may be different from yours. Your header file for the node and for the CSearchTreeBinInt looks like this, maybe:

    /* "cseatreebinint.h"
     *
     */
    #ifndef SEARCHTREEBININT_H
    #define SEARCHTREEBININT_H
    
    class Node
        {
    public:
        Node *pLeft, *pRight;
        int dat;
        void Print(); //maybe this needs to be added
        };
    
    class CSearchTreeBinInt
        {
    public:
        Node* pRoot;
        CSearchTreeBinInt(void);
        void Insert(int);
        void Insert(Node*& , int );
        };
    
    #endif  // SEARCHTREEBININT_H
    

    There might be issues, because I (simply) put everything in the public scope.

    You would need to implement the function Node::Print, because I added this function. E.g. a simple, recursive function to print your tree would look like this:

    void Node::Print()
        {
        std::cout << "This node contains the value " << dat << std::endl;
        if (pLeft != NULL)
            {
            pLeft->Print();
            }
        if (pRight != NULL)
            {
            pRight->Print();
            }
        }
    

    This is similar to something written in Data Structures & Algorithms in Java, 2nd ed. by Robert Lafore pp. 381-382. If you wanted to keep programming in this part of computer science in C++, it would be my recommendation to study some general C++ and Data Structs and Algorithms in C++ (I hear Robert Sedgewick wrote a book).

    For the final touch: In your function CSearchTreeBin::Print you would need to add an else-case and check for a valid root-node. If the root-node is valid, you call the recursive Print of the root-node and watch the magic happen.

    You put in more questions in your code-comments and your code has some crude C++ parts (inconsistency between 0 and NULL, sometimes using std:: and sometimes writing using namespace...). This webpage might help you learn C++-elements in English or maybe German. Also, welcome to C++-Programming.

    Edit: You wouldn't name the function Node::Print like I did, but I'll leave it like this.