C++11 60:6:错误:没有匹配的成员函数用于调用';推动';s、 推(头);
编译器说,C++11 60:6:错误:没有匹配的成员函数用于调用';推动';s、 推(头);,c++11,tree,stack,C++11,Tree,Stack,编译器说,stack::push()或我在下面代码中使用的任何堆栈函数都没有匹配的函数 此代码用于树的迭代预顺序遍历 代码如下: #包括 #包括 结构节点{ int数据; 节点*父节点,*左节点,*右节点; }*root=nullptr; 节点*创建节点(int num){ Node*new_Node=新节点; 新建_节点->数据=num; 新建节点->父节点=新建节点->左节点=新建节点->右节点=空PTR; 返回新的_节点; } void createTree(int num){ Node*
stack::push()
或我在下面代码中使用的任何堆栈函数都没有匹配的函数
此代码用于树的迭代预顺序遍历
代码如下:
#包括
#包括
结构节点{
int数据;
节点*父节点,*左节点,*右节点;
}*root=nullptr;
节点*创建节点(int num){
Node*new_Node=新节点;
新建_节点->数据=num;
新建节点->父节点=新建节点->左节点=新建节点->右节点=空PTR;
返回新的_节点;
}
void createTree(int num){
Node*new_Node=createNode(num);
如果(!root){
根=新的_节点;
返回;
}
节点*头=根,*父节点=根;
while(head){
if(numdata){
if(head->left==nullptr){
head->left=新建_节点;
新建\u节点->父节点=父节点;
}
父节点=头;
头部=头部->左侧;
}
否则{
if(head->right==nullptr){
head->right=新建_节点;
新建\u节点->父节点=父节点;
}
父节点=头;
头部=头部->右侧;
}
}
}
无效预订单行程(节点*头部){
std::堆栈s;
而(1){
while(head){
s、 推(头);
std::cout剩余数据;
}
如果(!s.empty())
打破
头部=s.顶部();
头部=头部->右侧;
s、 pop();
}
}
int main(){
int-num;
对于(int i=0;i<7;i++){
std::cin>>num;
createTree(num);
}
前序行程(根);
std::cout它应该是std::stack s
而不是std::stack s
,因为堆栈应该包含指向节点或节点*
的元素
对不起,我的错
#include <iostream>
#include <stack>
struct Node {
int data;
Node *parent, *left, *right;
} *root = nullptr;
Node *createNode(int num) {
Node *new_node = new Node;
new_node -> data = num;
new_node -> parent = new_node -> left = new_node -> right = nullptr;
return new_node;
}
void createTree(int num) {
Node *new_node = createNode(num);
if(!root) {
root = new_node;
return;
}
Node *head = root, *parent_node = root;
while(head) {
if(num < head -> data) {
if(head -> left == nullptr) {
head -> left = new_node;
new_node -> parent = parent_node;
}
parent_node = head;
head = head -> left;
}
else {
if(head -> right == nullptr) {
head -> right = new_node;
new_node -> parent = parent_node;
}
parent_node = head;
head = head -> right;
}
}
}
void preorderTraversal(Node *head) {
std::stack<Node> s;
while(1) {
while(head) {
s.push(head);
std::cout << head -> data << " ";
head = head -> left;
}
if(!s.empty())
break;
head = s.top();
head = head -> right;
s.pop();
}
}
int main() {
int num;
for(int i = 0; i < 7; i++) {
std::cin >> num;
createTree(num);
}
preorderTraversal(root);
std::cout << std::endl;
return 0;
}