用于语法错误的术语横向模板,寻找&#x27 ;;;

发布于 2025-02-05 03:48:06 字数 2411 浏览 1 评论 0原文

我创建了一个用于二进制树的横向横向的模板。在声明堆栈时,我会遇到语法错误。它期望一个';'但是我看不到我会放在哪里';

我可以将其运行以使用int类型而不是t类型运行。我想知道我在模板中做错了什么。

#include "Node.h"
#include <stack> 
#include <iostream>

template <typename T>
void inOrderTraversal(Node<T>* root)
{
    stack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTTIFIER, EXPECTTED ';'
    Node<T>* curr = root;
    // Initially s.empty==true
    while (curr != NULL || s.empty() == false)
    {
        /* Reach the left most Node of the
           curr Node */
        while (curr != NULL)
        {
            /* place pointer to a tree node on
               the stack before traversing
              the node's left subtree */
            s.push(curr);
            curr = curr->left;
            // end of inner whitl
        }

        /* Current must be NULL at this point */
        curr = s.top();
        s.pop();
        // print 
        cout << curr->data << " ";

        /* we have visited the node and its
           left subtree.  Now, it's right
           subtree's turn */
        curr = curr->right;

    } /* end of while */
}


// function to create binary tree node on the heap
template <typename T>
Node<T>* newNodeOnHeap(T data) {
    Node* nodetoreturn = new Node; // make node on the heap
    nodetoreturn->value = data;
    nodetoreturn->left = nullptr;
    nodetoreturn->right = nullptr;
    return &nodetoreturn;
}

// function to delete binary tree node on the heap
template <typename T>
void deleteNodeOnHeap(T& node){
    delete node; // delete node
   
}


int main() {
    // make a binary tree on the heap
    int data = 10;
    Node<int>* root = newNodeOnHeap(data);
    root->left = newNodeOnHeap(5);
    root->right = newNodeOnHeap(20);
    root->left->left = newNodeOnHeap(3);
    root->left->right = newNodeOnHeap(7);
    root->right->right = newNodeOnHeap(30);


// In order transversal of binary tree

    inOrderTraversal(root);
  
}

这是错误 stack&lt; node&lt; t&gt;*&gt; s //构建错误“意外标识符,预期';'

这是node.h文件

#define NODE_H_JAMES

template <typename T>
class Node
{
public:
    T value;
    Node* left;
    Node* right;

    // constructor
    Node(T val)
    {
        this->value = val;
        left = right = nullptr;
    
    }

};

I created a template for an inorder transverse of a binary tree. I get a syntax error in declaring the stack. It expects a ';' but I do not see where I would put an ';'

I can get it to run with int type instead of T type. I wonder what I am doing wrong in the template.

#include "Node.h"
#include <stack> 
#include <iostream>

template <typename T>
void inOrderTraversal(Node<T>* root)
{
    stack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTTIFIER, EXPECTTED ';'
    Node<T>* curr = root;
    // Initially s.empty==true
    while (curr != NULL || s.empty() == false)
    {
        /* Reach the left most Node of the
           curr Node */
        while (curr != NULL)
        {
            /* place pointer to a tree node on
               the stack before traversing
              the node's left subtree */
            s.push(curr);
            curr = curr->left;
            // end of inner whitl
        }

        /* Current must be NULL at this point */
        curr = s.top();
        s.pop();
        // print 
        cout << curr->data << " ";

        /* we have visited the node and its
           left subtree.  Now, it's right
           subtree's turn */
        curr = curr->right;

    } /* end of while */
}


// function to create binary tree node on the heap
template <typename T>
Node<T>* newNodeOnHeap(T data) {
    Node* nodetoreturn = new Node; // make node on the heap
    nodetoreturn->value = data;
    nodetoreturn->left = nullptr;
    nodetoreturn->right = nullptr;
    return &nodetoreturn;
}

// function to delete binary tree node on the heap
template <typename T>
void deleteNodeOnHeap(T& node){
    delete node; // delete node
   
}


int main() {
    // make a binary tree on the heap
    int data = 10;
    Node<int>* root = newNodeOnHeap(data);
    root->left = newNodeOnHeap(5);
    root->right = newNodeOnHeap(20);
    root->left->left = newNodeOnHeap(3);
    root->left->right = newNodeOnHeap(7);
    root->right->right = newNodeOnHeap(30);


// In order transversal of binary tree

    inOrderTraversal(root);
  
}

This is the error
stack<Node<T>*> s; // BUILD ERROR "UNEXPECTED IDENTIFIER, EXPECTED ';'

This is the Node.h file

#define NODE_H_JAMES

template <typename T>
class Node
{
public:
    T value;
    Node* left;
    Node* right;

    // constructor
    Node(T val)
    {
        this->value = val;
        left = right = nullptr;
    
    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文