A two-fork tree is generated from the sequence of pre and middle sequence

Source: Internet
Author: User

First, the foreword:

A colleague of mine brought her husband's distance education exam questions, called everyone to help do, I wrote this, source code through VC6 compile links, implementation success, hehe; the topic is to give a binary tree sequence (ABDCEGF) and sequence (DBAEGCF), Required to complete a binary tree based on these two inputs, but did not specify in what way, so I used the most clumsy order to store the binary tree, but use the map to save the binary tree. The topic also requires the formation of two-fork tree can traverse this binary tree, because three kinds of traversal is only different order, so I only realized the subsequent traversal, not to mention the input is the preface and in the preface. ) should have been done with template, but colleagues asked not to use complex C + + syntax, so that brother-in-law difficult to deal with the teacher's questions, so I only use string to save the data. I graduated from the Department of Mathematics, the data structure at that time only learned a few chapters, so please friends do not laugh at me. :> I think the only place that this program can help you is to remind you not to make recursive functions inline, and I hope you'll pay more attention to this when you're working. Very eager friends and I contact email, criticize me, let me progress, thank you. :>

Second, code implementation:

BTree3.cpp:Defines the entry point for the console application.
//
Author:song Ke
Email:kesongemini@vip.163.com
Homepage:http://kesongemini.diy.163.com
qq:123588179
COMPANY:www.etonglian.com
Aim:programmed by Songke 2002/12/16 Night at home
For Sister an Ning ' s her husband ' s exam-3:
Known as preorder (ABDCEGF) and inorder (DBAEGCF) sequence
To request for the btree and postorder sequence
status:finished!
Method:the binary Songke_binarytree with ordinal saving
test:succeeded by Compile and link
platform:vc++6.0 + SP5
Ms-stl not sgi-stl!
At Windows2000 + SP3
Note:don ' T WRITE the recursion FUNCTION as INLINE function!
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <utility>
#include <map>
#include <algorithm>
using namespace Std;
Class Songke_binarytree
{
Public
Songke_binarytree (): _i_generate (0) {}
~songke_binarytree () {}
void Pre_insert (const string& s) {_pre.push_back (s);}
void In_insert (const string& s) {_in.push_back (s);}
  
void Pre_out () {_out ("preorder:", _pre);}
void In_out () {_out ("inorder:", _in);}
  
void Post_out ();
Generate the binary tree
void Generate ();
Private
Get left or right subtree for iteration
void _get_subtree (int iNode, vector< pair<string, int> >& v);
void _get_subtree (bool bleft, iNode of int, vector< pair<string, int> >& v);
void _postorder_iterate (const vector< pair<string, int> >& v);//Postorder Iterate
void _inorder_iterate (const vector< pair<string, int> >& v); Using Postorder Iterate
void _preorder_iterate (const vector< pair<string, int> >& v);/using Postorder Iterate
int _i_generate; Point to current element of preorder
Mark the elements in Inorders, and recurse the Func in.
Bleft = = True or false is right
void _kesongemini (bool bleft, an int iNode, const vector<string>& v);
void _kesongemini (const string& node, int JJ, BOOL bleft, an int iNode, const vector<string>& v);
Print out
void _out (const string& Explain, const vector<string>& VEC);
Vector<string> _pre; Preorder sequence as known
Vector<string> _in; Inorder sequence as known
Vector<string> _post; Postorder sequence as Request
  
vector< pair<string, int> > _s; string, position as ordinal saving
Map<int, string> _sm; Assistant for making subtree when Postorder iterate
Vector<int> _si; Assistant for making subtree when Postorder iterate
};
void Songke_binarytree::_out (const string& Explain, const vector<string>& VEC)
{
cout << explain << "T";
for (Vector<string>::const_iterator i = Vec.begin (); I!= vec.end (); ++i)
{
cout << *i << "";
}
cout << Endl;
}
void Songke_binarytree::generate ()
{
cout << "The Btree is" << Endl;
String node (_pre[_i_generate++]); Get the the elem of preorder
int JJ = 1;
_kesongemini (node, JJ, True, 0, _in);
}
void Songke_binarytree::_kesongemini (bool bleft, an int iNode, const vector<string>& v)
{
String node (_pre[_i_generate++]); Get a elem of preorder in turn
int JJ = Bleft? 2*inode/* Left * *: 2*inode+1/* right * *;
_kesongemini (node, JJ, Bleft, INode, V);
}
void Songke_binarytree::_kesongemini (const string& node, int JJ, BOOL bleft, an int iNode, const VECTOR&LT;STRING&GT;&AM P V
{
_s.push_back (Make_pair (node, JJ)); Get a node of the Betree in turn
_sm[JJ] = node; Assistant for Postorder iterate later:)
_si.push_back (JJ); Assistant for Postorder iterate later:)
cout << "\t\t" << (* (_s.end ()-1)). A/C << "<< (* (_s.end ()-1)). Second << Endl;
Vector<string> L, R;
BOOL B = false;
To find the node in the inorder sequence
for (Vector<string>::const_iterator i = V.begin (); I<v.end (); ++i)
{
if (!b && *i!= node)//left subtree
{
L.push_back (*i);
}
else if (!b && *i = node)//backbone found here
{
B = true;
}
else if (b && *i!= node)/right subtree
{
R.push_back (*i);
}
}
if (!l.empty ()) _kesongemini (True, JJ, L); Left subtree
if (!r.empty ()) _kesongemini (False, JJ, R); Right subtree
}
void Songke_binarytree::_get_subtree (int iNode, vector> pair<string, int> >& v)
{
Vector<int>::iterator ITER;
  
iter = Find (_si.begin (), _si.end (), INode); The Header node self
if (ITER!= _si.end ())
{
V.push_back (Make_pair (_sm[inode), inode));
}
int JJ = inode*2; Left subtree
iter = Find (_si.begin (), _si.end (), JJ);
if (ITER!= _si.end ())
{
V.push_back (Make_pair (_sm[JJ], JJ));
_get_subtree (JJ, V);
}
  
++JJ; E.q. inode*2+1
Right subtree
iter = Find (_si.begin (), _si.end (), JJ);
if (ITER!= _si.end ())
{
V.push_back (Make_pair (_sm[JJ], JJ));
_get_subtree (JJ, V);
}
}
void Songke_binarytree::_get_subtree (bool bleft, iNode of int, vector< pair<string, int> >& v)
{
_get_subtree (Bleft inode*2:inode*2+1, v);
}
void Songke_binarytree::_postorder_iterate (const vector< pair<string, int> >& v)
{
vector< pair<string, int> > L, R;
Pair<string, int> f = *v.begin ();
Generate the new subtree L and R
_get_subtree (True, F.second, L);
_get_subtree (False, F.second, R);
Postorder algorithm
if (!l.empty ()) _postorder_iterate (L);
if (!r.empty ()) _postorder_iterate (R);
_post.push_back (F.first);
}
void Songke_binarytree::p ost_out ()
{
_postorder_iterate (_s);
_out ("Postorder:", _post);
}

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.