Home > Back-end >  Rookie help binary tree data structure
Rookie help binary tree data structure

Time:11-17

Run time has no error, no warning, but no output after the input data, don't know why orz
Bosses, please help to look at, thank you orz

The header file
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Using namespace STD.

# define TRUE 1
# define FALSE 0
# define OVERFLOW 0
# define OK 1
# define the ERROR 0

300//# define STACK_INIT_SIZE initialization storage space allocation amount
# define STACKINCREMENT 10//storage space allocation increment
# define MAXLEN 10
Typedef int the Status;

Typedef char TElemType;

Typedef struct BiTNode {
TElemType data;
Struct lchild BiTNode * and * rchild;
} BiTNode, * BiTree;

Typedef BiTree ElemType;

Typedef struct
{
ElemType * base;
ElemType * top;//the stack pointer
Int stacksize;
}SqStack;

//declare
The Status InitStack (SqStack & amp; S);//initialize
The Status of Push (SqStack & amp; S, ElemType e);//insert to stack element
The Status of Pop (SqStack & amp; S);//the stack
The Status StackEmpty (SqStack & amp; S);
The Status GetTop (SqStack S, ElemType & amp; E);
The Status StackEmpty (SqStack & amp; S);

The Status CreateBiTree (BiTree & amp; T, char STR []);
The Status PreOrderTraverse (BiTree T);//first sequence traversal
The Status InOrderTraverse (BiTree T);//in the sequence traversal
The Status PostOrderTraverse (BiTree T);//after sequence traversal
The Status SqStackInOrderTraverse BiTree (T);//use the stack non-recursive traversal

The Status InitStack (SqStack & amp; S)
{
S.b ase=(ElemType *) malloc (STACK_INIT_SIZE * sizeof (ElemType));
if(! S.b ase) exit (OVERFLOW);
S.t op=S.b ase;
S.s tacksize=STACK_INIT_SIZE;
Return OK;
};

The Status of Push (SqStack & amp; S, ElemType e)
{
If (S.t op - S.b ase>=S.s tacksize)
{
S.b ase=(ElemType *) realloc (S.b ase, sizeof (STACK_INIT_SIZE + STACKINCREMENT) * (ElemType));
if(! S.b ase) exit (OVERFLOW);
S.t op=S.b ase + S.s tacksize;
S.s tacksize +=STACKINCREMENT;
}
* S.t op++=e;
Return OK;
};

The Status of Pop (SqStack & amp; S, ElemType & amp; E) {
//if the stack is not empty, delete S element of the stack,
//e return its value, and return OK;
//otherwise returns the ERROR
If (S.t op==S.b ase) return the ERROR;
E=* - S.t op;
Return OK;
}

The Status StackEmpty (SqStack & amp; S)
{
If (S.t op==S.b ase) return TRUE;
The else return FALSE;
};


The Status DestroyStack (SqStack & amp; S)
{
Free (S.b ase);
S.b ase=NULL;
S.t op=NULL;
S.s tacksize=0;
Return OK;
}

Int StackLength SqStack (S)
{
Return S.t op - S.b ase;
}

The Status GetTop (SqStack S, ElemType & amp; E)
{
If (S.t op==S.b ase) return the ERROR;
E=* - S.t op;
Return OK;
}

The Status ClearStack (SqStack & amp; S)
{
S.t op=S.b ase;
Return OK;
}


Visit Status StackTravers (SqStack S Status (*) (ElemType))
{
ElemType * p=S.b ase;
If (S.t op==S.b ase) return the ERROR;
Do
{
if (! Visit (* p)) return the ERROR;
} while (p++!=S.t op);
Return OK;
}
//////////////////////////////////////////////////


The Status CreateBiTree (BiTree & amp; T, char STR [])
{
Static int I=0;
Char data;
data=https://bbs.csdn.net/topics/str [i++];
The scanf (STR);
If (datahttps://bbs.csdn.net/topics/==') T=NULL;
The else
{T=new BiTNode;
T - & gt; data=https://bbs.csdn.net/topics/data;
CreateBiTree (T - & gt; Lchild, STR);
CreateBiTree (T - & gt; Rchild, STR);
}
Return OK;
}

The Status PreOrderTraverse (BiTree T) {//first sequence traversal
If (T)
{
Printf (" % c ", T - & gt; The data);
PreOrderTraverse (T - & gt; Lchild);
PreOrderTraverse (T - & gt; Rchild);
} return OK;
}

The Status InOrderTraverse (BiTree T) {//sequence traversal
If (T)
{
InOrderTraverse (T - & gt; Lchild);
Printf (" % c ", T - & gt; The data);
InOrderTraverse (T - & gt; Rchild);
} return OK;
}

After the Status PostOrderTraverse (BiTree T) {//sequence traversal
If (T)
{
PostOrderTraverse (T - & gt; Lchild);
PostOrderTraverse (T - & gt; Rchild);
Printf (" % c ", T - & gt; The data);
} return OK;
}

The Status SqStackInOrderTraverse (BiTree T) {
SqStack S;
BiTree p=T;
InitStack (S);
While (p | |! StackEmpty (S)) {
If (p) {Push (S, p); P=p - & gt; Lchild; }
The else {
Pop (S, p);
Printf (" % c ", p - & gt; The data);
P=p - & gt; Rchild;
}//else
}//while
Return OK;
}


The main function
# include "B1. H"

Int main ()
{
BiTree tree;
Char STR [100].
cout<" Please input data ";
CreateBiTree (tree, STR);
cout<" PreOrder: ";
PreOrderTraverse (tree);
coutcout<" InOrder: ";
InOrderTraverse (tree);
coutcout<" PostOrder: ";
PostOrderTraverse (tree);
coutSqStackInOrderTraverse (tree);
coutreturn 0;
}

nullnullnullnullnullnullnullnullnullnullnull
  • Related