C++ c++;具有根子级的二叉搜索树错误
我正在尝试用单词建立一个二叉搜索树。然而,当我使用上面的代码时,我只能到达我的根,根的左和右子级似乎为空 代码:C++ c++;具有根子级的二叉搜索树错误,c++,search,tree,binary-tree,binary-search-tree,C++,Search,Tree,Binary Tree,Binary Search Tree,我正在尝试用单词建立一个二叉搜索树。然而,当我使用上面的代码时,我只能到达我的根,根的左和右子级似乎为空 代码: void NgramTree::insert(std::string str) { if(root==NULL) { root=new node(str,1); } else{ // checkAndIncrease method checks if its already in tree and counts, thi
void NgramTree::insert(std::string str)
{
if(root==NULL)
{
root=new node(str,1);
}
else{
// checkAndIncrease method checks if its already in tree and counts, this method works perfect too. I ve been sure , its going in if block below after first insertion.
bool have=checkAndIncease(root,str);
if(have==false)
{
node* cur=root;
while(cur!=NULL)
{
// first method returns 1 if first arg is smaller,0 if equal 1 if bigger and works perfectly!
int upper=first(cur->data,str,0);
if(upper==1)
{
cur=cur->right;
}
if(upper==0)
{
std::cout<< " hata var";
}
if(upper==-1)
{
cur=cur->left;
std::cout<< "cur=cur->left;\n";
}
}
/// WHEN I RUN PROGRAM, I CAN BE SURE CUR== ROOT->LEFT
if(cur==(root->left))
{
std::cout<< "cur==root->left DOGRUU\n";
}
// Although, cur==root->left, if i use cur here
// They arent connected, both childerens of root seems NULL
// If i do root->left=new Node(str,1) instead of cur just for try
// It works only for one insertion..
cur=new node(str,1);
}
}
}
void NgramTree::insert(std::string str)
{
if(root==NULL)
{
根=新节点(str,1);
}
否则{
//checkAndIncrease方法检查它是否已经在树中并计数,这个方法也可以完美地工作。我已经确定,它在第一次插入后将进入下面的if块。
bool have=checkandincase(根,str);
如果(have==false)
{
节点*cur=根;
while(cur!=NULL)
{
//如果第一个参数较小,则第一个方法返回1;如果等于0,则返回0;如果较大,则返回1,并且工作正常!
int upper=第一个(当前->数据,str,0);
如果(上限==1)
{
cur=cur->right;
}
如果(上限==0)
{
std::coutleft;
库特利夫特
如果(cur==(根->左))
{
coutleft,如果我在这里使用cur
//它们没有连接,根的两个子元素似乎都为空
//如果我做root->left=新节点(str,1)而不是cur,只是为了尝试
//它只适用于一次插入。。
cur=新节点(str,1);
}
}
}
以下是自定义二叉树代码示例
// TreeNode.h
#include <stdlib.h>
#include <string>
#ifndef __TREE_NODE_H__
#define __TREE_NODE_H__
class CTreeNode
{
public:
CTreeNode(std::string str);
~CTreeNode();
////////////////////////////////////////////////////////////
const CTreeNode* GetLeft() const;
const CTreeNode* GetRight() const;
std::string GetString() const;
void SetValue(std::string str);
private:
CTreeNode* m_pLeft;
CTreeNode* m_pRight;
std::string m_Str;
};
#endif
//TreeNode.h
#包括
#包括
#ifndef树节点__
#定义树节点__
类中心节点
{
公众:
CtreNode(std::string str);
~CTreeNode();
////////////////////////////////////////////////////////////
常量CtreNode*GetLeft()常量;
常量CtreNode*GetRight()常量;
std::string GetString()常量;
void设置值(std::string str);
私人:
CTreeNode*m_pLeft;
CTreeNode*m_pRight;
std::字符串m_Str;
};
#恩迪夫
CtreNode实现
#include "TreeNode.h"
CTreeNode::CTreeNode(int iValue, std::string str)
{
m_pLeft = NULL;
m_pRight = NULL;
m_Str = str;
}
CTreeNode::~CTreeNode()
{
delete m_pLeft;
m_pLeft = NULL;
delete m_pRight;
m_pRight = NULL;
}
const CTreeNode* CTreeNode::GetLeft() const
{
return m_pLeft;
}
const CTreeNode* CTreeNode::GetRight() const
{
return m_pRight;
}
std::string CTreeNode::GetString() const
{
return m_Str;
}
void CTreeNode::SetValue(std::string str)
{
if (str.compare(m_Str) < 0)
{
if (m_pLeft != NULL)
m_pLeft->SetValue(str);
else
m_pLeft = new CTreeNode(str);
}
else
{
if (m_pRight != NULL)
m_pRight->SetValue(str);
else
m_pRight = new CTreeNode(str);
}
}
#include "BinaryTree.h"
using std::endl;
using std::cout;
CBinaryTree::CBinaryTree()
{
m_pRoot = NULL;
}
CBinaryTree::~CBinaryTree()
{
delete m_pRoot;
m_pRoot = NULL;
}
void CBinaryTree::Add(std::string str)
{
if (m_pRoot != NULL)
m_pRoot->SetValue(str);
else
m_pRoot = new CTreeNode(str);
}
void CBinaryTree::PrintLR() const
{
PrintLR(m_pRoot);
}
void CBinaryTree::PrintLR(const CTreeNode* pNode) const
{
if (pNode == NULL)
return;
PrintLR(pNode->GetLeft());
cout << pNode->GetString() << endl;
PrintLR(pNode->GetRight());
}
#包括“TreeNode.h”
CtreNode::CtreNode(intivalue,std::string str)
{
m_pLeft=NULL;
m_pRight=NULL;
m_Str=Str;
}
CTreeNode::~CTreeNode()
{
删除m_pLeft;
m_pLeft=NULL;
删除m_pRight;
m_pRight=NULL;
}
常量CTreeNode*CTreeNode::GetLeft()常量
{
返回m_pLeft;
}
常量CTreeNode*CTreeNode::GetRight()常量
{
返回m_pRight;
}
std::string CtreNode::GetString()常量
{
返回m_Str;
}
void CtreNode::SetValue(std::string str)
{
如果(str.compare(m_str)<0)
{
如果(m_pLeft!=NULL)
m_pLeft->SetValue(str);
其他的
m_pLeft=新的中心节点(str);
}
其他的
{
如果(m_pRight!=NULL)
m_pRight->SetValue(str);
其他的
m_pRight=新的中心节点(str);
}
}
CBinaryTree声明
// BinaryTree.h
#include "TreeNode.h"
#include <iostream>
#ifndef __BINARY_TREE_H__
#define __BINARY_TREE_H__
class CBinaryTree
{
public:
CBinaryTree();
~CBinaryTree();
////////////////////////////////////////////////////////////
void Add(std::string str);
void PrintLR() const;
private:
void PrintLR(const CTreeNode* pNode) const;
CTreeNode* m_pRoot;
};
#endif /* __BINARY_TREE_H__ */
//BinaryTree.h
#包括“TreeNode.h”
#包括
#ifndef uu二叉树__
#定义二叉树__
类CBinaryTree
{
公众:
CBinaryTree();
~CBinaryTree();
////////////////////////////////////////////////////////////
voidadd(std::stringstr);
void PrintLR()常量;
私人:
无效打印lr(常数CTreeNode*pNode)常数;
CTreeNode*m_pRoot;
};
#endif/*.\uuu二叉树\u H\uu*/
CBinaryTree实现
#include "TreeNode.h"
CTreeNode::CTreeNode(int iValue, std::string str)
{
m_pLeft = NULL;
m_pRight = NULL;
m_Str = str;
}
CTreeNode::~CTreeNode()
{
delete m_pLeft;
m_pLeft = NULL;
delete m_pRight;
m_pRight = NULL;
}
const CTreeNode* CTreeNode::GetLeft() const
{
return m_pLeft;
}
const CTreeNode* CTreeNode::GetRight() const
{
return m_pRight;
}
std::string CTreeNode::GetString() const
{
return m_Str;
}
void CTreeNode::SetValue(std::string str)
{
if (str.compare(m_Str) < 0)
{
if (m_pLeft != NULL)
m_pLeft->SetValue(str);
else
m_pLeft = new CTreeNode(str);
}
else
{
if (m_pRight != NULL)
m_pRight->SetValue(str);
else
m_pRight = new CTreeNode(str);
}
}
#include "BinaryTree.h"
using std::endl;
using std::cout;
CBinaryTree::CBinaryTree()
{
m_pRoot = NULL;
}
CBinaryTree::~CBinaryTree()
{
delete m_pRoot;
m_pRoot = NULL;
}
void CBinaryTree::Add(std::string str)
{
if (m_pRoot != NULL)
m_pRoot->SetValue(str);
else
m_pRoot = new CTreeNode(str);
}
void CBinaryTree::PrintLR() const
{
PrintLR(m_pRoot);
}
void CBinaryTree::PrintLR(const CTreeNode* pNode) const
{
if (pNode == NULL)
return;
PrintLR(pNode->GetLeft());
cout << pNode->GetString() << endl;
PrintLR(pNode->GetRight());
}
#包括“BinaryTree.h”
使用std::endl;
使用std::cout;
CBinaryTree::CBinaryTree()
{
m_pRoot=NULL;
}
CBinaryTree::~CBinaryTree()
{
删除m_pRoot;
m_pRoot=NULL;
}
void CBinaryTree::Add(std::string str)
{
如果(m_pRoot!=NULL)
m_pRoot->SetValue(str);
其他的
m_pRoot=新中心节点(str);
}
void CBinaryTree::PrintLR()常量
{
PrintLR(m_pRoot);
}
void CBinaryTree::PrintLR(常量CTreeNode*pNode)常量
{
if(pNode==NULL)
返回;
PrintLR(pNode->GetLeft());
cout GetString()GetRight());
}
就你而言
void NgramTree::insert(std::string str)
{
if(root==NULL)
{
root=new node(str,1);
}
else{
// checkAndIncrease method checks if its already in tree and counts, this method works perfect too. I ve been sure , its going in if block below after first insertion.
bool have=checkAndIncease(root,str);
if(have==false)
{
node* cur=root;
while(cur!=NULL)
{
// first method returns 1 if first arg is smaller,0 if equal 1 if bigger and works perfectly!
int upper=first(cur->data,str,0);
if(upper==1)
{
if (cur->right == NULL)
{
cur->right = new node(str, 1)
break;
}
cur=cur->right;
}
else if(upper==0)
{
std::cout<< " hata var";
}
else
{
if (cur->left == NULL)
{
cur->left = new node(str, 1)
break;
}
cur=cur->left;
}
}
}
}
void NgramTree::insert(std::string str)
{
if(root==NULL)
{
根=新节点(str,1);
}
否则{
//checkAndIncrease方法检查它是否已经在树中并计数,这个方法也可以完美地工作。我已经确定,它在第一次插入后将进入下面的if块。
bool have=checkandincase(根,str);
如果(have==false)
{
节点*cur=根;
while(cur!=NULL)
{
//如果第一个参数较小,则第一个方法返回1;如果等于0,则返回0;如果较大,则返回1,并且工作正常!
int upper=第一个(当前->数据,str,0);
如果(上限==1)
{
if(cur->right==NULL)
{
cur->right=新节点(str,1)
打破
}
cur=cur->right;
}
else if(上限==0)
{
std::coutleft==NULL)
{
cur->left=新节点(str,1)
打破
}
cur=cur->left;
}
}
}
}
谢谢,但我想实现我自己的树。这是我第一次实现树,我需要自己去做。但是,我不认为我的代码是错误的,为什么它不工作??你的代码有一些逻辑错误。我已经更新了我的答案。再次非常感谢,但我已经尝试了你更改的代码,但我仍然有相同的错误:(顺便说一句,不要忘记将问题标记为已回答。)