每个树记录的多个值

问题描述:

我已经构建了一棵树来保存每条记录的单个字符串(数据)。我怎样才能让它为每个记录保存多个字符串?每个树记录的多个值

void BinarySearchTree::insert(string d) 
{ 
tree_node* t = new tree_node; 
tree_node* parent; 
t->data = d; 
t->left = NULL; 
t->right = NULL; 
parent = NULL; 

// is this a new tree? 
if(isEmpty()) root = t; 
else 
{ 
    //Note: ALL insertions are as leaf nodes 
    tree_node* curr; 
    curr = root; 
    // Find the Node's parent 
    while(curr) 
    { 
     parent = curr; 
     if(t->data > curr->data) curr = curr->right; 
     else curr = curr->left; 
    } 

    if(t->data < parent->data) 
     parent->left = t; 
    else 
     parent->right = t; 
    } 
    } 

每个节点都有多个指针。这些指针将指向一个字符串数据。这些指针可能是动态的或根据您的需要进行修复。

使用标准库的平衡二叉树(std :: set,multiset,map,multimap)。使用一串字符串作为密钥,如

std::set<std::vector<std::string> > 

您可以在记录中添加一个数组或字符串向量。你必须有一个关键字符串来比较你的树的节点。使用字符串数组/矢量

struct t { 
    //other fields... 
    std::vector<std::string> data; 
}; 

的第一个元素插入

void BinarySearchTree::insert(string new_string, string key_string) 
    { 
    if(!key_string.empty()) 
    { 
     BinarySearchTree::tree_node *existing_node = BinarySearchTree::find(key_string); 
     if(existing_node) 
     { 
     existing_node->data.push_back(new_string); 
     } 
    }  
    else 
    { 
     tree_node* t = new tree_node; 
     tree_node* parent; 
     if(!key_string.empty()) 
     t->data.push_back(key_string); 
     if(!new_string.empty())   
     t->data.push_back(new_string); 
     t->left = NULL; 
     t->right = NULL; 
     parent = NULL; 

     // is this a new tree? 
     if(isEmpty()) root = t; 
     else 
     { 
     //Note: ALL insertions are as leaf nodes 
     tree_node* curr; 
     curr = root; 
     // Find the Node's parent 
     while(curr) 
     { 
      parent = curr; 
      if(t->data[0] > curr->data[0]) curr = curr->right; 
      else curr = curr->left; 
     } 

     if(t->data[0] < parent->data[0]) 
      parent->left = t; 
     else 
      parent->right = t; 
     } 
    } 
    } 

现在你可以 1.Insert一个新的字符串转换成基于关键字的现有节点。 2.仅通过提供new_string,用新关键字创建新节点。 3.让一个新节点同时插入关键字和另一个字符串。

不知道这是不是你想要的东西我不是一个真正的C++程序员,所以这可能是错误的代码...