Discussion :C++ chain binomial tree of of with a non recursive way of order order order traversal binomial tree
- 2020-04-02 00:58:22
- OfStack
If there is a shortage, also hope to correct!
A binary tree is constructed when you type 3, 2, 5, 0, 0, 4, 0, 0, 0, 6, 0, 0 (where the comma represents the return key at the time of actual input) on the keyboard
3
2 6
5 4
Output:
The 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.
//Binarytree.cpp: defines the entry point for the console application.
//C++ to achieve chain binary tree, using a non-recursive way to order first, in order, after the order traversal binary 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 the 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);
};
//The destructor
template <class T>
void BiTree<T>::Release(BiNode<T> *bt)
{
if(bt==NULL)
{
Release(bt->lchild );
Release(bt->rchild );
delete bt;
}
}
//Set up a binary 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 the right child "<<endl;
Create(bt->rchild );
}
}
template <class T>
void BiTree<T>::InOrderTraverse()
{
stack<BiNode<T>*> sta; //Defines an empty stack to hold BiNode Pointers
BiNode<T>* p = root;
sta.push(p); //Push the root pointer on the stack
while(!sta.empty())
{
while (NULL != p)
{//Go left to the end, and keep the passing node pointer, push
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(); //The top element is pushed
p = p->rchild; //A step to the right & NBSP;
if (NULL != p)
{
sta.push(p);
}
}
}
}
template<class T>
void BiTree<T>::PreOrderTraverse()
{
stack<BiNode<T>*> sta;
BiNode<T>* p = root;
sta.push(p); //Push the root pointer on the stack
while(!sta.empty())
{
while (NULL != p)
{//Go left to the end, and keep the passing node pointer, push
cout << p->data << " ";
p = p->lchild;
if (NULL != p)
{
sta.push(p);
}
}
if (!sta.empty())
{
p = sta.top();
sta.pop(); //The top element is pushed
p = p->rchild; //A step to the right & NBSP;
if (NULL != p)
{
sta.push(p);
}
}
}
}
template<class T>
void BiTree<T>::PostOrderTraverse()
{
stack<BiNode<T>*> sta; //The stack that holds the node pointer
stack<int> flagsta; //A stack containing token bits, each out (in) a node pointer, synchronization out (in) a token bit
unsigned flag; //Set the flag bit, 1- first access, 2- second access
BiNode<T>* p = root;
sta.push(p); //Push the root pointer on the stack
flagsta.push(1);
while(!sta.empty())
{
while (NULL != p && NULL != p->lchild)
{//Go left to the end, and keep the passing node pointer, push
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 first accessed
flagsta.push(2); //The first access does not stack the element, but sets the flag bit to 2& PI;
p = p->rchild; //Step to the right
sta.push(p);
flagsta.push(1);
}
else
{
sta.pop(); //Elements in the stack
cout << p->data << " "; //Accessing the top element of the stack
p = NULL; //Set the pointer to null
}
}
}
}
//The 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;
}
A binary tree is constructed when you type 3, 2, 5, 0, 0, 4, 0, 0, 0, 6, 0, 0 (where the comma represents the return key at the time of actual input) on the keyboard
3
2 6
5 4
Output:
The 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.