Recently sorted out some of the original code, the brain a bit not well, or write it down.
Binary_tree.h, traversal includes both recursive and non-recursive, hierarchical traversal
#ifndef _binary_tree_h_
#define _BINARY_TREE_H_
template<class t>
struct binode
{
T data;
binode<t>* lchild, *rchild;
Template<class t>
class Bitree
{public
:
bitree ();
~bitree ();
binode<t>* getroot ();
void preorder (binode<t>* node);
void Inorder (binode<t>* node);
void Postorder (binode<t>* node);
Non-recursive implementation of
void Preordernonrec (binode<t>* node);
void Inordernonrec (binode<t>* node);
void Postordernonrec (binode<t>* node);
void Levelorder (binode<t>* node);/Hierarchy Traversal
protected:
private:
binode<t>* m_root;
binode<t>* Create ();
void release (binode<t>* root);
#endif
Bianry_tree.cpp
#include <iostream> #include <stack> #include <queue> using namespace std;
Template<class t> binode<t>* bitree<t>::getroot () {return m_root;}
Template<class t> Bitree<t>::bitree () {m_root = new binode<t>;
M_root = Create (); } template<class t> Bitree<t>::~bitree () {release (m_root);} template<class t> binode<t>* BiTre
E<t>::create () {char ch;
cin>>ch;
binode<t>* Pnode;
if (ch = = ' # ') Pnode = NULL;
else {pnode = new binode<t>;
Pnode->data = ch;
Pnode->lchild = Create ();
Pnode->rchild = Create ();
return pnode; } template<class t> void bitree<t>::release (binode<t>* root) {if (root!= NULL) {release (ROOT-&G
T;lchild);
Release (Root->rchild); } template<class t> void Bitree<t>::P reorder (binode<t>* node) {//TLR's first must be root if (node = NULL) R
Eturn;
else {cout<<node->data<< ""; Preorder (Node->lchild);
Preorder (Node->rchild);
} template<class t> void Bitree<t>::inorder (binode<t>* node) {//sequential traversal and in-sequence traversal can determine unique binary tree/sequential traversal and in-sequence traversal can also be
But the preface and the subsequent sequence are not allowed//characteristic is the fixed root, in the order of the left and right if (node = NULL) return;
else {inorder (node->lchild);
cout<<node->data<< "";
Inorder (Node->rchild);
} template<class t> void Bitree<t>::P ostorder (binode<t>* node) {//LRT The last one must be root if (node = NULL)
Return
else {postorder (node->lchild);
Postorder (Node->rchild);
cout<<node->data<< ""; } template<class t> void bitree<t>::P Reordernonrec (binode<t>* node) {Stack<binode<t>*> ;
S
binode<t>* p = node;
while (P!= null | |!s.empty ()) {while (P!= null) {cout<<p->data<< "";
S.push (P);
p = p->lchild;
} if (!s.empty ()) {p = s.top ();
S.pop ();
p = p->rchild;
}}} Template<class t>void Bitree<t>::inordernonrec (binode<t>* node) {//first-order traversal recursion needs to be implemented with stack s, simulating recursive calls// The total loop boundary is either the current node is not empty or the stack is not empty,//@1 is not empty at the current node p, p into the stack s,p left subtree to p, to ensure that the left subtree can be in the stack//p for the empty time, that is, Zuozi most left access, this time when the stack is not empty//@2 take the top of the stack to p, input p,
Out stack, at this time the most left-most node access, the P right subtree to P, repeat @1 stack<binode<t>*> s;
binode<t>* p = node;
while (P!= null | |!s.empty ()) {while (P!= null) {S.push (P);
p = p->lchild;
} if (!s.empty ()) {p = s.top ();
cout<<p->data<< "";
S.pop ();
p = p->rchild; }} template<class t> void Bitree<t>::P ostordernonrec (binode<t>* node) {//To ensure that the root node is accessible after the left child and right child Access,//So for any node p, first put it into the stack.
If p does not exist for the left child and the right child, you can access it directly;/or P has a left child or right child, but both the left and right children have been visited, and the node can also be accessed directly.
In either case, the right child of P and the left child are sequentially placed in the stack, which ensures that each time the top element is taken,//the left child is visited in front of the right child, and the left and right children are accessed before the root node.
if (node = NULL) return;
Stack<binode<t>*> s;
S.push (node);//node is the root binode<t>* pre = NULL;
binode<t>* cur;
while (!s.empty ()) {cur = s.top (); if (Cur->lchild = null && cur->rchild = NULL | | (Pre!= NULL) && (pre = = Cur->lchild | | pre = = cur->rchild))//Last accessed is the left subtree of the current node {Cout<<cur->data
<< "";
S.pop ();
Pre = cur;
else {if (cur->rchild) S.push (cur->rchild);
if (cur->lchild) S.push (cur->lchild); }} template<class t> void Bitree<t>::levelorder (binode<t>* node) {//Hierarchy traversal requires a queue to implement, thinking://@1 initialization
Queue//If root is null returns//@2 push (Root)//@3 while (queue is not empty)//S <--queue.front ()//Queue.pop ()//input S.data
If (S's left subtree is not empty)//S's left subtree Join//if (S's right subtree is not empty)//S's right subtree team queue<binode<t>*> Q;
binode<t>* s = node;
if (s = = NULL) return;
Q.push (s);
while (!q.empty ()) {s= q.front ();
Q.pop ();
cout<<s->data<< "";
if (s->lchild) Q.push (s->lchild);
if (s->rchild) Q.push (s->rchild); }
}Test
#include "list.h"
#include "binary_tree.h"
#include <iostream>
using namespace std;
int main ()
{
bitree<char> my_tree;
My_tree. Preorder (My_tree. Getroot ());
cout<<endl;
My_tree. Preordernonrec (My_tree. Getroot ());
cout<<endl;
My_tree. Inorder (My_tree. Getroot ());
cout<<endl;
My_tree. Inordernonrec (My_tree. Getroot ());
cout<<endl;
My_tree. Postorder (My_tree. Getroot ());
cout<<endl;
My_tree. Levelorder (My_tree. Getroot ());
cout<<endl;
My_tree. Postordernonrec (My_tree. Getroot ());
cout<<endl;
}