저는 AVL 트리뿐만 아니라 일반 이진 검색 트리를 구현하는 중입니다. 대부분 알아 냈지만 해결할 수없는 한 가지 문제가 있습니다. 드라이버를 컴파일하고 실행할 때 삽입이 실패합니다. 실행 또는 컴파일시 오류가 발생하지 않습니다. 단순히 삽입되지 않습니다. 필자는 BST 삽입 코드를 삽입 메소드에 붙여 넣기도했습니다. 아래에서 BST 구현과 함께 구현을 설명합니다. 어떤 도움이라도 완벽 할 것입니다! 반 지저분한 코드를 용서해주세요. 아직 잘 정리하지 않았습니까?AVL 삽입에 실패했습니다 (C++)
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);
인터넷에서 낯선 사람에게 코드를 덤프하고 마술처럼 수정 될 것이라고 기대하는 이유는 무엇입니까? 단계별 디버깅에 대한 좋은 구식 단계는 무엇입니까? –
글쎄, 나는 그걸 까먹었을 지 몰랐다. 나는 전에 여기에서 훨씬 더 안좋은 것을 보았으며 나는 여기에 게시하는 것을 처음으로 접했습니다. 나는 상속이 어떻게 작용했는지 확신 할 수 없었고 오류가 발생하지 않았기 때문에 그것이 옳았다 고 생각했습니다. 그래서 내가 한 디버깅은 나에게 아무 것도주지 않았다. 그리고 참고로, 그것은 그렇게 대답되었습니다 ... – dawkinsjh