Thought of sequential Traversal
If the binary tree is empty, the traversal is completed; otherwise, the left subtree of the root node is traversed in the middle order; the root node is accessed; and The right subtree is traversed in the middle order;
Implementation Code:
// Code template <typename T> void binarytree <t>: intrabitree (btnode <t> * P) {If (p) {intrabitree (p-> lchild ); cout <p-> data <"; intrabitree (p-> rchild );}}
The following describes how to simulate a computer by using pseudo-code.
Suppose there is such a simple binary tree
To simulate a computer with pseudo-code, follow these steps:
Intrabitree (a) {if (a) {intrabitree (B) // A is B on the left, not empty {If (B) {intrabitree (B on the left) // If the left child of B is empty, execute the Next Step {If (null) {};} cout <B; intrabitree (c) // The right child of B is c {If (c) {The left child of intrabitree (c) // The left child of C is empty. Execute cout <C; the right child of intrabitree (C)/C is empty, directly execute next} // end C layer C execution ends, jump to B layer} // end B layer B jump to a layer cout <A; intrabitree (d) // right A is d {If (d) {intrabitree (left sub-d) // left sub-D is empty, and the next cout <D intrabitree (right sub-d) is executed directly) // if the right child of D is empty, execute the next step }}}}
The following is the complete implementation code (C ++). You can test it.
Template <typename T> struct btnode {t data; btnode <t> * lchild; btnode <t> * rchild ;}; template <typename T> class binarytree {PRIVATE: btnode <t> * Bt; public: binarytree () {bt = NULL;} // constructor, construct the root node, and leave the root node empty void createbitree (t end ); // create the end Binary Tree btnode <t> * getroot (); // return the root address void intrabitree (btnode <t> * P ); // traverse binary tree in the middle order}; void binarytree <t>: createbitree (t end) {cout <"input binary tree, flag with "<End <" as the NULL pointer field: "<Endl; btnode <t> * P; T x; CIN> X; If (x = END) return; P = new btnode <t>; P-> DATA = x; P-> lchild = NULL; p-> rchild = NULL; bt = P; Create (P, 1, end); Create (p, 2, end );} template <typename T> static int create (btnode <t> * P, int K, int end) {btnode <t> * q; t x; CIN> X; if (X! = END) {q = new btnode <t>; q-> DATA = x; q-> lchild = NULL; q-> rchild = NULL; If (k = 1) p-> lchild = Q; If (k = 2) P-> rchild = Q; Create (Q, 1, end); Create (Q, 2, end );} return 0;} template <typename T> btnode <t> * binarytree <t>: getroot () {return Bt;} template <typename T> void binarytree <t> :: intrabitree (btnode <t> * P) {If (p) {intrabitree (p-> lchild); cout <p-> data <""; intrabitree (p-> rchild) ;}# include <iostream> using namespace STD; # include "binarytree. H "; int main (void) {binarytree <char> B; // construct a binary tree object. Only one B Pointer Points to null char end = '#'; B. createbitree (end); // create a binary tree, with # As the end sign getchar (); cout <"sequential traversal of a binary tree:"; B. intrabitree (B. getroot (); cout <Endl; getchar ();}