AVL 无法插入 (C++)
AVL failing to insert (c++)
我一直在努力实现一个普通的二叉搜索树和一个AVL树。我已经基本弄清楚了,但有一个问题我似乎无法解决。当我编译并运行驱动程序时,插入失败。运行或编译时没有错误;它根本不插入。我什至将 BST 插入中的代码粘贴到插入方法中,结果相同。我将把实现与BST实现一起放在下面。任何帮助都是完美的!原谅我半混乱的代码。还没有清理好。
BST Definition
class BSTree {
BSTNode *root;
public:
//constructors
BSTree();
BSTree(int);
//public members
bool isEmpty(); //check if the bst is empty
void insert(int newValue); //inserts an int into the bst. Returns success
bool find(int findMe); //searches bst for int. True if int is in tree
void preorder(); //calls recursive transversal
void inorder(); //calls recursive traversal
void postorder(); //calls recursive transversal
int height(BSTNode *n); // given node only.
int height(); //returns height of whatever node is passed in.
int totalheight(); //returns tot height. height of empty tree = -1
int avgheight(); //returns avg height of tree
int totaldepth(); //returns tot depth. depth of empty tree = -1
int avgdepth(); //returns avg depth of tree
bool remove(int value); //deletes int. returns true if deleted
private:
int depth(BSTNode *n, int count); //depth of node. recursive.
int counter(BSTNode *n); //called by other functions for counting nodes
int totalheight(BSTNode *n); //called from public totalheight
int totaldepth(BSTNode *n, int depth);
int avgheight(BSTNode *n, int th);
bool findRecursive(struct BSTNode *root, int findMe); //called from public find
struct BSTNode* insertRecursive(struct BSTNode *n, int newValue);
void inorderRecursive(BSTNode *n); //traverses tree in inorder
void preorderRecursive(BSTNode *n); //traverses tree in preorder
void postorderRecursive(BSTNode *n); //traverses tree in preorder
};
//----------------------Constructor---------------------------
BSTree::BSTree(){
root = NULL;
} // BSTree
//root value given
BSTree::BSTree(int value){
root = new BSTNode(value);
} //BSTree(int)
//--------------------------insert-------------------------
void BSTree::insert(int newValue){
root = insertRecursive(root,newValue);
} //insert
struct BSTNode* BSTree::insertRecursive(struct BSTNode *n,int newValue){
//base case
if(n==NULL)
return new BSTNode(newValue);
else if (newValue < n->value) {
n->left = insertRecursive(n->left,newValue);
}
else if (newValue > n->value) {
n->right = insertRecursive(n->right,newValue);
}
else; //duplicate: do nothing
return n;
} //insertRecursive
//--------------------------Call in main-------------------------
BSTree *t = new BSTree();
t->insert(50);
请记住,这非常有效。
AVL
class AVLTree : public BSTree {
BSTNode *root;
public:
//constructors
AVLTree(); //given nothing
AVLTree(int value); //giving root value
//member methods
void insert(int newValue);
private:
struct BSTNode* insert(BSTNode *n, int newValue);
struct BSTNode* rotateLeft(BSTNode *n);
struct BSTNode* rotateRight(BSTNode *n);
struct BSTNode* doubleLeft(BSTNode *n);
struct BSTNode* doubleRight(BSTNode *n);
};
//--------------------------constructors-----------------------
AVLTree::AVLTree(){
root = NULL;
} // AVLTree
//root value given
AVLTree::AVLTree(int value){
root = new BSTNode(value);
} //AVLTree(int)
我不会显示旋转方法,因为即使没有它们和正常的 BST 代码,它仍然不起作用。
//------------------------------insert------------------------
void AVLTree::insert(int newValue){
root = insert(root, newValue);
}//insert
struct BSTNode* AVLTree::insert(BSTNode* n, int newValue){
if (n == NULL){ //if we are at end of tree. Insert.
return new BSTNode(newValue);
}//if
else if (newValue < n->value) { //move left if newValue smaller than n->value
n->left = insert(n->left,newValue);
if (height(n->left) - height(n->right) == 2){
if(newValue < n->left->value)
n = rotateLeft(n);
else
n = doubleLeft(n);
}//if == 2
}//else if
else if (newValue > n->value) { //move right if newValue bigger
n->right = insert(n->right,newValue);
if (height(n->right) - height(n->left) == 2){
if(newValue > n->right->value)
n = rotateRight(n);
else
n = doubleRight(n);
}//if == 2
}//else if
else; //duplicate. Do nothing.
n->height = max(height(n->left),height(n->right)) + 1;
return n;
}//insert
//--------------------call in main ---------------------------
AVLTree *a = new AVLTree();
a->insert(50);
您有两个root
变量。
您的AVLTree
有自己的root
成员变量,该变量用于AVLTree
的方法。
但是,BSTree
的方法使用 BSTree
中声明的root
变量。
从 AVLTree
中删除不必要的变量。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在数字之间插入 + 或 - 符号以使其等于整数
- 在字符串中插入空格
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?