【问题标题】:Binary Tree (insert and search within radius)二叉树(在半径内插入和搜索)
【发布时间】:2015-10-29 19:13:05
【问题描述】:

我正在编写一个二叉树搜索程序,但我不确定如何添加节点并通过它们进行搜索。节点来自一个 .txt 文件,该文件正在使用不同的文件读取,因此假设它已经工作。

文本文件如下所示: 名称 位置 旧楼 31.2222 新楼 21.2111

就像我说的,程序已经读入了文件,所以这不是问题。但是,我必须将名称和位置插入到二叉树的节点中。然后我必须搜索范围内的所有内容,该范围是正负值的来源。

旁注:我的复制构造函数也可能不正确,尽管它符合正确。

感谢您的帮助!

#ifndef BINTREE_HPP
#define BINTREE_HPP

#include <utility>
#include <string>
#include <vector>

class bintree {

// A binary search tree for locations in Lineland.

// Notes: 
// - Assume a flat, one-dimensional world with locations from -180 to 180.
// - All locations and distances are measured in the same units (degrees).

public:
// Default constructor
bintree() {
   this->root = NULL;
}

// Copy constructor
bintree(const bintree &t) {
  this -> root = NULL;
*this = t;
 }

// Destructor
~bintree() {

}


 // Copy assignment is implemented using the copy-swap idiom

 friend void swap(bintree &t1, bintree &t2) {
 using std::swap;
 // Swap all data members here, e.g.,
 // swap(t1.foo, t2.foo);
 // Pointers should be swapped -- but not the things they point to.
 }
 bintree &operator= (bintree other) {
 // You don't need to modify this function.
 swap(*this, other);
 return *this;
 }

 void insert(const std::string& name, double p) {
   // insert node with name and location (p)
 }

 void within_radius(double p, double r, std::vector<std::string> &result) const {
  // Search for elements within the range `p` plus or minus `r`.
  // Clears `result` and puts the elements in `result`.
  // Postcondition: `result` contains all (and only) elements of the
  // tree, in any order, that lie within the range `p` plus or minus
  // `r`.
  }

private:

struct node
{
 node *left;
 node *right;

};

node* root; 

};

#endif

【问题讨论】:

    标签: c++ search insert binary-search-tree


    【解决方案1】:

    首先,您的节点需要保存数据:

    struct node
    {
     node *left;
     node *right;
     std::string name;  // This is the key for your reasearch
     double p;          // followed by other data 
    };
    

    然后你可以考虑浏览你的树来插入一个新节点。 在此示例中,我假设您可以插入多个具有相同名称的节点。

    void insert(const std::string& name, double p) {
       node *n = new node;       // create a new node
       n->name=name; n->p=p;     // intialise the data payload
       n->left=n->right=nullptr; // and make it a leaf.
    
       if (root==nullptr)        // if tree is empty, 
          root = n;                   // add the new node. 
    
       else {                    // else find where to insert it
          node* t=root; 
          while (true) {
              if (t->name > n->name) {     // go to left
                  if (t->left==nullptr) {
                     t->left = n;     
                     break;  
                  }
                  else t=t->left; 
              }
              else if (t->name == n->name) { // insert between current and next
                  n->right = t->right; 
                  t->right = n; 
                  break; 
              }
              else {                         // go to right 
                  if (t->right==nullptr) {
                     t->right = n;     
                     break;  
                  }
                  else t=t->right; 
              }
           }
        }
     }
    

    这里是live demo

    请注意,我只回答了您的插入问题,您仍然需要自己做很多事情(operator= 和复制构造函数需要审查,需要创建析构函数等...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多