【发布时间】:2015-07-29 03:25:13
【问题描述】:
我正在尝试用 C++ 编写一个 AVL 树类,我只是从编写普通 BST 的代码开始,但我遇到了一个问题。我遇到的问题是我的插入功能。我尝试将元素插入到树中,但实际上并没有这样做。我不太确定它为什么不这样做,我的直觉是我正在从函数中更改树,但我没有做任何事情来保存这些更改,我不知道该怎么做那个。
#ifndef AVLTREE_H
#define AVLTREE_H
#include <iostream>
template <class K, class V>
struct AVLNode{
K Key;
V Value;
AVLNode<K,V> *left;
AVLNode<K,V> *right;
};
template <class K, class V>
class AVLTree{
public:
AVLTree();
~AVLTree();
void insert(const K& Key, const V& Value);
void print_AVL();
private:
void print_AVL2(AVLNode<K,V> *node);
void insert2(AVLNode<K,V> *node, const K& Key, const V& Value);
AVLNode<K,V> *root;
};
template <class K, class V>
AVLTree<K,V>::AVLTree(){
root = nullptr;
}
template <class K, class V>
AVLTree<K,V>::~AVLTree(){
delete root;
}
template <class K, class V>
void AVLTree<K,V>::insert(const K& Key, const V& Value){
std::cout << "Trying to insert " << Key << ", " << Value << std::endl;
insert2(root, Key, Value);
}
template <class K, class V>
void AVLTree<K,V>::insert2(AVLNode<K,V> *n, const K& Key, const V& Value){
std::cout << n << std::endl;
if(n== nullptr){
n = new AVLNode<K,V>;
n->Key = Key;
n->Value = Value;
n->parent = nullptr;
n->left = nullptr;
n->right = nullptr;
}
else if(n->Key > Key){
insert2(n->left, Key, Value);
}
else{
insert2(n->right, Key, Value);
}
std::cout << n << std::endl;
}
template <class K, class V>
void AVLTree<K,V>::print_AVL(){
print_AVL2(root);
}
template <class K, class V>
void AVLTree<K,V>::print_AVL2(AVLNode<K,V> *n){
std::cout << n << std::endl;
if(n == nullptr){
return;
}
print_AVL2(n->left);
std::cout << "Name, ID: " << n->Value << ", " << n->Key << std::endl;
print_AVL2(n->right);
}
#endif
我的 Main 函数如下所示:
#include "AVLTree.hpp"
#include <iostream>
int main()
{
AVLTree<std::string,std::string> Tree;
Tree.insert("Hello","World");
Tree.print_AVL();
return 0;
}
【问题讨论】:
-
n = new AVLNode<K,V>;不会做调用者期望的事情,除非你通过引用传递n(如引用指针)。现在它是内存泄漏的秘诀。 -
parent中的insert2()是什么? -
我希望树中的每个节点都有一个父指针,指向它所属的节点
-
@OmarMorales 然后可能在
AVLNode中声明它,而不是保持原样,这会导致未知成员编译时错误。无论如何,我之前的评论是有效的。
标签: c++ class binary-search-tree avl-tree root-node