我创建了一个包含类Node的程序,用于表示任何类型(模板)的二叉树。

在我的Node.h类中,我有两个构造函数,但是不确定我是否正确实现了这两个构造函数。在构造函数中初始化值使我感到困惑。在我的main.cpp文件中,我有一个setUpTree函数。我的程序现在执行,但是不打印设置的树。

我已经尝试了几个小时试图解决这个问题,但是没有止境。我对C ++,指针,构造函数等还没有真正的经验。

如果有人可以帮助我修复代码以便setUpTree函数以及printTree方法起作用,我将不胜感激。

谢谢

Node.h类:

   #ifndef NODE_H
#define NODE_H
#include <iostream>
#include <string>
using namespace std;

//an object of type node holds 3 things
// - an item (of type t)
// - a left subtree
// - a right subtree

template<typename T>
class Node {
public:
    Node(T item); //constructor to create a leaf node
    Node(T item, Node *lft, Node *rht); //constructor which creates an internal node
    ~Node(); //Destructor

    //public data member functions:
    bool searchTree(T key);
    void printTree();

private:
    //private data member functions:
    Node* left;
    Node* right;
    T item;
};

//constructor
template<typename T>
Node<T>::Node(T i, Node<T> *lft, Node<T> *rht) {
    item = i;
    left = NULL;
    right = NULL;
}

//constructor
template <typename T>
Node<T>::Node(T i) { //should i be a parameter here?
    item = i; //is this right for this constructor?
}

//destructor
template <typename T>
Node<T>::~Node() {
    delete left;
    delete right;
    //delete;
}


//print tree method
template <typename T>
void Node<T>::printTree() {
    if (left != NULL) {
        left->printTree();
        cout << item << endl;//alphabetical order
    }

    if (right != NULL) {
        right->printTree();
        //cout << item << endl; //post order
    }
}

//search Tree method
template <typename T>
bool Node<T>::searchTree(T key) {
    bool found = false;
    if (item == key) {
        return true;
    }
    if (left != NULL) {
        found = left->searchTree(key);
        if (found) return true;
    }
    if (right != NULL) {
        return right->searchTree(key);
    }
    return false; //if left and right are both null & key is not the search item, then not found == not in the tree.
}

#endif


Main.cpp类别:

#include "Node.h"
#include <iostream>
using namespace std;

//set up tree method
Node<string> *setUpTree() {
    Node<string> *s_tree =
        new Node<string>("Sunday",
        new Node<string>("monday",
        new Node<string>("Friday"),
        new Node<string>("Saturday")),
        new Node<string>("Tuesday",
        new Node<string>("Thursday"),
        new Node<string>("Wednesday")));
    return s_tree;
}

int main() {

    Node<string> *s_tree;
    s_tree = setUpTree(); //call setUpTree method on s_tree

    cout << "Part 2 :Printing tree values: " << endl;
    s_tree->printTree(); //call print tree method

    cout << endl;

    //search for range of tree values
    //searchTree(s_tree, "Sunday");
    //searchTree(s_tree, "Monday");

    return 0;
}

最佳答案

我不知道这是否是唯一的问题,但是...如果构造叶子,则必须将leftright指针设置为NULL

template <typename T>
Node<T>::Node(T i) : left(NULL), right(NULL), item(i)
 { }


否则,在调用析构函数时

template <typename T>
Node<T>::~Node() {
    delete left;
    delete right;
    //delete;
}


delete在未定义的值上被调用;两次。

这是崩溃的完美秘诀。

在使用leftright的每个点中,还存在其他问题,如NULLprintTree()那样检查指针是否为searchTree():该值是不确定的,因此可以是非NULL,请通过测试并且printTree()在具有未定义值的指针上调用

-编辑-

建议的构造函数。

template <typename T>
Node<T>::Node (T i, Node<T> * lft, Node<T> * rht)
 : left(lft), right(right), item(i)
 { }

template <typename T>
Node<T>::Node (T i)
 : left(NULL), right(NULL), item(i)
 { }


-编辑2-


  现在至少打印一些值;星期一星期天星期二星期二。不确定其余


看看你的printTree()方法

模板
无效Node :: printTree(){
    if(left!= NULL){
        left-> printTree();
        cout <    }

if (right != NULL) {
    right->printTree();
    //cout << item << endl; //post order
}


}

仅当item不是left时才打印值(NULL)。因此,它不会显示叶子的值。

建议:即使printTree()item,也要修改left以打印NULL

举个例子

template <typename T>
void Node<T>::printTree() {
    if (left != NULL) {
        left->printTree();
    }

    cout << item << endl;

    if (right != NULL) {
        right->printTree();
    }
}

关于c++ - 设置二叉树,打印并搜索的程序-Node Class C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41147667/

10-12 15:05