我是一名大學生,作爲我的最終任務,我必須創建AVL樹詞典。我試圖自己寫,我已經寫了很多,但我有一個問題。當我將所有的獲取者和設置者用於隨機節點或者甚至是它們的矢量時,它都可以工作。但是當我試圖在Tree方法中設置Root時,它失敗了。我的意思是,它曾經工作過一次,但是一旦我嘗試在root中使用avl.getRoot或root進行調用,它就會失敗。我的程序崩潰了。這是我工作過的最難的程序。你能解決我的問題,並給我一些關於重要事情的提示嗎?先謝謝你。C++指針,參考和AVL樹
Main.cpp的 - 測試
Node n1("clown",1);
Node n2("cat",1);
Node n3("kid",1);
Node n4("wasp",1);
n1.setLSon(&n2);
std::cout<<"ENG: "<<n1.getLSon().getWord().getEng()<<std::endl;
n1.setRSon(&n3);
std::cout<<"ENG: "<<n1.getRSon().getWord().getEng()<<std::endl;
n1.setParent(&n4);
std::cout<<"ENG: "<<n1.getParent().getWord().getEng()<<std::endl;
if(n2.hasLSon)
n2.getLSon();
else
std::cout<<"n2 does not have a left son"<<std::endl;
AVL_Tree avl;
avl.addNode("cirrus",1);
avl.addNode("monkey",1);
std::cout<<"ENG: "<<avl.branches[0].getWord().getEng()<<std::endl;
std::cout<<"ENG: "<<avl.branches[1].getWord().getEng()<<std::endl;
avl.branches[0].setLSon(&avl.branches[1]);
std::cout<<"ENG: "<<avl.branches[0].getLSon().getWord().getEng()<<std::endl;
avl.branches[1].setParent(&avl.branches[0]);
std::cout<<"ENG: "<<avl.branches[1].getParent().getWord().getEng()<<std::endl;
/*Error is being called here*/
**std::cout<<"ROOT: "<<avl.getRoot().getWord().getEng()<<std::endl;**
}
樹類:有問題的功能
AVL_Tree::AVL_Tree()
{
root=NULL;
}
void AVL_Tree::sort()
{
}
Node AVL_Tree::getRoot()
{
return *root;
}
void AVL_Tree::addNode(std::string eng,int count)
{
int i=0;
branches.push_back(Node(eng,count));
for(i;i<branches.size();i++)
{
if(branches[i].getWord().getEng()==eng)
break;
}
if(branches.size()==1)
{
root=&(branches[i]);
std::cout<<"ROOT DODANY"<<endl;
std::cout<<root->getWord().getEng()<<std::endl;
}
else
std::cout<<"ROOTEM JEST: "<<root->getWord().getEng()<<std::endl;
if(!isBinary());
sort();
}
樹類
class AVL_Tree
{
public:
AVL_Tree();
void sort();
void addNode(std::string eng,int count);
void deleteNode(std::string eng);
Node findNode(std::string eng);
Node getRoot();
bool isBinary();
bool isNode(std::string eng);
std::vector<Node> branches;
private:
Node *root;
int leftFactor;
int rightFactor;
};
Node.cpp
Node::Node(std::string eng,int count):word(eng,count)
{
parent=NULL;
l_son=NULL;
r_son=NULL;
hasLSon=false;
hasRSon=false;
}
Node::~Node()
{
parent=NULL;
l_son=NULL;
r_son=NULL;
}
Word Node::getWord()
{
return word;
}
Node Node::getLSon()
{
return *l_son;
}
Node Node::getRSon()
{
return *r_son;
}
Node Node::getParent()
{
return *parent;
}
void Node::setLSon(Node *n)
{
l_son=n;
hasLSon=true;
}
void Node::setRSon(Node *n)
{
r_son=n;
hasRSon=true;
}
void Node::setParent(Node *n)
{
parent=n;
}
Node.h
class Node
{
public:
Node(std::string eng,int count);
~Node();
Word getWord();
Node getLSon();
Node getRSon();
Node getParent();
void setLSon(Node *node);
void setRSon(Node *node);
void setParent(Node *node);
bool hasLSon;
bool hasRSon;
private:
Node *parent;
Node *l_son;
Node *r_son;
Word word;
};
此代碼似乎並沒有反映二叉樹操作應該涉及的事實。首先爲二叉樹編寫實現可能會更好,然後將其擴展到AVL樹。 –
我知道,我讀過它。首先,我將把它寫成BST,然後使用函數對事物進行排序並使其成爲AVL – Advent
二元(搜索)樹(AVL樹,紅黑樹作爲特殊情況)不通過調用sort()函數進行排序因爲數據被插入到三個中,所以排序被保存在樹結構中。請參閱https://en.wikipedia.org/wiki/Binary_search_tree#Operations –