gpt4 book ai didi

c++ - 在 C++ 中,我不知道为什么我的函数不返回结果

转载 作者:行者123 更新时间:2023-11-30 05:35:36 25 4
gpt4 key购买 nike

<分区>

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;

ifstream fin("1.inp");

class TreeNode {
private:
friend class Tree;
int Keyvalue;
string Namevalue;
TreeNode *LeftChild;
TreeNode *RightChild;

TreeNode() { LeftChild = RightChild = 0; };
TreeNode(int _Keyvalue, string _Namevalue, TreeNode *Lefty, TreeNode *Righty) {
Keyvalue = _Keyvalue;
Namevalue = _Namevalue;
LeftChild = Lefty;
RightChild = Righty;
}
};

class Tree {
private:
TreeNode *root;
void insert_to_tree(TreeNode *, int, string);
void show_tree(TreeNode *);
public:
Tree() { root = 0; }
void insert_to_tree(int, string);
void delete_from_tree(int);
void show_tree();
void setup();
};

void Tree::insert_to_tree(int _Keyvalue, string _Namevalue){
insert_to_tree(root, _Keyvalue, _Namevalue);
}
//this is problem.
void Tree::insert_to_tree(TreeNode *CurrentNode, int _Keyvalue, string _Namevalue){
if (CurrentNode == 0)
CurrentNode = new TreeNode(_Keyvalue, _Namevalue, 0, 0);
else{
if (CurrentNode->Keyvalue < _Keyvalue)
insert_to_tree(CurrentNode->RightChild, _Keyvalue, _Namevalue);
else if (CurrentNode->Keyvalue == _Keyvalue)
CurrentNode->Namevalue = _Namevalue;
else
insert_to_tree(CurrentNode->LeftChild, _Keyvalue, _Namevalue);
}
}

void Tree::delete_from_tree(int _Keyvalue){
}

void Tree::show_tree(){
show_tree(root);
}
void Tree::show_tree(TreeNode *CurrentNode){
if (CurrentNode){
if (CurrentNode->LeftChild == 0 && CurrentNode->RightChild == 0){
cout << CurrentNode->Namevalue;
}
else{
show_tree(CurrentNode->LeftChild);
show_tree(CurrentNode->RightChild);
}
}
}

void get_command(Tree &_MyTree){
int Num_of_command;
string command;
fin >> Num_of_command;
for (int i = 0; i < Num_of_command; i++){
fin >> command;
int Keyvalue; string Namevalue;
if (command == "ins"){
fin >> Keyvalue >> Namevalue;
_MyTree.insert_to_tree(Keyvalue, Namevalue);
}
else if (command == "del"){
fin >> Keyvalue;
_MyTree.delete_from_tree(Keyvalue);
}
else if (command == "leaves")
_MyTree.show_tree();
}
}

int main(){
Tree MyTree;
get_command(MyTree);
}

当我调用函数时

void Tree::insert_to_tree(int _Keyvalue, string _Namevalue){
insert_to_tree(root, _Keyvalue, _Namevalue); }

root 没有被我的函数改变。函数后

void Tree::insert_to_tree(TreeNode *CurrentNode, int _Keyvalue, string _Namevalue)

root 始终为 0。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com