0
我正在創建二叉搜索樹。我遇到了插入函數的問題。正如問題標題中所述,我的錯誤是指將新的初始化程序表達式列表視爲複合表達式。BST錯誤:將新的初始化程序表達式列表視爲複合表達式
更具體地說,我的插入函數的這一行代碼。
v = new treeNode(n , NULL, NULL);
我的代碼:
#ifndef BST_H
#define BST_H
#include <iostream>
template <class T>
class BST {
private:
struct treeNode
{
treeNode* left;
treeNode* right;
T data;
};
treeNode* root;
int count;
void insert(const T & n, treeNode * & v);
public:
BST();
~BST();
void push(const T & n);
void printPreOrder() const;
void preOrder(treeNode* pre) const;
bool search(const T & s);
bool empty() const;
int size() const;
};
template<class T>
BST<T>::BST()
{
root = NULL;
count = 0;
}
template<class T>
void BST<T>::push(const T & n)
{
insert(n,root);
}
template<class T>
void BST<T>::insert(const T & n, treeNode* & v)
{
if (v == NULL)
v = new treeNode(n , NULL, NULL);
else if (n < v->data)
push(n, v->left); // goes to left node
else if (v->data < n)
push(n, v->right); // goes to right node
else
; // duplicate; do nothing.
}
我明白它的意思通過複合式的,但它需要更多的東西一樣嗎?
if (v == NULL)
treeNode* v = new treeNode;
v->data = n;
v->left = NULL;
v->right = NULL;
拍好了,如果我知道它是那麼容易。我只是沒有完全理解錯誤的上下文。謝謝你的回答和你的時間!我會盡快接受答案! – MipsMoreLikeWhips 2014-10-29 20:51:59