C language version Two fork Tree Basic Operation Example (first order recursive non-recursive) _c language

Source: Internet
Author: User

Copy Code code as follows:

Please follow the input binary tree elements (one character per node, empty node = ' = ') by First order:
abd==e==cf==g==

First-order recursive traversal:
A B D E C F G
In-sequence recursive traversal:
D B E A F C G
Subsequent recursive traversal:
D E B F G C A
Sequence recursive traversal:
ABCDEFG
First-order non-recursive traversal:
A B D E C F G
Middle-order non-recursive traversal:
D B E A F C G
Subsequent non-recursive traversal:
D E B F G C A
Depth:
Please press any key to continue ...

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>

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

#define STACK_INIT_SIZE 100
#define Stackincrement 10

typedef int STATUS;

typedef char ELEMTYPE;
typedef struct BTNODE
{
Elemtype data;
struct Btnode *leftchild;
struct Btnode *rightchild;
}btnode, *bintree;

typedef bintree SELEMTYPE;

typedef STRUCT{//Stack Structure definition
Selemtype *base;
Selemtype *top;
int stacksize;
}sqstack;

Bintree Createbintree (Bintree T);
Status Visit (Elemtype e);
Status Depth (Bintree T);
Status Preorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e));
Status Inorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e));
Status Postorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e));
Status Levelorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e));

Define the operation of the stack
Status Initstack (Sqstack *s);
Status Destroystack (Sqstack *s);
Status Clearstack (Sqstack *s);
Status stackempty (Sqstack S);
int Stacklength (Sqstack S);
Status GetTop (Sqstack s,selemtype *e);
Status Push (Sqstack *s,selemtype e);
Status POPs (Sqstack *s,selemtype *e);
Status stacktraverse (const sqstack *s);

Status Preordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e));
Status Inordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e));
Status Postordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e));

int main ()
{
int depth;
Bintree tree = NULL;
Status (*visit) (elemtype e) = visit;
printf_s ("Please enter the binary tree element (one character per node, the empty node is ' = ') by first order"; \ n ");
Tree = Createbintree (tree);

printf_s ("\ n first recursive traversal: \ n");
Preorderrecursiontraverse (Tree,visit);
printf_s ("\ n-ordinal recursive traversal: \ n");
Inorderrecursiontraverse (Tree,visit);
printf_s ("\ n sequence recursive traversal: \ n");
Postorderrecursiontraverse (Tree,visit);
printf_s ("\ n sequence recursive traversal: \ n");
Levelorderrecursiontraverse (Tree,visit);

printf_s ("\ n-Order non-recursive traversal: \ n");
Preordernonerecursiontraverse (Tree,visit);
printf_s ("\ n-Sequence non-recursive traversal: \ n");
Inordernonerecursiontraverse (Tree,visit);
printf_s ("\ n sequence not recursive traversal: \ n");
Postordernonerecursiontraverse (Tree,visit);

    printf_s ("\ n depth: \ n");
    depth = depth (tree);
    printf_s ("%d\n", depth);
    System ("pause");
    return 0;
}

//Create two fork tree
Bintree createbintree (bintree T)
{
    char ch;
    scanf_s ("%c", &ch);
    if (ch = = ')
    {
        T = NULL;
   }
    Else
    {
         if (!) ( t= (Btnode *) malloc (sizeof (Btnode)))
        {
             exit (OVERFLOW);
       }
        t->data = ch;   //Generate root node
         t->leftchild = Createbintree (t->leftchild);
        t->rightchild = Createbintree (t->rightchild);
   }
    return T;
}

//Access binary tree
Status Visit (elemtype e)
{
    if (e = ' ")
    {
         return ERROR;
   }
    Else
    {
        printf_s ("%c", E );
   }
    return OK;
}

//first-order traversal recursive algorithm
Status preorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e)
{
     if (T)
    {
        if (! Visit (t->data))
        {
             return ERROR;
       }
        preorderrecursiontraverse (T->leftchild, Visit);
        preorderrecursiontraverse (T->rightchild, Visit);
   }
    return OK;
}

//Middle-order traversal recursive algorithm
Status inorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e)
{
    if (T)
    {
        inorderrecursiontraverse (t-> Leftchild, Visit);
        if (! Visit (t->data))
        {
             return ERROR;
       }
        inorderrecursiontraverse (T->rightchild, Visit);
   }
    return OK;
}

Sequence traversal recursive algorithm
Status Postorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e))
{
if (T)
{
Postorderrecursiontraverse (T->leftchild, Visit);
Postorderrecursiontraverse (T->rightchild, Visit);
if (! Visit (T->data))
{
return ERROR;
}
}
return OK;
}

Sequence traversal recursive algorithm
Status Levelorderrecursiontraverse (Bintree T, Status (*visit) (Elemtype e))
{
if (T)
{
Btnode *q[100];//assumption does not overflow
int front = -1,rear =-1;
if (T)
{
Q[++rear] = T;
printf_s ("%c", t->data);
while (front!= rear)
{
Btnode *p;
if (!) ( p = (Btnode *) malloc (sizeof (Btnode)))
{
Exit (OVERFLOW);
}
p = Q[++front];
if (p->leftchild)
{
Q[++rear] = p->leftchild;
printf ("%c", p->leftchild->data);
}
if (p->rightchild)
{
Q[++rear] = p->rightchild;
printf ("%c", p->rightchild->data);
}
}
}
}
return OK;
}

Status Depth (Bintree T)
{
int a,b;
if (! T
{
return ERROR;
}
Else
{
A = Depth (t->leftchild) + 1;
b = Depth (t->rightchild) + 1;
Return a > B? A:B;
}
}

First-order traversal non-recursive algorithm
Status Preordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e))
{
Sqstack S;
Selemtype p;

Initstack (&s);
Push (&s, T);

while (! Stackempty (S))
{
POPs (&s, &p);
if (! Visit (P->data))
{
return ERROR;
}
if (p->leftchild)
{
Push (&s, p->rightchild);
}
if (p->rightchild)
{
Push (&s, p->leftchild);
}
}
Destroystack (&s);
return OK;
}

A non-recursive algorithm for sequential traversal
Status Inordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e))
{
Sqstack S;
Selemtype p;

Initstack (&s);
Push (&s, T);
while (! Stackempty (S))
{
while (GetTop (s,&p) && p)
{
Push (&s, p->leftchild);
}
POPs (&s, &p);
if (! Stackempty (S))
{
POPs (&s, &p);
if (! Visit (P->data))
{
return ERROR;
}
Push (&s, p->rightchild);
}
}
Destroystack (&s);
return OK;
}

Post-convenience non-recursive algorithm
Status Postordernonerecursiontraverse (Bintree T, Status (*visit) (Elemtype e))
{
Sqstack S;
Selemtype p, q;
Initstack (&s);
Push (&s,t);
while (! Stackempty (S))
{
while (GetTop (s,&p) &&p&& (p->leftchild| | P->rightchild))
{
Push (&s,p->rightchild);
Push (&s,p->leftchild);
}
if (! Stackempty (S)) {
Pop (&AMP;S,&AMP;P);
if (p)
{
if (! Visit (P->data))
{
return ERROR;
}
}
Else
{
Pop (&AMP;S,&AMP;P);
if (! Visit (P->data))
{
return ERROR;
}
}
while (GetTop (s,&q) &&q&&p==q->rightchild)
{
Pop (&AMP;S,&AMP;P);
if (! Visit (P->data))
{
return ERROR;
}
GetTop (S,&AMP;Q);
}
}
}
Destroystack (&s);
return OK;
}

-----------stack of related operations--------------//
Status Initstack (Sqstack *s) {
S->base = (Selemtype *) malloc (stack_init_size * sizeof (Selemtype));
if (! S->base)
{
Exit (0);
}
S->top = s->base;
S->stacksize = stack_init_size;
return OK;
}

Status Destroystack (Sqstack *s) {
if (! S
{
Exit (0);
}
Free (s->base);
return OK;
}

Status Clearstack (Sqstack *s) {
if (! S
{
return FALSE;
}
S->top = s->base;
return OK;
}

Status stackempty (Sqstack S) {
if (s.top==s.base)
{
return TRUE;
}
Else
{
return FALSE;
}
}

int Stacklength (Sqstack S) {
return s.stacksize;
}

Status GetTop (sqstack s,selemtype *e) {
    if (s.top = s.base)
    {
         return FALSE;
   }
    Else
    {
        *e = * (s.top-1);
        return OK;
   }
}

Status Push (Sqstack *s,selemtype e) {
    if (s->top-s->base>=s->stacksize)
    {
        s->base = (Selemtype *) realloc (s->base, (S- >stacksize + stackincrement) * sizeof (Selemtype));
        if (! S->base)
        {
             exit (0);
       }
        s->top = s->base+s->stacksize;
        s->stacksize + = stackincrement;
   }
    *s->top++ = e;
    return OK;
}

Status Pop (sqstack *s,selemtype *e) {
if (s->top==s->base)
{
return ERROR;
}
*e = * (--s->top);
return OK;
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.