0

我正在研究一个二叉树程序,当调用我的插入函数时,它似乎插入了正在输入的值,而在检查该值时,我什么也没有得到,现在我很难过,我可能遗漏了什么或有什么问题?我已经提供了类数据和下面的插入/搜索函数定义,我只是觉得这与我的模板实现有关,或者只是模板的一般帮助?

struct node
{
 string keyValue; //value to stored that is being searched for in our tree
 node *left; //left side of pointing tree from parent node
 node *right; //right side of pointing tree from parent node

};
template <class T> 
class Tree
{
  public: 
   Tree(); //constructor
  ~Tree(); //destructor

void displayTree();     
void insertTree(T key);
node *searchTree(T key);
void deleteTree();

private:

 node *root; //points to the root point of our tree
 void displayTree(node *leaf);
 void insertTree(T key, node *leaf); //takes in our keyValue to 
 void deleteTree(node*leaf);
 node *search(T key, node *leaf);
};

 template <class T>
 void Tree<T>::insertTree(T key)
 {
  cout << "instert1" << endl;
   if(root != NULL)
   {
    cout << "instert2" << endl;
    insertTree(key, root);
   }
   else
   {
    cout << "inster else..." << endl;
    root = new node;
    root->keyValue = key;
    cout << root->keyValue << "--root key value" << endl;
    root->left = NULL;
    root->right = NULL;
 }   

}

template <class T>      
void Tree<T>::insertTree(T key, node *leaf)
{
 if(key < leaf->keyValue)
 {
   if(leaf->left != NULL)
   { 
    insertTree(key, leaf->left); 
   }
   else //descend tree to find appropriate NULL node to store keyValue (left side of tree)
   {

     leaf->left = new node; //Creating new node to store our keyValue (data)
     leaf->left -> keyValue = key;
     leaf->left -> left = NULL; //Assigning left and right child of current child node to NULL
     leaf->left -> right = NULL;
   }
  }
   else if(key >= leaf->keyValue)
   {
     if(leaf->right != NULL)
     {
      insertTree(key, leaf->right);
     }
     else //descend tree to find appropriate NULL node to store keyValue (right side of tree)
     {
      leaf->right = new node; //Creating new node to store our keyValue (data)
      leaf->right -> keyValue = key;
      leaf->right -> right = NULL; //Assigning left and right child of current child node to NULL
      leaf->right -> left = NULL;
    }
  }     
}
    template <class T>
node *Tree<T>::searchTree(T key)
{
 cout << "searching for...key: " << key << " and given root value:" << endl;
  return search(key, root);
}
template <class T>
node *Tree<T>::search(T key, node*leaf)
{
  if(leaf != NULL)
  {
    cout << "check passed for search!" << endl;
    if(key == leaf->keyValue)
    {
       return leaf;
    }
    if(key < leaf->keyValue)
    {
      return search(key, leaf->left);
    }
    else
    {
      return search(key, leaf->right);
    }

  }

  else 
  {
    cout << key << " Not found...!" << endl;
    return NULL;
  }
}
4

1 回答 1

0

您的代码似乎有效。你可以看这里:http: //ideone.com/w9Aa1w

你可能需要做的,这就是我所做的,是模板化你的节点结构,就像这样......

template <typename T>
struct NodeStruct
{
 T keyValue; //value to stored that is being searched for in our tree
 NodeStruct<T> *left; //left side of pointing tree from parent node
 NodeStruct<T> *right; //right side of pointing tree from parent node
};

然后在你的树类typedef中你的新节点类型只是'node':

typedef NodeStruct<T> node;

它似乎插入正确。

将来,如果您更清楚自己遇到的确切问题会有所帮助,而不是仅仅发布整个程序,而是至少确定您遇到问题的代码小节。

这个问题太模糊了,作为参考资料可能对其他人没有帮助。

于 2013-10-10T04:08:05.337 回答