C ++二进制树打印函数实现

时间:2018-05-12 05:59:20

标签: c++ binary-tree binary-search-tree computer-science breadth-first-search

我必须在C ++中实现二叉树,问题是我刚刚开始用C ++编写代码。所以我对这个主题并不熟悉,在用C或Python编写之前,而不是用C ++编写。

我已经创建并初始化了一棵二叉树,覆盖了一些leafes,一个树根元素,现在我想知道我所做的是否正常,因此它有效或完全无意义。

检查我的代码,我使用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;
}

自定义标头文件:

"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

我的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";
   }
  }
  }

在这个文件中有些东西搞砸了,我不知道。我只想打印元素,而不是总是短信“Call Printfunction!”,我想将它们打印到输出控制台。图形输出可以在以后完成,现在我只想让它运行。

#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();
}

不知怎的,我/我们必须找到一种方法来打印包含元素,如果它们已经在树中,否则我必须找到错误为什么树仍然是空的。

如上所述,我刚开始用C ++开发。是的,有一些BFS的样本算法或相关的东西,但它们都没有像我这样的复杂性。

1 个答案:

答案 0 :(得分:0)

从您的代码我可以生成缺少的头文件。但是我的一些声明可能与你的不同。 nodeCSearchTreeBinInt的标头文件如下所示,可能是:

/* "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

可能存在问题,因为我(简单地)将所有内容放在公共范围内。

您需要实现函数Node::Print,因为我添加了此函数。例如。 简单,打印树的递归函数如下所示:

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

这类似于Data Structures & Algorithms in Java, 2nd ed. by Robert Lafore第381-382页中的内容。如果你想用C ++继续编程这部分计算机科学,我建议用C ++学习一些通用的C ++和数据结构和算法(我听过Robert Sedgewick写了一本书)。

最后一步:在函数CSearchTreeBin::Print中,您需要添加一个else-case并检查有效的根节点。如果根节点有效,则调用根节点的递归Print并观察魔法发生。

您在代码注释中添加了更多问题,而您的代码中包含一些粗略的C ++部分(0NULL之间的不一致,有时使用std::,有时会写using namespace } ...)。 此网页可能会帮助您学习C ++ - EnglishGerman中的元素。另外,欢迎使用C ++ - 编程。

编辑:你不会像我一样命名这个函数Node::Print,但我会这样离开。