c++ - 计算二叉树中的节点

标签 c++ function templates binary-tree

http://pastebin.com/gGY6Dw5y

上面的代码有一个链接。关键是创建一个方法来计算二叉树中的节点,然后创建两个也计算节点的方法,一个用于树的每一侧。我的问题是如何调用计算节点的方法。

我的问题是如何调用我的 countNodes 方法。我不知道它在寻找什么样的论点。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <cstdlib>
using namespace std;
template <class datatype>
class treenode{
public:
    datatype data;
    string qa;
    treenode<datatype> *lchild, *rchild;

};
template<class datatype>
class btree{
private:
    treenode<datatype> *root, *current;
public:
    btree();
    bool tree_empty(void);
    void insertdata(datatype x); //call from main program to insert data
    void inserttree(treenode<datatype> *&p, datatype d);
    void inorder(treenode<datatype> *p);
    void printinorder(void);//call from main program
    void preorder(treenode<datatype> *p);
    void printpreorder(void);//call from main program
    void postorder(treenode<datatype> *p);
    void printpostorder(void);//call from main program
    void deletevalue(datatype v);
    void deltree(datatype val, treenode<datatype> *&p);
    treenode<datatype>* findmin(treenode<datatype> *p);
    void inserttree2(treenode<datatype> *&p, datatype d, string qas);
    void insertdata2(datatype x, string a);
    void yesno(void);
    int countNodes(treenode<datatype> *p);


};

template<class datatype>
btree<datatype>::btree(){
    root =NULL;
};

template<class datatype>
bool btree<datatype>::tree_empty(){
    bool x = true;
    if(root == NULL){
            x = true;
    }else{
            x = false;
    }
    return x;
};

template<class datatype>
void btree<datatype>::insertdata(datatype x){
    inserttree(root,x);
};

template<class datatype>
void btree<datatype>::inserttree(treenode<datatype> *&p, datatype d){
    if(p == NULL){
            p = new treenode<datatype>;
            p->data = d;
            p->lchild = NULL;
            p->rchild = NULL;
    }else{
            if(p->data > d)
                    inserttree(p->lchild,d);
            else
                    inserttree(p->rchild,d);
    }
};

template<class datatype>
void btree<datatype>::inorder(treenode<datatype> *p){
    if(p!= NULL){
            inorder(p->lchild);
            cout<< p->data << "  ";
            inorder(p->rchild);
    }
};

template<class datatype>
void btree<datatype>::printinorder(void){
    inorder(root);
};

template<class datatype>
void btree<datatype>::preorder(treenode<datatype> *p){
    if(p!= NULL){
            cout<< p->data << "  ";
            preorder(p->lchild);
            preorder(p->rchild);
    }
};

template<class datatype>
void btree<datatype>::printpreorder(void){
    preorder(root);
};

template<class datatype>
void btree<datatype>::postorder(treenode<datatype> *p){
    if(p!= NULL){
            postorder(p->lchild);
            postorder(p->rchild);
            cout<< p->data << "  ";
    }
};

template<class datatype>
void btree<datatype>::printpostorder(void){
    postorder(root);
};

template<class datatype>
void btree<datatype>::deletevalue(datatype v){
    deltree(v,root);
};

template<class datatype>
void btree<datatype>::deltree(datatype val, treenode<datatype> *&p){
    treenode<datatype> *buff;
    if(p != NULL)
            if(val < p->data)
                    deltree(val,p->lchild);
            else
                    if(val > p->data)
                            deltree(val, p->rchild);
                    else
                            if(p->lchild == NULL && p->rchild == NULL)
                                    p= NULL;
                            else
                                    if(p->lchild == NULL)
                                            p= p->rchild;
                                    else
                                            if(p->rchild == NULL)
                                                    p = p->lchild;
                                            else
                                            {
                                                    buff = findmin(p->rchild);
                                                    buff->lchild = p->lchild;
                                                    p = p->rchild;

                                            }


};
template<class datatype>
void btree<datatype>::inserttree2(treenode<datatype> *&p, datatype d, string qas){
    if(p ==NULL){
            p = new treenode<datatype>;
            p->data = d;
            p->qa = qas;
            p->lchild = NULL;
            p->rchild = NULL;
    }else{
            if(p->data > d)
                    inserttree2(p->lchild,d,qas);
            else
                    inserttree2(p->rchild,d,qas);
    }
};

template<class datatype>
void btree<datatype>::insertdata2(datatype x, string a){
    inserttree2(root,x,a);
};
template<class datatype>
void btree<datatype>::yesno(void){
    current = root;
    bool solved = false;
    char c;
    cout<<current->qa<<endl;
    while(!solved){
            cout<<"Enter a Y or N";
            cin>> c;
            if(c == 'Y' ||c == 'N'){
            if(c == 'Y'){
                    current = current->lchild;
            }
            if(c == 'N'){
                    current = current->rchild;
            }
            cout<<current->qa<<endl;
            if(current->lchild == NULL){
                    solved = true;
                    cout<<"You have your answer "<<endl;
            }
            }
            }
};

template<class datatype>
treenode<datatype>* btree<datatype>::findmin(treenode<datatype> *p){
    if(p->lchild == NULL)
            return (p);
    else
            return(findmin(p->lchild));
};

template<class datatype>
int btree<datatype>::countNodes(treenode<datatype> *p){
int count = 1;    
    if ( p == NULL ){
       return 0;  
            }else{
       int count = 1;   /
       count += countNodes(p->lchild);  

       count += countNodes(p->rchild);
            }                                  /
       return count;  
    };

int _tmain(int argc, _TCHAR* argv[])
{


    btree<int> genesistree;
    int datavalues,
            deletevalues,
            addvalues,
            newdata,
            olddata,
            newnewdata,
            ifstatement,
            c,
    *p;
    bool flag;
    genesistree.insertdata(14);
            genesistree.insertdata(2);
            genesistree.insertdata(34);
            genesistree.insertdata(41);
            genesistree.insertdata(12);


    /*
    if(genesistree.tree_empty()){
            cout<<"The list is empty"<<endl;
    }else{
            cout<<"The list is not empty"<<endl;
    }
    cout<<""<<endl;

    cout<<"Enter the ammount of data values for the tree"<<endl;
    cin>>datavalues;
    cout<<""<<endl;
    for(c = datavalues, c >= 0; c--;){
            cout<<"Enter a datavalue for the tree"<<endl;
            cin>>newdata;
            genesistree.insertdata(newdata);
            cout<<""<<endl;
    }

    cout<<""<<endl;

    if(genesistree.tree_empty()){
            cout<<"The list is empty"<<endl;
    }else{
            cout<<"The list is not empty"<<endl;
    }

    cout<<""<<endl;
    cout<<"Print Preorder"<<endl;
    genesistree.printpreorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print in order"<<endl;
    genesistree.printinorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print Postorder"<<endl;
    genesistree.printpostorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Enter the ammount of datavalues you would like to delete"<<endl;
    cin>>deletevalues;
    cout<<""<<endl;
    if(deletevalues < datavalues){
    for(c = deletevalues, c >= 0; c--;){
            cout<<"Enter a datavalue to delete in the tree"<<endl;
            cin>>olddata;
            genesistree.deletevalue(olddata);

    }
    }else{
            if(deletevalues == 0){
                    cout<<""<<endl;
            }else{

            }
    }
    cout<<""<<endl;
    cout<<"Would you like to re print the results?"<<endl;
    cout<<""<<endl;
    cout<<"Hit 1 for yes, and 0 for no"<<endl;
    cin>>ifstatement;
    if(ifstatement == 1){
            flag = true;
    }else{
            flag = false;
    }
    if(flag == true){
    cout<<""<<endl;
    cout<<"Print Preorder"<<endl;
    genesistree.printpreorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print in order"<<endl;
    genesistree.printinorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print Postorder"<<endl;
    genesistree.printpostorder();
    cout<<""<<endl;
    cout<<""<<endl;
    }else{
            if(flag == false){
            cout<<""<<endl;
            }
    }
    cout<<""<<endl;

cout<<"Enter the ammount of datavalues you would like to add"<<endl;
    cin>>addvalues;
    cout<<""<<endl;
    if(addvalues > 0){
    for(c = deletevalues, c >= 0; c--;){
            cout<<"Enter a datavalue to add in the tree"<<endl;
            cin>>newnewdata;
            genesistree.insertdata(newnewdata);
            cout<<""<<endl;

    }
    }else{
            if(deletevalues == 0){
                    cout<<""<<endl;
            }else{

            }
    }
    cout<<""<<endl;
    cout<<"Would you like to re print the results?"<<endl;
    cout<<""<<endl;
    cout<<"Hit 1 for yes, and 0 for no"<<endl;
    cin>>ifstatement;
    if(ifstatement == 1){
            flag = true;
    }else{
            flag = false;
    }
    if(flag == true){
    cout<<""<<endl;
    cout<<"Print Preorder"<<endl;
    genesistree.printpreorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print in order"<<endl;
    genesistree.printinorder();
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"Print Postorder"<<endl;
    genesistree.printpostorder();
    cout<<""<<endl;
    cout<<""<<endl;
    }else{
            if(flag == false){
            cout<<""<<endl;
            }
    }
    */
    cout<<""<<endl;
    countNodes(genesistree);
            /*
    btree<int> mytree;
    string QandA[7];
    int Tpos[7];
    QandA[0] = "Question 1";
    QandA[1] = "Question 2";//Y to 0
    QandA[2] = "Question 3";//N to 0
    QandA[3] = "answer 1";//Y to 1
    QandA[4] = "answer 2";//N to 1
    QandA[5] = "answer 3";//N to 2
    QandA[6] = "answer ";//Y to 2

    Tpos[0] = 50;
    Tpos[1] = 40;
    Tpos[2] = 60;
    Tpos[3] = 20;
    Tpos[4] = 45;
    Tpos[5] = 70;
    Tpos[6] = 55;

    for(int i = 0; i<=6; i++){

            mytree.insertdata2(Tpos[i],QandA[i]);

    }
    mytree.yesno();



    cout<<endl;    
    */
    return 0;
}

最佳答案

您的countNodes 方法采用treeNode 指针。看起来没有任何方法可以访问您的根节点,因此该方法在 btree 类之外没有用。

我会像这样做另一种方法

template<class datatype>
int btree<datatype>::getNodeCount() const 
{
    if ( root == NULL ) return 0;
    // Otherwise continue counting from root.
    ...

关于c++ - 计算二叉树中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506777/

相关文章:

function - 编写一个函数来删除对象(如果存在)

templates - 为什么我的 Magento 产品页面的评论部分没有显示?

c++ - C++中带有模板变量的结构

C++ ostream 从对象数组返回数据

c++ - 为什么我无法检索变体的索引并使用它来获取其内容?

sql - 在SQL(Hive)中使用collect_list函数汇总用户序列

c++ - 非原始盒装类型的运算符

c++ - CPP 中的++ 运算符是否对 char 有任何重载?

c++ - 如何从 QListView 中搜索和选择项目?

c++ - 我的编译器是否混淆了它认为的重载函数?