Home > Back-end >  Why binary tree level traversal will appear this kind of mistake, wish you a big help
Why binary tree level traversal will appear this kind of mistake, wish you a big help

Time:11-11

 # include 
# define maxnum 100
using namespace std;

Typedef struct BTNode {
Char data;
Struct BTNode * lchild;
Struct BTNode * rchild;
} BTNode;
Void visite (BTNode * P)
{
Cout & lt;

}
//before sequence traversal
Void preorder (BTNode * P)
{
If (P!=NULL)
{
Visite (P);
Preorder (P - & gt; Lchild);
Preorder (P - & gt; Rchild);
}
}
//in the sequence traversal
Void inorder (BTNode * P)
{
If (P!=NULL)
{
Inorder (P - & gt; Lchild);
Visite (P);
Inorder (P - & gt; Rchild);
}
}
//after sequence traversal
Void postorder (BTNode * P)
{
If (P!=NULL)
{
Postorder (P - & gt; Lchild);
Postorder (P - & gt; Rchild);
Visite (P);
}
}
//level traversal
Void level (BTNode * p)
{
Int the front and rear;
Que BTNode * [maxnum];
The front rear of==0;
BTNode * q;
If (p!=NULL)
{
Rear=(rear + 1) % maxnum;
Que [rear]=p;
While (front!=rear)
{
The front=(front + 1) % maxnum;
Q=que (front),
Visite (q);
If (q - & gt; Lchild!=NULL)
{
Rear=(rear + 1) % maxnum;
Que (rear)=q - & gt; Lchild;
}
If (p - & gt; Rchild!=NULL)
{
Rear=(rear + 1) % maxnum;
Que (rear)=q - & gt; Rchild;
}
}
}
}
Int main ()
{
BTNode * node1=new BTNode;
BTNode * 2=new BTNode;
BTNode * node3=new BTNode;
BTNode * node4=new BTNode;
BTNode * node5=new BTNode;
Node1 - & gt; Data='https://bbs.csdn.net/topics/A'.
Node1 - & gt; Lchild=2;
Node1 - & gt; Rchild=node3;
2 - & gt; Data='https://bbs.csdn.net/topics/B'.
2 - & gt; Lchild=NULL;
2 - & gt; Rchild=NULL;
Node3 - & gt; Data='https://bbs.csdn.net/topics/C'.
Node3 - & gt; Lchild=node4;
Node3 - & gt; Rchild=node5;
Node4 - & gt; Data='https://bbs.csdn.net/topics/E'.
Node4 - & gt; Rchild=NULL;
Node4 - & gt; Lchild=NULL;
Node5 - & gt; Data='https://bbs.csdn.net/topics/D'.
Node5 - & gt; Lchild=NULL;
Node5 - & gt; Rchild=NULL;
Cout & lt; <"Big stick, please enter the order you want to choose the binary sort tree" & lt; Cout & lt; <"1: sort order 2: first ranking order 3: after the sort order 4: level traversal" & lt; Int gengzi;
Cin & gt;> Gengzi;
The switch (gengzi)
{
Case 1:
Cout & lt; <"Big stick the preorder traversal for you choose:";
Preorder (node1);
break;
Case 2:
Cout & lt; <"Big stick in your choice of sequence traversal is:";
Inorder (node1);
break;
Case 3:
Cout & lt; <"Big stick in your choice of sequence traversal is:";
Postorder (node1);
break;
Case 4:
Cout & lt; <"Big root level traversal for you choose:";
Level (node1);
break;
Default:
Cout & lt; <"Big root by what ah you in disorderly!!! ";
}
}




  • Related