BTS树搜索功能C ++

问题描述 投票:0回答:1

该程序读取CSV文件,并将其输入到二进制搜索树中。到目前为止,我已经设法插入了一个新节点,并将其排序,但是在内部,执行搜索以请求Varibale密钥,但是我没有设法使其正常工作,有人可以帮我吗?

CSV文件正在读取的文件包括:1,名称1,1234562,名称2,1651513,名称3,1566516


#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
#include <vector>

struct Person {
  int key;
  std::string name;
  int num;
};

struct Node : Person {
  Node(const Person &person) : Person(person) {}
  std::unique_ptr<Node> left, right;
  void insert(const Person &person);
};

void Node::insert(const Person &person) {
  /* recur down the tree */
  if (key > person.key) {
    if (left)
        left->insert(person);
    else
        left = std::make_unique<Node>(person);
  } else if (key < person.key) {
    if (right)
        right->insert(person);
    else
        right = std::make_unique<Node>(person);
  }
}

std::vector<Person> persons;

void inorder(Node *root) {
  if (root) {
    // cout<<"\t";

    inorder(root->left.get());
    std::cout << '\t' << root->key << ' ' << root->name << ' ' << root->num << '\n';
    inorder(root->right.get());
  }
}

Node *minValueNode(Node *node) {
  Node *current = node;

  /* loop down to find the leftmost leaf */
  while (current && current->left) current = current->left.get();

  return current;
}


int main() {
  std::unique_ptr<Node> root;
  std::ifstream fin("data.txt");
  if (!fin) {
    std::cout << "File not open\n";
    return 1;
  }

  std::string line;
  const char delim = ',';

  while (std::getline(fin, line)) {
    std::istringstream ss(line);
    Person person;
    ss >> person.key;
    ss.ignore(10, delim);
    std::getline(ss, person.name, delim);
    ss >> person.num;
    if (ss) persons.push_back(person);
  }

  for (unsigned int i = 0; i < persons.size(); i++) {
    std::cout << std::setw(5) << persons[i].key << std::setw(20)
              << persons[i].name << std::setw(15) << persons[i].num << '\n';
    if (!root) root = std::make_unique<Node>(persons[i]);
    else root->insert(persons[i]);
  }

  std::cout << "\n\nInorder:\n";
  //    cout<<node.name;

  inorder(root.get());

  return 0;
}

/*bool busqueda(Node *root, int dato)
{

    if(root){
    return false;
    }
    else if(root->key==dato){
        return true;
    }
    else if(dato<root->key){
       busqueda(root->left.get(),dato);
    }
    else{
        return busqueda(root->right.get(),dato);
    }

   }*/

c++ csv binary-tree binary-search-tree
1个回答
0
投票

假定这是您需要帮助的功能:

bool busqueda(Node *root, int dato) {
    if(root){
    return false;
    }
    else if(root->key==dato){
        return true;
    }
    else if(dato<root->key){
       busqueda(root->left.get(),dato);
    }
    else{
        return busqueda(root->right.get(),dato);
    }
}

这里有一些问题:

if(root) {

这是在测试root是否为[[不是 nullptr,因此,如果root指向某物,您将立即保释。意思是,如果提供了any树,即您想要的树的opposite,您将立即返回false。更糟糕的是,如果root is null,您将继续尝试取消引用null指针,这将使程序崩溃。将此行更改为if (root != nullptr) {

else if(dato<root->key){ busqueda(root->left.get(),dato); }
此递归调用看起来很正确,除了

您不返回结果

,这意味着您将到达返回bool的函数的末尾而没有实际返回任何有意义的东西。只需在此函数调用之前添加return
© www.soinside.com 2019 - 2024. All rights reserved.