繁体   English   中英

表示二叉树的节点类C ++

[英]Node Class to represent a binary tree C++

我创建了一个node.h类,定义了一个称为node的类,用于表示二叉树(任何类型)。 似乎是构造函数无法正常工作。 错误如下:我只是开始在此类之内编写构造函数,而这是我第一次遇到二进制树。 谁能为我指出正确的方向,以解决这些错误并使我的代码正常工作? 谢谢。 在此处输入图片说明

节点

#ifndef NODE_H
#define NODE_H
#include <iostream>

//an object of type node holds 3 things
// - an item (oftype 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:
    //..
};

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

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

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


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

    if (rht != NULL) {
        rht->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")));
}

int main() {

    Node<string> *s_tree;
    s_tree = setUpTree();

    cout << "Part 2 :Priting tree vals " << endl << endl;
    s_tree->printTree();
    cout << endl;

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

    return 0;
}

没有在构造函数和其他方法中使用的成员声明。 编译器不知道“ rht或“ right含义。 从您的代码来看,该类应该看起来像这样:

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:
    Node* left;
    Node* right;
    T item;
    //private data member functions:
    //..
};

因此,现在编译器知道leftrightitem含义。 现在,您可以在该类的成员函数中使用这些标识符。 请注意,编译器仍然不知道rhtlft是什么,因此应将它们替换为rightleft

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM