Discussion: C + + implementation chain binary tree (with non-recursive way first, in order, sequential traversal of binary tree) _c language

Source: Internet
Author: User
If there are deficiencies, also hope to correct the point!
Copy Code code as follows:

BinaryTree.cpp: Defines the entry point for a console application.
C + + implementation chain binary tree, the use of a non-recursive way to preface, in order, after the second traversal of the two-fork tree
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace Std;
Template<class t>
struct Binode
{
T data;
struct binode<t> *rchild,*lchild;
};
Template<class t>
Class Bitree
{
Public
Bitree () {
cout<< "Please enter root node:" <<endl;
Create (root);
if (NULL!= root)
{
cout<< "root=" <<root->data<<endl;
}
Else
{
cout << "The BinaryTree is empty." << Endl;
}
}
~bitree () {release (root);}
void Inordertraverse ();
void Preordertraverse ();
void Postordertraverse ();
Private
Binode<t> *root;
void Create (binode<t>* &bt);
void release (Binode<t> *BT);
};
destructor
Template <class t>
void Bitree<t>::release (binode<t> *bt)
{

if (bt==null)
{
Release (Bt->lchild);
Release (Bt->rchild);
Delete bt;
}
}
Build two fork Tree
Template <class t>
void Bitree<t>::create (binode<t>* &bt)
{
T-ch;
cin>>ch;
if (ch== 0) Bt=null;
Else
{
Bt=new binode<t>;
Bt->data =ch;
cout<< "Call left child" <<endl;
Create (Bt->lchild);
cout<< "Call right child" <<endl;
Create (Bt->rchild);
}
}
/************************************************************************
Method: In-sequence traversal (non-recursive form)
Thought: Go to the left to the end, into the stack. Out stack, Access node, one step to the right
************************************************************************/
Template <class t>
void Bitree<t>::inordertraverse ()
{
Stack<binode<t>*> STA; Defines an empty stack that holds a Binode type pointer
binode<t>* p = root;
Sta.push (P); Put the root pointer into the stack
while (!sta.empty ())
{
while (NULL!= p)
{///left to the end, and keep the passing node pointer, into the stack
p = p->lchild;
if (NULL!= p)
{
Sta.push (P);
}
}
if (!sta.empty ())
{
p = sta.top ();
cout << p->data << ""; Accessing the top element of the stack,
Sta.pop (); Stack top element out of stack
p = p->rchild; One step to the right
if (NULL!= p)
{
Sta.push (P);
}
}
}
}
/************************************************************************
Method: First-order traversal (non-recursive form)
Thought: Go to the left to the end, into the stack, access nodes. Out stack, one step to the right
************************************************************************/
Template<class t>
void Bitree<t>::P Reordertraverse ()
{
Stack<binode<t>*> STA;
binode<t>* p = root;
Sta.push (P); Put the root pointer into the stack
while (!sta.empty ())
{
while (NULL!= p)
{///left to the end, and keep the passing node pointer, into the stack
cout << p->data << "";
p = p->lchild;
if (NULL!= p)
{
Sta.push (P);
}
}
if (!sta.empty ())
{
p = sta.top ();
Sta.pop (); Stack top element out of stack
p = p->rchild; One step to the right
if (NULL!= p)
{
Sta.push (P);
}
}
}
}
/************************************************************************
Subsequent traversal (not recursive form)
Thought: Start from the root node, go to the left to the end, merged into the stack, while setting the flag bit to 1.
If the node has a right subtree when it is out of the stack, it is the first visit, if it is the 1th time,
is not out of the stack, the flag will be changed to 2, if the second access, the stack.

************************************************************************/
Template<class t>
void Bitree<t>::P Ostordertraverse ()
{
Stack<binode<t>*> STA; Stack that holds the pointer to a node
Stack<int> Flagsta; Store the stack of flag bits, each out (into) a node pointer, sync out (into) a sign bit
unsigned flag; Set flag bit, 1-first visit, 2-second visit
binode<t>* p = root;
Sta.push (P); Put the root pointer into the stack
Flagsta.push (1);
while (!sta.empty ())
{
while (null!= p && null!= p->lchild)
{///left to the end, and keep the passing node pointer, into the stack
p = p->lchild;
Sta.push (P);
Flagsta.push (1);
}
if (!sta.empty ())
{
Flag = Flagsta.top ();
Flagsta.pop ();
p = sta.top ();
if ((NULL!= p->rchild) && flag = 1)
{//If the right subtree is not empty and is the first time to access
Flagsta.push (2); The element does not stack on first access, but sets the flag bit to 2
p = p->rchild; One step to the right
Sta.push (P);
Flagsta.push (1);
}
Else
{
Sta.pop (); Element out Stack
cout << p->data << ""; accessing stack top elements
p = NULL; Set the pointer to null
}
}
}
}

Copy Code code as follows:

Test program
void Main ()
{
Bitree<int> A;
cout << "The Inordertraverse is:";
A.inordertraverse ();
cout << Endl;
cout << "The Preordertraverse is:";
A.preordertraverse ();
cout << Endl;
cout << "The Postordertraverse is:";
A.postordertraverse ();
cout << Endl;
}

When the 3,2,5,0,0,4,0,0,6,0,0 is entered once on the keyboard (where the comma represents the return key when the actual input is entered), the two-fork tree is constructed
3
2 6
5 4
Output:
Root=3
The Inordertraverse is:5 2 4 3 6
The Preordertraverse is:3 2 5 4 6
The Postordertraverse is:5 4 2 6 3
Achieve the desired effect.
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.