Binary Tree creation and recursive and non-recursive Traversal

Source: Internet
Author: User

Binary Tree creation and recursive and non-recursive Traversal
# Define destdio. h
# Define demalloc. h
# Define destdlib. h
Typedef struct lNode
{
Char data;
Struct lNode * lchild;
Struct lNode * rchild;
} LNODE, * Tree;


Typedef struct Node
{
Tree data;
Struct Node * Next;
} NODE, * PNODE;


Typedef struct Stack
{
PNODE pBottom;
PNODE pTop;
} STACK, * PSTACK;


// Function declaration
PSTACK initStack (void); // create a stack
Void push (pstack s, Tree val); // press the stack
Void pop (pstack s); // output Stack
Bool stackEmpty (PSTACK pS );






Tree CreateBiTree (); // Tree Creation
Void pretraverse (Tree T); // first-order recursive Traversal
Void Intraverse (Tree T); // recursive traversal in the middle order
Void posttraverse (Tree T); // recursive post-order traversal
Void NoIntraverse (Tree T); // non-recursive traversal in the middle order


Int main ()
{
Tree T;
T = CreateBiTree ();


Pretraverse (T); printf ();

Intraverse (T); printf ();


Posttraverse (T); printf ();

NoIntraverse (T); printf ();


Return 0;
}
// Build
Tree CreateBiTree ()
{
Char ch;
Tree T;
Scanf (% c, & ch );
If (ch = '#') T = NULL;
Else {
T = (Tree) malloc (sizeof (LNODE ));
T-> data = ch;
T-> lchild = CreateBiTree ();
T-> rchild = CreateBiTree ();
}
Return T; // return the root node
}


// Recursive traversal in the First Order (traversal order: left and right of the root)
Void pretraverse (Tree T)
{
If (T! = NULL)
{
Printf (% c, T-> data); // output the root node data
Pretraverse (T-> lchild); // traverses the left subtree
Pretraverse (T-> rchild); // traverses the right subtree
}
}
// Recursive traversal in the middle order (traversal order: Left root and right)
Void Intraverse (Tree T)
{
If (T! = NULL)
{
Intraverse (T-> lchild); // traverses the left subtree
Printf (% c, T-> data); // output the root node data
Intraverse (T-> rchild); // traverses the right subtree


}
}
// Recursive post-order traversal (traversal order: Left and Right root)
Void posttraverse (Tree T)
{
If (T! = NULL)
{
Posttraverse (T-> lchild); // traverses the left subtree
Posttraverse (T-> rchild); // traverses the right subtree
Printf (% c, T-> data); // output the root node data

}
}


// Ordinal traversal (non-recursive traversal)
Void NoIntraverse (Tree T)
{
PSTACK s;
S = initStack ();
Tree p = T;
While (p! = NULL |! StackEmpty (s ))
{
While (p)
{
Push (s, p );
P = p-> lchild;
}
If (! StackEmpty (s ))
{
Pop (s );

P = p-> rchild;
}
}
}
PSTACK initStack (void)
{
PSTACK pS;
PS-> pTop = (PNODE) malloc (sizeof (NODE ));
If (pS-> pTop = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
PS-> pBottom = pS-> pTop;
PS-> pBottom-> Next = NULL;

}
Return pS;
}
Void push (PSTACK pS, Tree val)
{
PNODE q;
Q = (PNODE) malloc (sizeof (NODE ));
If (q = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
Q-> data = val;
Q-> Next = pS-> pTop;
PS-> pTop = q;
}

}
Void pop (PSTACK pS)
{
PNODE q;
If (pS-> pTop = NULL)
{
Printf (dynamic memory allocation failed !);
Exit (-1 );
}
Else
{
Q = pS-> pTop;
Printf (% c, q-> data );
PS-> pTop = q-> Next;
Free (q );

}


}
Bool stackEmpty (PSTACK pS)
{
If (pS-> pTop = pS-> pBottom)
{
Return true;
}
Return false;
}

 

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.