简体   繁体   中英

c++ binary search tree error with root children

I'm trying to build a binary search tree with words. However, when I use the code above , I can only reach my root, root's left and right children seem to be null.

Code:

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);

    }

}

}

Here is custom binary tree code example.

// 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

CTreeNode implementation

#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);
    }
}

CBinaryTree declaration

// 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__ */

CBinaryTree implementation

#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());
}

in your case

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;
           }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM