C++ 如何深度优先打印a B+;树C++;?

C++ 如何深度优先打印a B+;树C++;?,c++,C++,我有一个订单5 Btree,包括插入、高度、查找和打印方法。我很难理解如何打印深度优先A-Z?我有一个打印功能将显示每个节点和其中的元素,但想了解如何按顺序打印。为了方便,我更喜欢AVL树 #include <iostream> #include <stdlib.h> #include <string> using namespace std; const int MAX = 4 ; //maximum # of keys const int MIN =

我有一个订单5 Btree,包括插入、高度、查找和打印方法。我很难理解如何打印深度优先A-Z?我有一个打印功能将显示每个节点和其中的元素,但想了解如何按顺序打印。为了方便,我更喜欢AVL树

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;


const int MAX = 4 ; //maximum # of keys
const int MIN = 2 ; //minimum # of keys

struct btnode  
{
    int count ;
    string value[MAX + 1] ;  //number of elements + 1;
    btnode *child[MAX + 1] ;
} ;

class btree   
{
    private :
        btnode *root ;
    public :
        btree( ) ;
        void insert ( string val ) ;
        int setValue ( string val, btnode *n, string *p, btnode **c ) ;
        static int searchNode ( string val, btnode *n, int *pos ) ;
        void fillNode ( string val, btnode *c, btnode *n, int k ) ;
        void split ( string val, btnode *c, btnode *n, int k, string *y, btnode **newnode ) ;
        void show( ) ;
        //void search(string key);
        int searchPos(string key,string *key_arr,int n);  //sub-function for search
        static void display ( btnode *root) ;
       // void height(string key);
} ;


btree :: btree( )
{
    root = NULL ;
}

int btree :: searchNode ( string val, btnode *n, int *pos )  //searches for string in node
{
    if ( val < n -> value[1] )
    {
        *pos = 0 ;
        return 0 ;
    }
    else
    {
        *pos = n -> count ;
        while ( ( val < n -> value[*pos] ) && *pos > 1 )
            ( *pos )-- ;
        if ( val == n -> value[*pos] )      //if value exists in the node returns 
        {    
            for(int j =0; j <= n->count; j++)
            {
                //cout<<"Test in searchmode function"<<endl;
                cout<<n->value[j]<<endl;
            }
        return 1 ;
        }   

        else
            return 0 ;
    }
}

void btree :: insert ( string val )  //inputting a string
{
    string in ;
    btnode *c, *n ;
    int flag ;
    flag = setValue ( val, root, &in, &c );  //in flag has value

    if ( flag )
    {
        n = new btnode ;            //create a new btnode
        n -> count = 1 ;            //initiates the count to 1
        n -> value[1] = in ;        //value of our node is set to the String passed in
        n -> child[0] = root ;      //setting pointer of child[0] to root
        n -> child[1] = c ;         
        root = n ;
    }
}
int btree :: setValue ( string val, btnode *n, string *p, btnode **ch )    //returns  1 or 0 to set flag
{
    int key ;

    if ( n == NULL )
    {
        *p = val ;
        *ch = NULL ;
        return 1 ;      //if root is NULL 1 is returned to flag and node is created
    }
    else
    {
        if ( searchNode ( val, n, &key ) )  //checks if the value has already been entered
            cout << endl << "Key value already exists." << endl ;

        if ( setValue ( val, n -> child[key], p, ch ) )  
        {
            if ( n -> count < MAX )   //if the node is less than MAX order the node is filled
            {
                fillNode ( *p, *ch, n, key ) ;      
                return 0 ;          //value is returned as 0 to flag    
            }
            else                    //if the MAX for node is met split
            {
                split ( *p, *ch, n, key, p, ch ) ;
                return 1 ;          //after split 1 is returned to flag to set node value
            }
        }
        return 0 ;
    }
}

void btree :: fillNode ( string val, btnode *c, btnode *n, int k )   //function to fill the node
{
    int i ;
    for ( i = n -> count ; i > k ; i-- )     //first node count is = to 1, k=0
    {
        n -> value[i + 1] = n -> value[i] ; 
        n -> child[i + 1] = n -> child[i] ;
    }
    n -> value[k + 1] = val ;       //assigns string to location in node
    n -> child[k + 1] = c ;         //assigns pointer to child
    n -> count++ ;                  //increments node count by one

}

void btree :: split ( string val, btnode *c, btnode *n,
        int k, string *y, btnode **newnode )
{
    int i, mid ;
    cout<<"In split function k is :"<<k;
    if ( k <= MIN )     
        mid = MIN ;     //Min is defined as 2, the minimum number in a node
    else
        mid = MIN + 1 ; //k passing as 3 or 4 will make the midpoint 3

    *newnode = new btnode ;

    for ( i = mid + 1 ; i <= MAX ; i++ )
    {
        ( *newnode ) -> value[i - mid] = n -> value[i] ;  //new node value is placed at i - mid point setting it as first position in split node
        ( *newnode ) -> child[i - mid] = n -> child[i] ;
    }

    ( *newnode ) -> count = MAX - mid ;
    n -> count = mid ;

    if ( k <= MIN )
        fillNode ( val, c, n, k ) ;
    else
        fillNode ( val, c, *newnode, k - mid ) ;  //fillNode is called and *newnode

    *y = n -> value[n -> count] ;                           //mid point moves up to root node
    ( *newnode ) -> child[0] = n -> child[n -> count] ;   
    n -> count-- ;
}


void btree :: show( )
{
    display ( root ) ;
}


void btree :: display ( btnode *root)  
{

    int i=0;
    if ( root != NULL )
    {

        for ( i = 0 ; i < root -> count ; i++ )
        {
               cout<< root->value[i]  ;
           display ( root -> child[i] ) ;

        }
       display ( root-> child[i] ) ;
        //cout<< root->value[i]  ;
    }



}

int main( )
{
    btree b ;
    int choice;
    string key;
    string t = "temp"; //to run through height function.
    do{
    cout<<"\n\n\tSELECT YOUR CHOICE\n\n1. insert\n2. search\n3. display\n4. height\n\n5. EXIT\n\n\n=";
    cin>>choice;
    switch(choice)
    {
                  case 1:cout<<"\n\nEnter the string to be inserted:";
                         cin>>key;
                         b.insert(key);
                         break;
                  case 2:cout<<"\n\nEnter search:";
                          cin>>key;
                         // b.search(key);
                          break;
                  case 3:
                    cout<<"could not get to print A-Z?!." <<endl;
                       b.show();
                       cout<<"\n";
                         break;
                    case 4:

                       //b.height(t);
                       cout<<"\n";
                         break;      

                 case 5:break;
                   default:cout<<"\nINVALID ENTRY";
                           break;};
                           }while(choice!=5);

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
常数int MAX=4//最大键数
常数int MIN=2//钥匙的最小数量
结构btnode
{
整数计数;
字符串值[MAX+1];//元素数+1;
btnode*子节点[MAX+1];
} ;
B类树
{
私人:
btnode*根;
公众:
btree();
无效插入(字符串val);
int setValue(字符串val,btnode*n,字符串*p,btnode**c);
静态int searchNode(字符串val、btnode*n、int*pos);
void fillNode(字符串val,btnode*c,btnode*n,int k);
无效拆分(字符串val、btnode*c、btnode*n、int k、字符串*y、btnode**newnode);
无效显示();
//无效搜索(字符串键);
int searchPos(string key,string*key_arr,int n);//用于搜索的子函数
静态无效显示(btnode*root);
//空隙高度(字符串键);
} ;
btree::btree()
{
root=NULL;
}
int-btree::searchNode(字符串val,btnode*n,int*pos)//在节点中搜索字符串
{
如果(valvalue[1])
{
*pos=0;
返回0;
}
其他的
{
*pos=n->count;
而((valvalue[*pos])&&*pos>1)
(*pos)--;
if(val==n->value[*pos])//如果节点中存在值,则返回
{    
对于(int j=0;j计数;j++)
{
//cout值[i];
n->child[i+1]=n->child[i];
}
n->value[k+1]=val;//将字符串分配给节点中的位置
n->child[k+1]=c;//将指针分配给child
n->count++;//将节点计数增加一
}
void btree::split(字符串val,btnode*c,btnode*n,
int k,字符串*y,btnode**newnode)
{
int i,mid;
cout count=mid;
if(k值[n->count];//中点上移到根节点
(*newnode)->child[0]=n->child[n->count];
n->计数--;
}
void btree::show()
{
显示(根);
}
void btree::display(btnode*root)
{
int i=0;
if(root!=NULL)
{
对于(i=0;icount;i++)
{
coutvalue[i];
显示(根->子[i]);
}
显示(根->子[i]);
//coutvalue[i];
}
}
int main()
{
b树b;
智力选择;
字符串键;
字符串t=“temp”;//用于运行高度函数。
做{
coutchoice;
开关(选择)
{
案例1:库特基;
b、 插入(键);
打破
案例2:库特基;
//b.搜索(键);
打破
案例3:

cout我调试代码的方法是:(a)使其非交互式,以便轻松运行测试;(b)添加打印函数和语句。我从
main()
中删除了“UI”代码,并将其替换为硬编码的内容

问题的一部分是
insert()
函数;它可能不会执行您认为它会执行的操作。特别是,它会将空树中的第一个值放入
value[1]
,而不是
value[0]
。我已经把这个问题的代码弄得一团糟;在你需要拆分一个节点之前,它会一直工作。在这一点上,它似乎丢失了一些数据

玩得开心

您应该检查将字符串作为引用而不是作为值传递;我已经修复了一些,但绝不是所有这些。我没有找到您需要的地方
;我添加了
和一些断言,但我没有得到断言

插入指令的代码
#包括
#包括
#包括
使用名称空间std;
常数int MAX=4;
常数int MIN=2;
结构btnode
{
整数计数;
字符串值[MAX+1];
btnode*子节点[MAX+1];
无效转储(ostream和out、const string和tag);
};
void btnode::dump(ostream&out、常量字符串和标记)
{
出子项[0]=n->子项[n->计数];
n->计数--;
}
void btree::show()
{
cout btree::insert('amaranthine')
-->>设置值:
新节点:
计数:1
值1=
show():
根不为空
值1=
设置值:
-->>设置值:
设置值:
设置值:
设置值:

显示方法似乎已经按顺序显示了结果。首先显示左侧,然后显示中间,然后显示右侧。请详细说明您发布的代码的具体问题。例如,它只显示4项。我输入一个b c d;它只输出一个b c。当我插入e并且节点拆分时,output=a dIf我使用cout after for loop.input=a b c d e output=a b d e c;c是拆分的中点,向上移动并最后打印?从头开始。在循环之后不应该使用任何东西。您完成了。循环完成了所有操作。您当前的代码会重新显示
子[0]
,出于某种原因,这是徒劳的。当我删除显示时(root->child[I])输入字母a-z并运行显示函数我的输出是:acdijlm当我画出树并将其拆分时,该函数只在树左侧的[1]处打印项目,而完全忽略树的右侧?循环外的行显示
root->child[root->count]
因为循环在
i==root->count
时停止。只有在
root->count
为零的情况下,它才会重新显示同一个节点。感谢您花时间研究此问题。它为我提供了一个开始修复的位置。非常感谢!
void btree :: display ( btnode *root)  
{
    int i=0;
    if ( root != NULL )
    {
        for ( i = 0 ; i < root -> count ; i++ )
        {
               cout<< root->value[i]  ;
           display ( root -> child[i] ) ;

        }
       display ( root-> child[i] ) ;
        //cout<< root->value[i]  ;
    }
}
#include <cassert>
#include <iostream>
#include <string>

using namespace std;

const int MAX = 4;
const int MIN = 2;

struct btnode
{
    int count;
    string value[MAX + 1];
    btnode *child[MAX + 1];
    void dump(ostream &out, const string &tag);
};

void btnode::dump(ostream &out, const string &tag)
{
    out << "-->> " << tag << ":\n";
    out << "Count:   " << count << "\n";
    assert(value[0] == "");
    for (int i = 1; i <= count; i++)
        out << "Value " << i << " = <<" << value[i] << ">>\n";
    out << "<<-- " << tag << "\n";
}

class btree
{
    private:
        btnode *root;
    public:
        btree() : root(0) {}
        void insert(const string &val);
        int setValue(const string &val, btnode *n, string *p, btnode **c);
        void fillNode(string val, btnode *c, btnode *n, int k);
        void split(string val, btnode *c, btnode *n, int k, string *y, btnode **newnode);
        void show();
        int searchPos(string key,string *key_arr,int n);
        static void display(btnode *root);
        static int searchNode(const string &val, btnode *n, int *pos);
};

int btree::searchNode(const string &val, btnode *n, int *pos)
{
    if (val < n->value[1])
    {
        *pos = 0;
        return 0;
    }
    else
    {
        *pos = n->count;
        while ((val < n->value[*pos]) && *pos > 1)
            (*pos)--;
        if (val == n->value[*pos])
        {
            for(int j =0; j <= n->count; j++)
            {
                cout << n->value[j] << endl;
            }
            return 1;
        }
        else
            return 0;
    }
}

void btree::insert(const string &val)
{
    cout << "-->> btree::insert('" << val << "')\n";
    string in = "";
    btnode *c = 0;
    int flag = setValue(val, root, &in, &c);
    cout << "setValue set flag = " << flag << "\n";

    if (flag)
    {
        cout << "Creating new node\n";
        btnode *n = new btnode;
        assert(n != 0);
        n->count = 1;
        n->value[0] = "";
        n->value[1] = in;
        n->child[0] = root;
        n->child[1] = c;
        n->dump(cout, "New node");
        root = n;
        this->show();
    }
    cout << "<<-- btree::insert()\n";
}

int btree::setValue(const string &val, btnode *n, string *p, btnode **ch)
{
    cout << "-->> setValue: <<" << val << ">>\n";
    if (n == NULL)
    {
        *p = val;
        *ch = NULL;
        cout << "<<-- setValue: 1A\n";
        return 1;
    }
    else
    {
        int key;
        if (searchNode(val, n, &key))
            cout << endl << "Key value already exists." << endl;

        if (setValue(val, n->child[key], p, ch))
        {
            if (n->count < MAX)
            {
                fillNode(*p, *ch, n, key);
                cout << "<<-- setValue: 0A\n";
                return 0;
            }
            else
            {
                split(*p, *ch, n, key, p, ch);
                cout << "<<-- setValue: 1B\n";
                return 1;
            }
        }
        cout << "<<-- setValue: 0B\n";
        return 0;
    }
}

void btree::fillNode(string val, btnode *c, btnode *n, int k)
{
    for (int i = n->count; i > k; i--)
    {
        n->value[i + 1] = n->value[i];
        n->child[i + 1] = n->child[i];
    }
    n->value[k + 1] = val;
    n->child[k + 1] = c;
    n->count++;
}

void btree::split(string val, btnode *c, btnode *n,
        int k, string *y, btnode **newnode)
{
    int i, mid;
    cout << "In split function k is: " << k << "\n";
    if (k <= MIN)
        mid = MIN;
    else
        mid = MIN + 1;

    *newnode = new btnode;

    for (i = mid + 1; i <= MAX; i++)
    {
        (*newnode)->value[i - mid] = n->value[i];
        (*newnode)->child[i - mid] = n->child[i];
    }

    (*newnode)->count = MAX - mid;
    n->count = mid;

    if (k <= MIN)
        fillNode(val, c, n, k);
    else
        fillNode(val, c, *newnode, k - mid);

    *y = n->value[n->count];
    (*newnode)->child[0] = n->child[n->count];
    n->count--;
}

void btree::show()
{
    cout << "-->> show():\n";
    display(root);
    cout << "<<-- show()\n";
}

void btree::display(btnode *root)
{
    if (root != NULL)
    {
        cout << "Root is not null\n";
        int i;
        for (i = 1; i <= root->count; i++)
        {
            cout << "Value " << i << " = <<" << root->value[i] << ">>\n";
            if (root->child[i-1])
                display(root->child[i-1]);
        }
        if (root->child[i])
            display(root->child[i]);
    }
    else
        cout << "Root is null\n";
}

int main()
{
    btree b;

    string key = "amaranthine";
    b.insert(key);
    b.show();

    key = "absinthe";
    b.insert(key);
    b.show();

    key = "amazonia";
    b.insert(key);
    b.show();

    key = "zululand";
    b.insert(key);
    b.show();

    key = "asbestos";
    b.insert(key);
    b.show();

    return 0;
}
-->> btree::insert('amaranthine')
-->> setValue: <<amaranthine>>
<<-- setValue: 1A
setValue set flag = 1
Creating new node
-->> New node:
Count:   1
Value 1 = <<amaranthine>>
<<-- New node
-->> show():
Root is not null
Value 1 = <<amaranthine>>
<<-- show()
<<-- btree::insert()
-->> show():
Root is not null
Value 1 = <<amaranthine>>
<<-- show()
-->> btree::insert('absinthe')
-->> setValue: <<absinthe>>
-->> setValue: <<absinthe>>
<<-- setValue: 1A
<<-- setValue: 0A
setValue set flag = 0
<<-- btree::insert()
-->> show():
Root is not null
Value 1 = <<absinthe>>
Value 2 = <<amaranthine>>
<<-- show()
-->> btree::insert('amazonia')
-->> setValue: <<amazonia>>
-->> setValue: <<amazonia>>
<<-- setValue: 1A
<<-- setValue: 0A
setValue set flag = 0
<<-- btree::insert()
-->> show():
Root is not null
Value 1 = <<absinthe>>
Value 2 = <<amaranthine>>
Value 3 = <<amazonia>>
<<-- show()
-->> btree::insert('zululand')
-->> setValue: <<zululand>>
-->> setValue: <<zululand>>
<<-- setValue: 1A
<<-- setValue: 0A
setValue set flag = 0
<<-- btree::insert()
-->> show():
Root is not null
Value 1 = <<absinthe>>
Value 2 = <<amaranthine>>
Value 3 = <<amazonia>>
Value 4 = <<zululand>>
<<-- show()
-->> btree::insert('asbestos')
-->> setValue: <<asbestos>>
-->> setValue: <<asbestos>>
<<-- setValue: 1A
In split function k is: 3
<<-- setValue: 1B
setValue set flag = 1
Creating new node
-->> New node:
Count:   1
Value 1 = <<amazonia>>
<<-- New node
-->> show():
Root is not null
Value 1 = <<amazonia>>
Root is not null
Value 1 = <<absinthe>>
Value 2 = <<amaranthine>>
<<-- show()
<<-- btree::insert()
-->> show():
Root is not null
Value 1 = <<amazonia>>
Root is not null
Value 1 = <<absinthe>>
Value 2 = <<amaranthine>>
<<-- show()