简体   繁体   English

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

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

I have created a node.h class, defining a class called node for representing a binary tree(any type). 我创建了一个node.h类,定义了一个称为node的类,用于表示二叉树(任何类型)。 It seems to be that the constructor isn't working. 似乎是构造函数无法正常工作。 The errors are below.Ive only started writing constructors within classes like these, and this is the first ive encountered binary trees. 错误如下:我只是开始在此类之内编写构造函数,而这是我第一次遇到二进制树。 Can anyone point me in the right direction on how to fix these errors and make my code work? 谁能为我指出正确的方向,以解决这些错误并使我的代码正常工作? Thanks. 谢谢。 在此处输入图片说明

Node.h 节点

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

There is no declarations of members you use in your constructor and other methods. 没有在构造函数和其他方法中使用的成员声明。 The compiler does not know what rht or right means. 编译器不知道“ rht或“ right含义。 Judging from your code the class should look more like this: 从您的代码来看,该类应该看起来像这样:

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:
    //..
};

So now the compiler knows what left , right and item mean. 因此,现在编译器知道leftrightitem含义。 Now you can use these identifiers inside member functions of that class. 现在,您可以在该类的成员函数中使用这些标识符。 Note that compiler still does not know what rht or lft are, so you should replace them with right and left . 请注意,编译器仍然不知道rhtlft是什么,因此应将它们替换为rightleft

Hope this helps 希望这可以帮助

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

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