#include <iostream>#include <string.h>using namespace STD;Template<TypeNameType>structnode{Type data; Node<type> *left; Node<type> *right; Node (Type D = Type ()):d ATA (d), left (null), right (null) {}};Template<TypeNameType>classmtree{ Public: MTree () {root = NULL; }voidInit (Const Char*S1,Const Char*S2) {intn =strlen(S1); Init (Root,s1,s2,n); }voidPrintf () {_printf (root); }Private:void_printf (node<type> *t) {if(T!=null) {cout<<t->data<<"\ T"; _printf (T->left); _printf (T->right); } }voidInit (node<type> *&t,Const Char*&S1,Const Char*S2,intN) {//Based on the binary tree pre-order followed by the subsequent construction of the two-fork tree. inti =0;if(*s1==' + ')return; for(; i<=n;i++) {if(*s1==s2[i]) { Break; } }if(i>n)return; t =NewNode<type> (*S1); Init (t->left,++s1,s2,i-1); Init (t->right,s1,s2+i+1, n-i); } node<type> *root;};intMain () {mtree<Char> MT;Char*S1 =New Char[7];Char*S2 =New Char[7];strcpy(S1,"ABCDEFG");strcpy(S2,"Cbdafeg"); Mt. Init (S1,S2); Mt. Printf ();return 0;}
C + + constructs a two-fork tree based on pre-order and middle order