Traversing a binary tree using the stack sequence

Source: Internet
Author: User
Tags printf
Line sequence builds a two-fork tree and then iterates through the stack sequence

#include "Stdlib.h"
#include "stdio.h"
#include "iostream.h"
Traversal *********** of Binary tree
typedef char Telemtype;

#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 10
#define Stackincrement 2
#define OK 1
#define ERROR 0
#define OVERFLOW-2
typedef int SELEMTYPE;
typedef int STATUS;

Define binary tree and node structure
typedef struct BITNODE
{
Telemtype data;
int isvisit;
struct Bitnode *lchild,*rchild;
}bitnode,*bitree;

typedef bitree SELEMTYPE;

Define the structure of the sequential stack;

typedef struct SQSTACK
{
Selemtype *base;
Selemtype *top;
int stacksize;
}sqstack;


//Initialize an empty stack,
Status initstack (sqstack *s)

  (*s). Base = (Selemtype *) malloc (stack_init_ Size*sizeof (Selemtype));
 if (!) ( *s)   
  exit (OVERFLOW);
  (*s). Top = (*s). base; 
  (*s). StackSize = stack_init_size; 
 return ok; 
}

Data elements into the stack;
Status Push (Sqstack *s,selemtype e)
{
if ((*s). top-(*s). Base >= (*s). stacksize)//Increase space
{
(*s). Base = (Selemtype *) realloc ((*s). Base,
((*s). Stacksize+stackincrement) *sizeof (Selemtype));
if (! ( *s). Base)
Exit (OVERFLOW);
(*s). Top = (*s). base+ (*s). stacksize;
(*s). StackSize + = stackincrement;
}
* ((*s). top) + + = e;
return OK;
}

Data elements out of the stack;
Status Pop (sqstack *s,selemtype *e)
{
if ((*s). Top = = (*s). Base)
return ERROR;
*e = *--(*s). Top;
return OK;
}

Determine if a stack is empty;

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

destroying a stack;

Status Destroystack (Sqstack *s)
{
Free ((*s). Base);
(*s). base = NULL;
(*s). top = NULL;
(*s). stacksize = 0;
return OK;
}


Take the data element of the top of the stack;
Status GetTop (sqstack s,selemtype *e)
{
if (s.top>s.base)
{
*e = * (s.top-1);
return OK;
}
Else
return ERROR;
}


Create an empty two-fork tree
Status Initbitree (Bitree *t)
{
*t = NULL;
return OK;
}

Destroy the binary tree
void Destroybitree (Bitree *t)
{
if (*t)
{
if ((*t)->lchild)
Destroybitree (& (*t)->lchild);
if ((*t)->rchild)
Destroybitree (& (*t)->rchild);
Free (*t);
*t = NULL;
}
}

Construct a two-pronged tree
void Createbitree (Bitree *t)
{
Telemtype ch;
Initbitree (T);

cin>>ch;
if (ch = = ' 0 ')
T = NULL;
Else
*t = (bitree) malloc (sizeof (Bitnode));
if (! ( T))
Return
(*t)->data = ch;
(*t)->isvisit = 1;
Createbitree (& (*t)->lchild);
Createbitree (& (*t)->rchild);
}

node of output binary tree
Status Visitt (Telemtype e)
{
printf ("%c", e);
return OK;
}

Sequential traversal of two-fork trees using stacks
Status stackinordervisit (Bitree t,status (*visit) (Telemtype))
{
Sqstack S;
Initstack (&s);
Push (&s,t);

while (s.base! = s.top)
{
while (T->lchild && (*t). Isvisit = = 1)
while (T->lchild)
{
T = t->lchild;
Push (&s,t);
t->isvisit = 0;
}

Pop (&s,&t);
Visit (T->data);

if (t->rchild)
{
T = t->rchild;
Push (&s,t);
}

}
return OK;
}

Main function
void Main ()
{
Bitree T;
printf ("Create a tree in the first order: \ n");
cout<< "Please enter a node (0 = empty)" <<endl;

Createbitree (&t); Line order build two fork Tree

Stackinordervisit (T,visitt); Middle sequence Traversal binary tree

printf ("\ n");
Destroybitree (&t);
}

Operation Result:

To create a tree in the first order:
Please enter a node (0 = empty).
Dba00c00fe00g00
Abcdefg

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.