二叉树只添加到根
Binary Tree only adding to the the root
我正在用 C++ 编写一个简单的二叉树程序,现在它只存储在根节点输入的最新值,例如。如果我在树中输入 10 然后在树中输入 9,9 只是覆盖 10 作为根节点,所以树只存储值 9.
我在网上查看了多个 C++ 二叉树解决方案并尝试了它们的实现版本,但我仍然没有成功。
这是树中单个节点的结构
struct TreeNode{
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int value){
this -> value = value;
left = NULL;
right = NULL;
}
};
到目前为止,我的 class 二叉树
class IntTree{
private :
TreeNode *root;
public :
IntTree();
TreeNode* getRoot();
void insertValue(TreeNode *root, int intValue);
TreeNode* searchTree(TreeNode *root, int intValue);
void inOrder(TreeNode *root);
void deleteValue(int intValue);
void deleteTree(TreeNode *root);
};
插入方法
void IntTree::insertValue(TreeNode *root, int intValue){
if(root == NULL){
root = new TreeNode(intValue);
}
else if(intValue == root->value){
cout << "Value already exists in the tree" << endl;
}
else if(intValue < root->value){
insertValue(root->left, intValue);
}
else{
insertValue(root->right, intValue);
}
}
然后在这样的菜单中简单地调用此方法
cout << "Enter Value to Insert : " << endl;
input = readInt();
theTree.insertValue(theTree.getRoot(), input);
逻辑对我来说似乎都很好,除了我试过不使用构造函数而只是单独设置变量,有两个函数用于插入一个只有 int 参数的函数,所以我不必稍后使用 getRoot() 和我忘记的其他一百万件事
答案很简单,你修改的指针只是一个副本,所以这个副本在函数结束时被丢弃,你失去了记忆。您需要引用指针才能实际修改它(没有其他修改):
void insertValue(TreeNode *& root, int intValue)
这应该有效:
新的 insertvalue 函数调用如下所示
void insertValue(TreeNode **root, int intValue)
{
if(*root == NULL)
{
*root = newNode(intValue);
}
else if(intValue == (*root)->value)
{
cout << "Value already exists in the tree" << endl;
}
else if(intValue < (*root)->value)
{
insertValue(&(*(root))->left, intValue);
}
else
{
insertValue(&(*(root))->right, intValue);
}
}
int main()
{
//initial code
insertvalue(&root,value) //root is a single pointer variable.
//code for printing the tree
}
有许多不太复杂的方法可以实现相同的目的。我刚刚修改了你的代码。
我正在用 C++ 编写一个简单的二叉树程序,现在它只存储在根节点输入的最新值,例如。如果我在树中输入 10 然后在树中输入 9,9 只是覆盖 10 作为根节点,所以树只存储值 9.
我在网上查看了多个 C++ 二叉树解决方案并尝试了它们的实现版本,但我仍然没有成功。
这是树中单个节点的结构
struct TreeNode{
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int value){
this -> value = value;
left = NULL;
right = NULL;
}
};
到目前为止,我的 class 二叉树
class IntTree{
private :
TreeNode *root;
public :
IntTree();
TreeNode* getRoot();
void insertValue(TreeNode *root, int intValue);
TreeNode* searchTree(TreeNode *root, int intValue);
void inOrder(TreeNode *root);
void deleteValue(int intValue);
void deleteTree(TreeNode *root);
};
插入方法
void IntTree::insertValue(TreeNode *root, int intValue){
if(root == NULL){
root = new TreeNode(intValue);
}
else if(intValue == root->value){
cout << "Value already exists in the tree" << endl;
}
else if(intValue < root->value){
insertValue(root->left, intValue);
}
else{
insertValue(root->right, intValue);
}
}
然后在这样的菜单中简单地调用此方法
cout << "Enter Value to Insert : " << endl;
input = readInt();
theTree.insertValue(theTree.getRoot(), input);
逻辑对我来说似乎都很好,除了我试过不使用构造函数而只是单独设置变量,有两个函数用于插入一个只有 int 参数的函数,所以我不必稍后使用 getRoot() 和我忘记的其他一百万件事
答案很简单,你修改的指针只是一个副本,所以这个副本在函数结束时被丢弃,你失去了记忆。您需要引用指针才能实际修改它(没有其他修改):
void insertValue(TreeNode *& root, int intValue)
这应该有效:
新的 insertvalue 函数调用如下所示
void insertValue(TreeNode **root, int intValue)
{
if(*root == NULL)
{
*root = newNode(intValue);
}
else if(intValue == (*root)->value)
{
cout << "Value already exists in the tree" << endl;
}
else if(intValue < (*root)->value)
{
insertValue(&(*(root))->left, intValue);
}
else
{
insertValue(&(*(root))->right, intValue);
}
}
int main()
{
//initial code
insertvalue(&root,value) //root is a single pointer variable.
//code for printing the tree
}
有许多不太复杂的方法可以实现相同的目的。我刚刚修改了你的代码。